× Python Introduction What is Python Python Features Python History Python Applications Python Install Python Path Python Example Execute Python Keywords Constant Variable Statements & Comments Python I/O and Import Operators UnaryBinaryTernary Unary Operators Unary Minus Binary Operators Arithmetic Operators Assignment Operators Relational Operators Logicaloperators Bitwise Operator Ternary Operators Control Statements in Python conditonal Statements IF if else Else If Nested if Switch For loop Nested For Loop While Loop Nested while Loop Unconditonal Statemets Continue Break Pass FUNCTIONS Python Function Function Argument Python Recursion Anonymous Function Python Modules NATIVE DATATYPES Python List Python Numbers Python Tuple Python String Python Set Python Dictionary OOPS PRINCIPALS Encapsulation Class Variable Method Object Or Instance CreationMethod Calling OOPS Syntax And Explanation DATA ABSTRACTION Constructor Inheritance 1.Single or simple Inheritance 2.Multilevel Inheritance 3.Hierarchical Inheritance 4.Multiple Inheritance 5.Hybrid Inheritance Operator Overloading File Operation Python Directory Python Exception Python - Multithreading Python - Database Access Python - CGI Python - Reg Exp Python - Date Python - XML Processing Python - GUI
  • iconPython Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Python Global, Local and Nonlocal variables

In this tutorial, you’ll learn about Python Global variables, Local variables, Nonlocal variables and where to use them.

Global Variables
In Python, a variable declared outside of the function or in global scope is known as a global variable. This means that a global variable can be accessed inside or outside of the function.
Let's see an example of how a global variable is created in Python.
Example 1: Create a Global Variable
x = "global"

def myfun():
    print("x inside:", x)


myfun()
print("x outside:", x)


Output
x inside: global
x outside: global
In the above code, we created x as a global variable and defined a myfun() to print the global variable x. Finally, we call the myfun() which will print the value of x.
What if you want to change the value of x inside a function?
    
x = 10

def myfun():
    x = x * 2
    print(x)

myfun()



Output
UnboundLocalError: local variable 'x' referenced before assignment

The Output shows an error because Python treats x as a local variable and x is also not defined inside myfun().
To make this work, we use the global keyword. Visit Python Global Keyword to learn more.
Local Variables
A variable declared inside the function's body or in the local scope is known as a local variable.
    
Example 2: Accessing local variable outside the scope
def myfun():
    y = 10
    print(y)


myfun()
print(y)



Output
NameError: name 'y' is not defined
The Output shows an error because we are trying to access a local variable y in a global scope whereas the local variable only works inside myfun() or local scope.
Let's see an example on how a local variable is created in Python.
Example 3: Create a Local Variable
Normally, we declare a variable inside the function to create a local variable.

def myfun():
    y = 10
    print(y)

myfun()


Output
local
Let's take a look at the earlier problem where x was a global variable and we wanted to modify x inside myfun().

Global and local variables
Here, we will show how to use global variables and local variables in the same code.
    
Example 4: Using Global and Local variables in the same code
x = 10 

def myfun():
    global x
    y = 20
    x=x+10
    print(x)
    print(y)

myfun()



Output
global global 
local
In the above code, we declare x as a global and y as a local variable in the myfun(). Then, we use multiplication operator * to modify the global variable x and we print both x and y.
After calling the myfun(), the value of x becomes global global because we used the x * 2 to print two times global. After that, we print the value of local variable y i.e local.
    
Example 5: Global variable and Local variable with same name
x = 5

def myfun():
    x = 10
    print("local x:", x)


myfun()
print("global x:", x)



Output
local x: 10
global x: 5
In the above code, we used the same name x for both global variable and local variable. We get a different result when we print the same variable because the variable is declared in both scopes, i.e. the local scope inside myfun() and global scope outside myfun().
When we print the variable inside myfun() it Outputs local x: 10. This is called the local scope of the variable.
Similarly, when we print the variable outside the myfun(), it Outputs global x: 5. This is called the global scope of the variable.
Nonlocal Variables
Nonlocal variables are used in nested functions whose local scope is not defined. This means that the variable can be neither in the local nor the global scope.
Let's see an example of how a nonlocal variable is used in Python.
We use nonlocal keywords to create nonlocal variables.
    
Example 6: Create a nonlocal variable
def outer():
    x = "local"

    def inner():
        nonlocal x
        x = "nonlocal"
        print("inner:", x)

    inner()
    print("outer:", x)


outer()


Output
inner: nonlocal
outer: nonlocal
In the above code, there is a nested inner() function. We use nonlocal keywords to create a nonlocal variable. The inner() function is defined in the scope of another function outer().

Note : If we change the value of a nonlocal variable, the changes appear in the local variable.

Python Global, Local and Nonlocal variables

Image