English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Python Functions

Python Data Types

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Python Knowledge

Python Reference Manual

Python Global, Local, and Nonlocal Variables

In this article, you will learn about Python global variables, local variables, nonlocal variables, and where to use them.

Global variables

In Python, variables declared outside a function or in the global scope are called global variables. This means that global variables can be accessed inside or outside a function.

Let's see an example of how to create a global variable in Python.

Example1:Creating a global variable

x = "global"
def foo():
 print("internal x:", x)
foo()
print("external x:", x)

When we run the code, the output will be:

internal x: global
external x: global

In the above code, we willxcreated as a global variable, and defined foo() to print the global variablex。Finally, we call foo() to printx

If you want to change the value inside a functionxWhat about the value?

x = "global"
def foo():
 x = x * 2
 print(x)
foo()

When we run the code, the output will be:

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 in foo().

To achieve this functionality, we used the global keyword. For more information, please visitPython global keyword

Local variables

Variables declared within a function or local scope are called local variables.

Example2:Accessing a local variable outside its scope

def foo():
 y = "local"
foo()
print(y)

When we run the code, the output will be:

NameError: name 'y' is not defined

The output shows an error because we are trying to access a local variable y in the global scope, while local variables only work within foo() or the local scope.

Let's look at an example of how to create local variables in Python.

Example3:Creating local variables

Generally, we declare a variable inside a function to create a local variable.

def foo():
 y = "local"
 print(y)
foo()

When we run the code, it will output:

local

Let's take a look atprevious question,其中xis a global variable, and we want to modify it inside foo()x

Global and local variables

Here, we will show you how to use global and local variables in the same code.

Example4:Using global and local variables in the same code

x = "global"
def foo():
 global x
 y = "local"
 x = x * 2
 print(x)
 print(y)
 
foo()

When we run the code, the output will be:

global global 
local

In the above code, we declare x as a global variable and y as a local variable in foo(). Then, we use the multiplication operator*Modify the global variable x and output x and y at the same time.

After calling foo(), the value of x becomes global global because we use x * 2Print global twice. Then, we print the value of the local variable y, that is, the local variable.

Example5: Global and local variables with the same name

x = 5
def foo():
 x = 10
 print("local x:", x)
foo()
print("global x:", x)

When we run the code, the output will be:

local x: 10
global x: 5

In the code above, we used the same name x for both global variables and local variables. When we print the same variable, we get different results because the variable is declared in two scopes, that is, the local scope inside foo() and the global scope outside of foo().

When we print a variable inside foo(), it outputs local x: 10. This is called the local scope of the variable.

Similarly, when we print a variable outside of foo(), it outputs global x: 5. This is called the global scope of the variable.

Nonlocal variable

Nonlocal variables are used for nested functions that have undefined local scope. This means that the variable cannot be in the local scope or the global scope.

Let's see an example of how to create a global variable in Python.

We use the nonlocal keyword to create a nonlocal variable.

Example6: Create a nonlocal variable

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

When we run the code, the output will be:

inner: nonlocal
outer: nonlocal

In the code above, there is a nested function inner(). We use the nonlocal keyword to create a nonlocal variable. The inner() function is defined within the scope of another function external().

NoteIf we change the value of a nonlocal variable, these changes will appear in the local variable.