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.
Example:1
def abc(): a=15 //local variable print(a) a=95 // global variable print(a) abc() print(a)
Output:
95
15
95
>>>
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:
Example:2
a=500 def hello(): a=50 print("a = ",a) a+=100 print("a = ",a) print(a) a=a+100 hello() print(a)
Output:
500
a = 50
a = 150
600
>>>
Example:3
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)
Output:
90
a= 60
a= 120
450
>>>
Example:4
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)
Output:
170
a= 15
a= 25
340
>>>