codingstreets
Search
Close this search box.
python-scope

Introduction to Python Scope with Examples

In this article you will learn about Python Scope.

Python Scope – Before moving ahead, let’s know a little bit about Python def function

Scope – A variable that is created inside the function and can be used only from inside the function is called local scope.

Local Scope – A variable that is created inside the function and can be used only from that function is called local scope.

Example 1- Created a local variable inside a function.

def Myfunction():
       x = 1
       print(x)
 
Myfunction() 
python-scope

Explanation – In the above example, a local variable name x value is taken from a function Myfunction() and executed result.

Function inside Function – It is defined as a function is created inside another function.

Example 2- The local variable (x) can be used from a function (Myfunction) inside another the function

def Myfunction():
     x = 1

     def innerfun():
         print(x)
     innerfun()
 
Myfunction() 
python-scope

Explanation In the above example, local variable name x value is taken from another function (innerfun()) that is already inside another function that is Myfunction().

Global Scope – A variable which is created outside the function can be accessed from any scope (global, local) inside the function.

Example 3- Created a global variable and accessed by any scope.

x = 2

def Myfunction():
     print(x)
 
Myfunction()
print(x)                                          
python-scope

Explanation In the above example, local variable is treated as global variable, therefore it returned value from both print() function.

Naming Variables – Use of same variable name inside and outside the function, Python will treat it as two different variables.

Variable outside the function will be treated as global scope and variable inside the function will be treated as local scope.

Example 4- Use same variable name for local and global scope.

x = 1

def myfunc():
    x = 2
    print(x)

myfunc()
print(x) 
python-scope

Explanation In the above example, as same variable name is used for local and global variable, therefore it executed them one by one as print() function is used for both local and global variable.  

Global Keyword – A local variable can be converted into the global variable by using global keyword.

Example 5- Use of global keyword to access local variable as global variable.

def myfunc():
    global x
    x = 1

myfunc()
print(x) 
python-scope

Explanation In the above example, as global keyword is used inside function, therefore it returned x value because variable is treated as global variable.

Use global keyword to make changes in global variable inside the function.

Example 6- Use global keyword to bring change inside function.

x = 2

def myfunc():
    global x
    x = 1

myfunc()
print(x) 
python-scope

Explanation – In the above example, as global keyword is used inside function, therefore x value is changed to its latest value.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment down below.

Like us on

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on