codescracker


python

Python Variable Scope



« Previous Tutorial Next Tutorial »


Here you will learn about variable scope in python, that is, local variable and global variable in python.

Python Local Variable Scope

Here is an example of local variable scope in python.

def checkin():
    sop = 10;
    print("Value of variable sop is",sop);
sop = 5;
print("Value of variable sop is",sop);
checkin();
print("Value of variable sop is still",sop);

Here is the sample output produced by above variable scope in python example code.

python local variable scope

As you can see from the above example, the variable sop with value 5 is global variable, and the variable (same variable, that is sop) inside the function checkin() is the local variable, therefore the value 10 is initialized to it is local and holds only upto that function, that is when you exit from that function and comes to the main, then again 5 is the value that will be printed by the same variable.

Python Global Variable Scope

Here is an example of global variable scope in python. To change the value of any variable globally, then use global keyword as shown in the following example.

def checkin():
    global sop;
    sop = 10;
    print("Value of variable sop is",sop);
sop = 5;
print("Value of variable sop is",sop);
checkin();
print("Now the value of variable sop is",sop);

Here is the sample output of the above global variable scope example code in python.

python global variable scope

As you can see from the above example, when you use the global keyword to make the variable global and then change its value, changes the value of the same variable globally after that two statements.

Python Variable Scope Example

Here is an example illustrating global and local scope of a variable inside the function and outside the function in python.

def read_from_global():
    print("I am inside the local scope of read_from_global().");
    print("Value of the variable val is",val);
def local():
    val = 100;
    print("I am inside the local scope of local().");
    print("Value of the variable val is",val);
def change_global_variable():
    global val;
    val = 100;
    print("I am inside the local scope of change_global_variable().");
    print("Value of the variable val is",val);

# here the value 50 given below and initialized to val variable is a global
# variable just because, it is created and initialized in global scope here
val = 50;
print("I am in the global scope.");
print("Value of the variable val is",val);
read_from_global();
print("I am back in the global scope.");
print("Value of the variable val is still",val);
local();
print("I am again back in the global scope.");
print("Value of the variable val is still",val);
change_global_variable();
print("I am again back in the global scope.");
print("Now this time, the value of the variable val becomes",val);
print("exiting...");

After running the above example of global and local variable scope in python function, here is the sample output you will see on your output screen:

python variable scope

« Previous Tutorial Next Tutorial »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test