Python Menu

Variables in Python



   A variable is a named memory location in which we can store values for the particular program.In other words, Variable is a name which is used to refer memory location. Variable also known as identifier and used to hold value.

Creating Variables in Python

In Python, We don't need to declare explicitly variable in Python. When we assign any value to the variable that variable is declared automatically.

In Python, We don't need to specify the type of variable because Python is a loosely typed language. I.e. In loosely typed language no need to specify the type of variable because the variable automatically changes it's datatype based on assigned value.

For Example:

   a=10       Where variable a is an integer datatype.

   b="Glance"    Where variable b is string datatype.

Rules for naming variable:

  • Variable names can be a group of both letters and digits, but they have to begin with a letter or an underscore.
  • It is recommended to use lowercase letters for variable name. ‘SUM’ and ‘sum’ both are two different variables.


 Python Example Program - Variables in Python


Example: "vardemo.py"
a=10                #integer
b="StudyGlance"     #string
c=12.5              #float
print(a)
print(b)
print(c)
Output: $python3 vardemo.py
10
StudyGlance
12.5

Assign Value to Multiple Variables

Python allows us to assign a value to multiple variables and multiple values to multiple variables in a single statement which is also known as multiple assignment.


Assign single value to multiple variables :

Example: "vardemo1.py"
x=y=z=50  
print x  
print y  
print z 
Output: $python3 vardemo1.py
50
50
50
Assign multiple values to multiple variables :

Example: "vardemo2.py"
a,b,c=5,10,15  
print a  
print b  
print c 
Output: $python3 vardemo2.py
5
10
15

Next Topic :Data Types in Python