← Back to Python Course | Chapter 4: Functions | Lesson 8 of 9

Python Scope & Lifetime

Scope is about where a name can be seen, like a secret whispered in one room that people in other rooms cannot hear. Names made inside a function usually stay inside it.
Syntax
python
global_variable = value

def function_name():
    local_variable = value
    global global_variable

Local Scope

A variable created inside a function exists only in that function's local scope — it's created when the function starts running and destroyed when it returns, invisible to any code outside that function.

Example: Local Scope

python
def greet():
    message = "Hello"  # local variable, only exists inside greet()
    print(message)

greet()

Global Scope

Variables assigned at the top level of a script, outside any function, live in the global scope and can be *read* from inside any function without any special syntax — only *reassigning* them requires extra steps.

Example: Global Scope

python
count = 10  # global variable, defined outside any function

def show():
    print(count)  # can read the global variable without any special syntax

show()

The global Keyword

Attempting to assign to a global variable's name inside a function normally creates a new local variable instead of touching the outer one; declaring it with the global keyword first tells Python you intend to modify the actual outer variable.

Example: The global Keyword

python
count = 0

def increment():
    global count  # declares intent to modify the outer count, not create a local one
    count += 1

increment()
print(count)

Nonlocal Scope

In a function nested inside another function, nonlocal lets the inner function modify a variable belonging to the enclosing (but not global) function's scope — distinct from global, which only reaches all the way out to module level.

Example: Nonlocal Scope

python
def outer():
    value = 1
    def inner():
        nonlocal value  # refers to outer's value, not the global scope
        value += 1
    inner()
    print(value)

outer()

Built-in Scope

Built-in names like print(), len(), and int() live in Python's built-in scope, the outermost layer checked last, which is why you can call them from literally anywhere without importing or defining them yourself.

Example: Built-in Scope

python
print(len("hello"))
print(int("42"))
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Assigning to a global variable inside a function without global, which creates a new local variable instead.
  2. Reading a global and then assigning to it in the same function, which raises UnboundLocalError.
  3. Trying to use a variable defined inside a function from outside it, which raises NameError.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.