over 9 years ago
In Python variables in a program which may not be accessible at all locations in python program. This depends on where you have declared a variable and value.
The scope of a variable determines the portion of the program using which we can access a particular identifier and variable. There are two basic scopes of variables in Python to given below
Global variables-
Local variables-
And for example you can see below code-
- # Function definition is here
- def sum( arg1, arg2 ):
- # Add both the parameters and return them."
- total = arg1 + arg2; # Here total is local variable.
- print "Inside the function local total : ", total
- return total;
- # Now you can call sum function
- sum( 40, 60 );
- print "Outside the function global total : ", total
# Function definition is here def sum( arg1, arg2 ): # Add both the parameters and return them." total = arg1 + arg2; # Here total is local variable. print "Inside the function local total : ", total return total; # Now you can call sum function sum( 40, 60 ); print "Outside the function global total : ", total
And output-
Inside the function local total :100
Outside the function global total :0
0 Comment(s)