Q. What is the difference between local and global variables?
Answer =
Local variable:-
• It is a variable which is declared within a function or within a block.
• It is accessible only within a function/block in which it is declared.
Global variable: -
• It is variable which is declared outside all the functions.
• It is accessible throughout the program.
Example :-
def sum( ):
a = 20 # Here a is local variable
b = 30 # Here b is local variable
print("Value of a in fuction = ",a)
print("Value of b in fuction = ",b)
su = a+b
return su
a =10
print("Value of a out of fuction = ",a)
print("Sum of a and b ",sum())
print("Value of a out of fuction = ",a)
Output :-
Value of a out of fuction = 10
Value of a in fuction = 20
Value of b in fuction = 30
Sum of a and b = 50
Value of a out of fuction = 10
Value of a in fuction = 20
Value of b in fuction = 30
Sum of a and b = 50
Value of a out of fuction = 10
>>>
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )