Local and Global Variables
We can have local and global variable of the same. But in such situations value of local variable is present only within the local scope i.e. inside the function. and value of global variable is present in global scope i.e. outside the function.
def abc(): a=15 //local variable print(a) a=95 // global variable print(a) abc() print(a)
In the above program variable a is declared in local as well as global scope. But the value of a in both the region is different . The variable ‘a’ in abc() is local to this function but outside this function variable a is a global variable.
Solve the following:
a=500 def hello(): a=50 print("a = ",a) a+=100 print("a = ",a) print(a) a=a+100 hello() print(a)
a = 50
a = 150
a=60 def abc(): a=50 a+=10 print("a= ",a) a*=2 print("a= ",a) a+=30 print(a) abc() a*=5 print(a)
a=160 def abc(): a=15 print("a= ",a) a-=10 a**=2 print("a= ",a) a+=10 print(a) abc() a*=2 print(a)