Home / Computer Science / Python / Variables in Python

Variables in Python

 

Variables in Python:

A variable is address of a memory, which can store the value and accessible to the program when it is needed. The value of the variable is varying from one to another scenario.  Python is a programming language which is widely used variables in its programming.

Basically, variables are two types based on their scope in the program.

Local variables

Global variables

Rules for declaring a variable:

  1. Variable name must start with alphabet or _ (Underscore) symbol.
  2. Names are case sensitive (i.e, ABC and abc are not equal or same).
  3. Variables contains only alphabets or numbers or combination of both along with underscore.
  4. Must not start with a number.

Declare a variable:

As I said in the above, a variable is used to store a value.

Here “x” is a variable which has a value of 100.

So, when we use x in any case the value of x is 100.

See the below demo code.

 


>>> x = 100
>>> print x
100

Declare a variable in Python
Declare a variable in Python

If we want to change the value of x or re-declare the value of x follow like below.


x =100;
print x;
# change the value of x to 150
x =150;
print x;

Re-Declare a variable in Python
Re-Declare a variable in Python

Types of variable declaration:
Multiple variable to Multiple values:
In one line of statement assign multiple variables to multiple values. Here is the demo.

Multiple Variable Assign
Multiple Variable Assign


>>> x, y, z = 1,2,3
>>> print x
1
>>> print y
2
>>> print z
3
>>>

If we assign insufficient values to the variables it throws an error like ValueError: need more than 2 values to unpack. In this case I am assigning only two values for three variables, so it throws an error.

ValueError in Python
ValueError in Python

Multiple variables to Single Value:
In python, there is a possibility to assign single value to multiple variables in a single statement or line of code.

>>> x=y=z=100;
>>> print x
100
>>> print y
100
>>> print z
100
>>>

Multiple Variables to a Single Value
Multiple Variables to a Single Value

Here in this case I am assigning x,y,z to value 100.

Concatenate & Output variable:

For some cases, we need print variable name along with answer and text. For example, finding an area of square requires output like ”Area of the square is +area”. In these cases, we just concatenate the text and variable into a single line.
See the below demo.

Concatenate variables in Python
Concatenate variables in Python


#assign a vlaue to side of the square
x = 24;
str1="side of a square is:"
print ( str1+str( x))
#caliculate area
area = x*x;
str2="area of the square is "
print( str2+ str(area))


#assign a vlaue to side of the square
x = 24;
print ( "side of a square is:"+str( x))
#caliculate area
area = x*x;
print( "area of the square is "+ str(area))

In the above program, side of square is string and 24 is integer, so it’s not possible to concatenate integer and string together and it throws an error.

#assign a vlaue to side of the square
x = 24;
print ( "side of a square is:"+ x)
#caliculate area
area = x*x;
print( "area of the square is "+ area)

Error - String to Int
Error – String to Int

Here is some more example for assigning of values and its concatenation.

Concatenation in Python
Concatenation in Python


>>> a = "Hi"
>>> b = "Learners choice"
>>> c = "Viewer"
>>> print a+b+c
Hi Learners choice Viewer
>>> x =5
>>> y =10
>>> print (x+y)
15
>>>

String and Int in Python
String and Int in Python

Deletion of a variable:
If any variable you want to remove, then use “del” with variable name
del x
After executing the del x, it is giving an error, which means that the x was deleted from the memory location.

Deletion of variable
Deletion of variable

Local variables and Global variables:
Global variables scope is reached any where in the program whereas local variable scope is reached with in the code block or function.
See the below picture for better understand.

Scope of Variable
Scope of Variable

In the above diagram a is having scope in block 1 and b is scope in block 2 and c have scope in block 3. But the variable X can have scope of all the three blocks. This means that the variable x is utilized by the all the three blocks. So, x is global variable and a,b and c are local variables.


#variable declare
a = 8
print (a*a)
#define function to distinguish local and global variables
def distVariable():
#Local declaration
a="Hi techies"
print (a)
distVariable()
#global declaration
print(a)

Local vs global in Python
Local vs global in Python

By using “global” keyword the variable scope shall be maintained as global variable and that variable has a scope through out the program.
So, if we change the previous program the output like.

#variable declaration
a = 8
print (a*a)
#define function to distinguish local and global variables
def distVariable():
#global keyword
global a
print(a)
#local variable
a="Hi techies"
print (a)
distVariable()
#global declaration
print(a)

Variable Scope in Python
Variable Scope in Python

About GSK

Hi, i am Santosh Gadagamma, a tutor in Software Engineering and an enthusiast for sharing knowledge in Computer Science and other domains. I developed this site to share knowledge to all the aspirants of technologies like, Java, C/C++, DBMS/RDBMS, Bootstrap, Big Data, Javascript, Android, Spring, Hibernate, Struts and all levels of software project design, development, deployment, and maintenance. As a programmer I believe that, "The world now needs computers to function." Hope, this site guides you as a learning tool towards greater heights. I believe that Education has no end points and i wish to learn more in the process of teaching you.

Check Also

IDLE in Python (Integrated Development & Learning Environment)

While installing, python is come up with IDLE (GUI). That means it is GUI for …