Skip to the workspace
Pebellslearn by building

Python syntax & reference

Find the form you need, compare how it behaves, and open the complete explanation when you need more depth.

Python 3.10+ syntax; individual entries identify newer features. Browser exercises use the configured Python runner.

1 matching syntax entry

Name lookup and closures

Learn in depth →

Functions resolve names through local, enclosing, global and built-in scopes. Closures retain bindings.

nonlocal name; global name

Example

def make_counter():
    count = 0
    def increment():
        nonlocal count
        count += 1
        return count
    return increment
c = make_counter()
print(c(), c())

Result

1 2

Watch for: Assignment makes a name local unless declared otherwise. Late-bound closures observe a binding at call time.

Practise this topic →
Complete explanation index (91 lessons indexed)

Variable Scope & the LEGB Rule

Technical sources and review date

Original Pebells examples. Reviewed 2026-10-28.

Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.