local and Global scope of variables
Scope of a variable is defined as the area with in which the variable is accessible.
variable is a label for a location in the memory.
Not all variables are accessible, and not all variables exist for the same amount of time.
types of variables:
variables can be local and global
local variables are the variables that can be accessed in a local scope .ie with in the respective loop/function/if-else statement etc
global variables are the variables that can be accessed in globally
print("case 1 " , a)
''' in the above case 10 is printed as it takes local value of a
print("case 2" ,a)
''' in the above case 25 is printed as the program could not find a in local scope '''
'''Global key word helps the programmer to get the permission for python to change the global variable
in the local scope'''
print("case 3" ,a)
In the above example the value of a is changed using the Global key word
'''Nested functions and their scope .'''
a = 25 # this is local scope of functon 1
print("This is function 1" ,a)
global a # this is local scope of function 2
# print("this is function 2", a)
print("This is function 1 after call of function 2" ,a)
1.a is a local variable of function 1
2.function 2 is trying to change the global variable a (which is not the same as a of function 1)
3.result of before and after function one stays the same as 25 .
4.since global key word does not changes , value of other local variables.
case 1 10
case 2 25
case 3 50
This is function 1 25
This is function 1 after call of function 2 25
you can copy the code to your python interpreter and it will work.