Before we start learning the variable concept in python let’s go back to school days to revise an important concept of geometry.
let’s calculate the area of a circle.
If you still remember the area of a circle can be calculated using the formula πr2.
Here the value of the pi would be 3.1416 and let’s assume the radius of the circle is 3 meters.
Now open the python shell and calculate the area of the circle.
Although the above syntax works fine, there is one major limitation. Here the radius of the circle is hardcoded.
Therefore, if you want to change the radius of the circle then you have to rewrite the same syntax again and again.
And the code repetition is not good practice while working for real-world projects.
Here variable concept comes into the picture.
Let’s use variable and rewrite the above code syntax using the variable.
What is Variable in python
A variable can be understood as a kind of box in which value can be stored.
That box usually corresponds to a memory location in the computer memory.
a = 10
The symbol equality (=) must be understood as an assignment, not as a mathematical equality.
As you could see in the above example, a is a variable (unique identifier) that represents value 10.
In simple words, we are asking python to keep the value 10 in a “box”, and later if we want to access the value we can simply use the variable name to retrieve or re-assign the value.
How to declare a variable
In python, variables are created when they are defined and assigned a value for the first time.
It is very easy to define a variable in python.
You just have to tell the name of a variable and assign a value to that variable using the assignment operator
However, If you are coming from Java then you would be a little surprised to see the variable declaration in python.
In python, you don’t have to explicitly declare the type of the variable.
Thus, we can say that Python is not a statically typed programming language.
pi = 3.14159 radius = 3 area_of_circle = pi * (radius ** 2) print(area_of_circle)
Here ‘pi’, ‘radius’, and ‘area_of_circle’ are the variables.
You can assume that a variable is nothing but a container that represents a value temporarily.
In the above example, the variable
pi is representing value 3.14159,
radius representing 3 and area_of_circle is used to represent the area of the circle ( πr2 ) or value
How to re-declare a variable
message = 'Hello' message = 101 print(message)
What will be the output of the above program?
Runtime error, ‘Hello’ or 101?
It is 101.
In python, you can re-declare a variable anytime.
Because python is not a statically typed language, python will determine the type of that variable from its value.
Key Points to remember while creating variables in python
1. The variable name can start only with an underscore or alphabets
Below are valid Syntax
_config_value = 10 __q = 10 sum = 10
Below are invalid syntax
0q = 10 $a = 'Apple' pi$ = 3.14159 p$i = 60 &pi = 20 p&i = 3.1416 *hsa = 10
2. Variable name should be meaningful
Although it is not mandatory, it is recommended that the name of the variable should be meaningful to the information stored in it.
sum = 10 ddh = 10
In the above example, there are two variables
Both variables represent the same values 10.
but only ‘sum’ clearly tells us that this variable represents the summation of numbers.
Therefore, to make it easier to understand the program always use the meaningful name of variables.
3. Reserved keywords are not allowed for variable names.
False = 10 except = 20
You can’t use reserved keywords for the variable names. The above syntax is not valid.
SyntaxError: invalid syntax
4. A variable name cannot start with a number.
A variable name can’t start with a number.
File ".\variable.py", line 19 1abc = 20 ^ SyntaxError: invalid syntax
5. A variable name can only contain the alpha-numeric characters and the underscore.
student_name='Mark' employee_age=32 __is_employee=True
The above variable declaration is valid.
The name of a variable must start with a letter or an underscore (_) and can continue with more letters, numbers or underscores.
Also, when the name of a variable contains several words, it is advisable to separate them with underscores for better readability.
sum_of_two_numbers = 56 student_name = 'Mark'
6. Variables are the case-sensitive
# Case sensitive name = "Mark" print(Name)
For the variable ‘
name‘, we have assigned the value ‘Mark’.
However, we are trying to print the value of Name (capital N) which is not valid.
Traceback (most recent call last): File ".\variable.py", line 28, in <module> print(Name) #NameError: name 'Name' is not defined NameError: name 'Name' is not defined
How to know the type of a variable
In python, it is easy to determine the type of a variable.
we can use type() function.
a = 10 print(type(a))
The output of the above program would be
a = 10.45 print(type(a)) a = False print(type(a)) a = "Hello World" print(type(a))
The output will be
<class 'float'> <class 'bool'> <class 'str'>
How to assign values to multiple variables
You can assign a single value to multiple variables or multiple values to multiple variables in a single line
# Assign a single value to multiple variables a = b = c = d = e = 10 print(e)
# Assign different values to multiple variables a , b , c , d , e = 10, 10.34, 'Hello', False, 12
In this tutorial, we discussed about variables and how to create them.
We also discussed do’s and don’t while declaring variable names with few examples.