Types of variables:-
We can define two
types of variables in python. They are:
- global variables
- local variables
1)global variables:-
- The variables which are declared outside of the functions are known as global variables.
- global variables can be accessed in all the functions. In order to modify the global variables data within the functions we need to follow forward declaration mechanism.
Syntax:-
global <global_variable_name>
x=0
def f1():
global x
x=x+1
print("in f1")
if x<=5:
f1()
f1()
in f1
in f1
in f1
in f1
in f1
in f1
2)local variables:-
- The variables which are declared within the function are known as local variables.
- Local variables of one function cannot be accessed in other functions.
z=300
def f1():
x=100
print(x)
global z
print(z)
z=400
print("in f1")
def f2():
y=200
print(y)
print(z)
print("in f2")
f1()
f2()
100
300
in f1
200
400
in f2
No comments:
Post a Comment