What is Variable?

The memory location is reserved to store the value by the interpreter when a variable is created.
The value of any data type can be stored on the variable. For example, Number, string, list, tuple, dictionary


Assigning Value to Variable

The declaration is not required in Python. Declaration occurs automatically when the value is assigned to the variable.

Python does not have a default value of variable due to a lack of declaration.


For example:-

Source code:


a = 10                   #Number
b = "Hello"          #string
c = [1,17,21]       #list
print (a, b, c)

Assigning a value to the variable 



Output:

5 Hello [1,17,21]


Changing Variable's Value

The value of the variable can be changed or re-assigned in Python.

Source Code:




a = 10
print (a)
b = "Hello"
print (b)
c = [1,17,21]
 print (c)

changing variables value

Output:-

5

Hello
[1,17,21]



Assigning Single Value to Multiple Variables

In Python, the same value can be assigned to more than one variable.

Source Code:



a = b = c = d = "Hello"
print (a) 
print (b) 
print (c) 
print(d)

Assigning a single value to multiple variables


Output:


Hello

Hello
Hello
Hello



Assigning Value to Variable according to order

Value is stored on the variable in Python in order.

For example, the same memory location multiple variables and their values ​​are assigned.


Source Code:



a, b, c = 1, 'Hello', [1, 17]
 print (a)
 print (b) 
 print (c)

Assigning a value to variables according to order


Output:

1
Hello
[1, 17]


Variables Concatenation

In Python, variables of the same data types can be concatenated.

On the example:-


 str () function has been used to convert the object from integer to string.


Source Code:



a = 1
b = 17
print (a + b)
print (str (a) + str (b))
c = "Hello"
print (str (a) + c)

Variable concatenation

Output:

18

17
1Hello



Types of Variables

There are two types of variables in Python.


  • Local Variables
  • Global Variables

1. Local Variables

Local Variables; are inside functions. Their visibility is only inside the function, when they come out of the function, then they are destroyed.


Source Code:



def func (): 
 a = 17                #local variable
print (a) 
func ()
print(a)

Local variable
Output:

17

Traceback (most recent call last):
    print (a)
NameError: name 'a' is not defined

2. Global Variables


Global Variables; are outside the function. Their visibility is in and out of function. Their scope is on the whole program.



a = 17                       #global variable
def func (): 
 print (a) 
func () 
print (a)


global variable

Output:

17
17

Both local and global variables are declared on the example:


The variable outside the function is global and the variable inside is local. The scope of the global variable is in and out of the function, but due to a separate variable declaration inside the function, the value of the variable changes as soon as calling func ().



Source Code:



a = 17                                       #global variable
def func (): 
print (a)
func () 
print (a)

declare variable

Output:
1
17


With 'global' and without 'global' Keyword

The keyword 'global' is also used for the variable in the function.


  • Without global


On the example, the 'a' variable has been printed before the declaration of 'a' variable, due to this the exception 'UnboundLocalError' has occurred.



Source Code:



def func (): 
print (a) 
a = "local" 
print (a) 
a = "global"
func () 
print (a)


without global


Output:
 in func
    print (a)
UnboundLocalError: local variable 'a' referenced before assignment


  • With global


On the example, the global keyword has been used in the beginning and after that variable a has been printed. Wherever in the program, there will be a global variable named 'a', it will be printed first as soon as calling func ().


Source Code:



def func (): 
global a
 print (a)                               #print global
 a = "Hello" 
print (a)                               #print local
a = "Everyone"
func ()
 print (a)                              #print local

with global


Output:
Everyone
Hello
Hello



No-Local or No-Global Variable

The 'nonlocal' variable is used for nested function. The 'nonlocal' variables are not even global and are not global either. If they are taken in the inner function, then their values ​​in the outer functions do not change.


  • Without nonlocal


Source Code:



var = 1
def outer ():
 var = 17
def inner ():
 var = 21
 print ("inner:", var)
inner ()
print ("outer:", var)
outer ()
print ("global:", var)

without nolocal
Output:
inner: 21
outer: 17
global: 1

  • With nonlocal
Source Code:


var = 1
def outer (): 
var = 17
 def inner (): 
nonlocal var
 var = 21 
print ("inner:", var) 
inner () 
print ("outer:", var) 
outer () 
print ("global:", var)

with nonlocal

Output:
inner: 21
outer: 21
global: 1