Scope of Variables in Python

Python variables are used to store the different type of data. We first need to assign a data to variable and then we can use inside anywhere in program.

Example of Python Variable

id=10908
name="Henry Fort"
salary=80000

print(id)
print(name)
print(salary)
Output of the above program is

10908
Henry Fort
80000

Scope of Variables

Scope variables decide the visibility of a variable in your program.

Global Scope Variables

Global variables are that defined outside the function and that are not specified to any function. These variable can be used in any part of the program.

Example of Global Scope Variable

def show():   
    print(s)  
    
# Global scope  
s = "Scope of Python Variables"
show()  
Output of the above program is

Scope of Python Variables

Example of Global Scope Variable with the same name of variable inside function

def show():   
    s = "Inside show() Function"
    print(s)  
    
# Global scope  
s = "Scope of Python Variables" 
show()  
print(s) 
Output of the above program is

Inside show() Function
Scope of Python Variables

Description of Output

Here when we call the show() function then the preference goes to the variable that is inside the function.

Local Scope Variables

Local variables are defined or declared inside the function block. These variables are not allowed to be used outside the function block or in global scope.

Example of Local Scope Variables

def show():
    text = "Inside show() Function" #text is a local scope variable
    print(text)

# Global scope
s = "Scope of Python Variables"
show()
print(text) #error in this line

Output of the above program is

Inside show() Function
Traceback (most recent call last):
File “E:/python/khusboo/test-py.py”, line 9, in
print(s1)
NameError: name ‘s1’ is not defined

Example to change the value of global variable inside the local scope

# Global scope
s = "Scope of Python Variables"

def show():
    print(s)
# error in this line
    s = "Inside show() Function"
    print(s)

show()
print(s)

Output of the above program is

Traceback (most recent call last):
File “E:/python/khusboo/test-py.py”, line 9, in
show()
File “E:/python/khusboo/test-py.py”, line 5, in show
print(s)
UnboundLocalError: locaDesl variable ‘s’ referenced before assignment

Description of Output

In this example we can not change the value of global variable. When we try to change the value that line is assumed as the declaration of local variable inside the function. We can not use variable without creating it so the error will come at 5 line in program.

Example to change the value of global variable using the global keyword

By using the global keyword while changing the value of global variable inside the local scope of variable overcomes the problem that occurred in the last program.  

# Global scope
text = "Scope of Python Variables"

def show():
    global text
    print(text)
    text = "Inside show() Function"
    print(text)

show()
print(text)
Output of the above program is

Scope of Python Variables
Inside show() Function
Inside show() Function


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.