In [8]:
def area_circle(radius):
return pi * radius**2
pi = 3.14
print(area_circle(5))
In the above program, even though pi is not defined in area_circle function, it did not throw error, because it managed to get the pi value from global scope .
#Rule 2 - Scope Limits</u>
We can only use a variable from global scope in a function, but we cannot modify or reassign it inside a function.
We can only use a variable from global scope in a function, but we cannot modify or reassign it inside a function.
In [11]:
a = 10
def prod_a():
a = a*a
return a
print(prod_a())
In [12]:
a = 10
def prod_a():
b = a*a
return b
print(prod_a())
#Rule 3 - Global Variable :</u>
We can access modify global variable (declared as global) from both local and global scope
We can access modify global variable (declared as global) from both local and global scope
In [24]:
global b
b = 10
def prod_a():
global b
b = 2*b # It modifies the global variable b
return b
print(prod_a())
print(b)
In [ ]:
No comments :
Post a Comment