In this tutorial you will learn:
- What is a variable
- How to create variables in python
- How to use print to display variables
- How to read user inputs
1 What is a variable:
To understand what is a variable lets imagine that we have two boxes, the first box contain some clothes and the second one
contain some video games CD’s as you see our two boxes are containers that holdes some objects.
Now lets say the we want to get our video games CD’s, to do that we simply open the box that contain the video games, but
how can we know which box does it contain the CD’s ? if the two boxes looks the same, this is actually not
a big problem for two boxes but if we have much more boxes it will be a very difficult task to find the specific box that we want.
To solve this problem we add a label to the box, the label will describe the content of this box so that we can destinguish between the boxes.
Variables are simply just like this boxes we create them to hold some data that we use during the program life cycle, each variable has a name that describe
its content.
Now that you know what is a variable, lets see how to make one in python.
2 How to make variables in python:
To make a variable in python we first define the name of our variable and then we use the ‘=’ sign that we call it the assignement sign to assign values to our variable.
0video_games_CDs = 65
As you see we have asigned 65 to the video_games_CDs variable, this variable name describe its content so that 65 means the number of CDs that we have.
In python and other programming language we have some rules to follow to create a correct variable name, this rules are the following:
Variable can only be a combination of letters lowercase or uppercase(a-z)(A-Z) or digits (0-9) or an underscore _ Exmeple: MyVariable, my_variable, my_Variable1
Variable can only start with letter or underscore, it can not start with digits
Variable can not contain any special symbol like @, #, $, %, !, * etc…
Variable can not be a special keyword (special keyword are some names reserved by python)
Variables in python are case-sesitive which mean that MyVariable and myvariable are two different variables.
The list of reserved keywords:
None | class | is | lambda | nonlocal | raise |
True | and | not | if | global | for |
False | assert | in | elif | import | from |
as | del | or | except | with | |
def | else | return | try | while | |
break | continue | yield | finally | pass |
Now lets see some examples:
0continue = 5
Now you know how to create a variable now lets see how to print it.
3 How to use print to display variables:
We use the print() function to outpout anything to our console window (we will learn more about functions in later tutorial).
Lets print now our video_games_CDs variable.
0video_games_CDs = 65
1print(video_games_CDs)
As you see our print function prints out the value of our variable. We can add more argument to the print function by seperating each argument with a comma ‘,’
0cars = 3
1houses = 2
2
3print("Hey i have", cars, "cars and", houses, "houses")
In this example we have passed 5 arguments to our print function which demonstrate that we can add as much as we want of arguments.
Note that if we want to pass a string (which is a data type in python, we will learn more about string later in another tutorial)
we use the double quotes “” we can also use single quotes “.
Now lets see this example:
0print("Blue", "Silver", "Red")
When your run this code you will see that each number is seperated by space, this is because the default arguments in the print function are seperated by space
but we can change that by passing ‘sep’ argument to the print function.
lets see how to do that:
0print("Blue", "Silver", "Red", sep=', ')
As you see our values are now seperated with comma and a space, you can define any seperator you want.
Now lets see another useful argument that can be passed to the print function.
0print("Hello")
1print("World")
> OUTPUT: World
When you run this code you will see that each word is printed on a seperate line, this is due to the default value of the “end” argument
this argument define how do we want to end the world in the print function, by default it is end line, which force the interpreter to return to the line
when the print function outpout its content.
Lets see an example on how to change this default end line.
0print("Hello", end=" ")
1print("World")
As you see we have changed the value of the “end” argument from end line which is represented by “\n” to an empty space this caused the interpreter
to output the next value of the print function on the same line of the first print function output.
Now that you know how to create a variable in print it out lets see how to read user inputs.
4 How to read user inputs:
To read user input in python we use the input() function.
0user_name = input("Please enter your name: ")
1print("The user name is:", user_name)
When you enter your name and hit enter, the value will be assigned to the user_name variable and it will be printed in the print function.
NOTE:
print(): is a function in python 3.x but in python 2.x is a statement: print “something”.
input(): is a function that read use inputs in python 3.x, but in python 2.x we use raw_input() function to read user inputs.
Exercices Section:
This section will contain some exercices to practice what you have learned.
Exercice 1
Which one of this variables name is correct:
- Age = 56
- $My_age = 45
- __userName = ‘KANIKI’
- break = 2
- @email = “email@mail.com”
Exercice 2
Write a program that ask the user to write his name and age, and assign the values to (user_name and user_age) then print the name and the age of the user like the following:
OUTPUT: Hi my name is your_username and i am you_userage
Exercice 3
month = 3
day = 16
year = 2018
use the sep argument of the print function to print the variables like the following result:
OUTPUT: 16/3/2018