Back to Study Material Python

Global and Local Variables

A variable is local to a function if it is assigned anywhere inside that function, even if a global with the same name exists. Local variables exist only w...

Global and Local Variables

Local Variables

A variable is local to a function if it is assigned anywhere inside that function, even if a global with the same name exists. Local variables exist only while the function is executing; they are created on the call stack when the function is called and destroyed when it returns. This means each function call gets its own fresh copy of its local variables. Local scope keeps functions self-contained and prevents unintended interference between parts of a program.

Global Variables

A variable defined at the top level of a module (outside any function or class) is a global variable. It exists for the lifetime of the module. Any code in that module can read it. However, to modify a global variable inside a function, you must explicitly declare it with the global keyword: def increment(): global counter; counter += 1. Without the declaration, counter += 1 would attempt to read an undefined local variable and raise UnboundLocalError.

The LEGB Rule

When Python encounters a name, it searches four scopes in order: Local (inside the current function), Enclosing (any enclosing functions, for closures), Global (the module level), Built-in (Python's built-in namespace). The first match wins. This is the LEGB rule. Understanding it explains why a local variable shadows a global with the same name, and why built-in names like len or print can be (accidentally) overridden by creating a variable with the same name.

The nonlocal Keyword

nonlocal is used inside nested functions to refer to a variable in the immediately enclosing function's scope (not the global scope). Example: def outer(): x = 10; def inner(): nonlocal x; x += 1; inner(); print(x). Without nonlocal, x += 1 inside inner would create a local variable in inner and the outer x would not change. nonlocal is essential for implementing closures that maintain and modify state across multiple calls.

Worked Example: Counter Closure

def make_counter(): count = 0; def increment(): nonlocal count; count += 1; return count; return increment. Each call to make_counter() creates a new, independent counter. c1 = make_counter(); c1() returns 1; c1() returns 2; c2 = make_counter(); c2() returns 1. The count variable lives in the enclosing scope and persists between calls to increment because the inner function holds a reference to it, forming a closure.

Exam Tip

The most tested scenario: def f(): print(x); x = 5; f() where x = 10 is global. This raises UnboundLocalError, not printing 10. Why? Because Python sees the assignment x = 5 inside f and marks x as local throughout the entire function — including the print before the assignment. The fix is either global x or moving the assignment before the print. This "local variable referenced before assignment" error is one of the most common beginner confusions in Python exams.