Python Scope of Variables

Last Updated 13 Sep, 2026

The scope of a variable determines where that variable can be accessed in a Python program. A variable created inside a function is generally available only inside that function, while a variable created outside functions can usually be accessed from different parts of the program.

Python uses a specific lookup order called the LEGB rule to find variables.

Local Scope

A variable created inside a function has local scope. It can be accessed only within that function.

def greet():
    message = "Hello"
    print(message)

greet()

Output:

Hello

Here, message is a local variable because it is created inside greet().

Trying to access it outside the function causes an error:

def greet():
    message = "Hello"

greet()
print(message)

Python raises:

NameError: name 'message' is not defined

Each function can also have its own local variables:

def first():
    value = 10
    print(value)

def second():
    value = 20
    print(value)

first()
second()

Output:

10
20

Although both variables have the same name, they belong to different local scopes.

Global Scope

A variable created outside all functions has global scope. It can be accessed from different parts of the program.

name = "Abhishek"

def greet():
   print("Hello", name)

greet()
print(name)

Output:

Hello Abhishek
Abhishek

Here, name is a global variable because it is defined outside the function.

A function can read a global variable without using the global keyword:

number = 10

def display():
    print(number)

display()

Output:

10

However, assigning a new value to a global variable from inside a function requires special handling.

global Keyword

The global keyword tells Python that a variable inside a function refers to a variable defined in the global scope.

For example:

count = 10

def update():
    global count
    count = 20

update()

print(count)

Output:

20

Without global, Python treats count as a local variable when you assign a value to it inside the function.

count = 10

def update():
    count = 20

update()

print(count)

Output:

10

Here, the count inside update() is a different local variable. The global count remains 10.

The global keyword is therefore useful when a function needs to modify a variable that belongs to the global scope.

LEGB Rule

When Python encounters a variable name, it searches for that name according to the LEGB rule:

L → Local

E → Enclosing

G → Global

B → Built-in

Python searches these scopes in this order.

Local

Python first checks the current function's local scope.

x = 10

def test():
    x = 20
    print(x)

test()

Output:

20

Python finds x in the local scope before checking the global scope.

Enclosing

The enclosing scope occurs when one function is defined inside another function.

x = "Global"

def outer():
    x = "Enclosing"

    def inner():
        print(x)

    inner()

outer()

Output:

Enclosing

inner() does not have its own x, so Python looks in the enclosing function outer() and finds it there.

Global

If Python cannot find a variable in the local or enclosing scope, it checks the global scope.

x = "Global"

def show():
    print(x)

show()

Output:

Global

There is no local x, so Python finds the variable in the global scope.

Built-in

Finally, Python checks the built-in scope, which contains names provided by Python itself, such as len(), print(), type(), and sum().

numbers = [10, 20, 30]

print(len(numbers))

Output:

3

Here, len is found in Python's built-in scope.

So, when Python looks for a variable or name, the general search order is:

Local → Enclosing → Global → Built-in

If the name is not found in any of these scopes, Python raises a NameError.