Editor

Predict the Output: Variable Scope

Intermediate · Predict the output · from 15 Python Pitfalls That Trip Up Even Experienced Developers

From the lesson: 15 Python Pitfalls That Trip Up Even Experienced Developers

Python is famous for being readable and beginner-friendly. But lurking beneath that clean syntax are several traps that catch even experienced developers. These aren't bugs in Python - they're features that behave differently than you'd expect.

Read the lesson

What does this code print? Pay attention to how Python resolves variable names.

x = "global"

def outer():
    x = "outer"
    def inner():
        print(x)
    inner()

outer()
print(x)
x = "global"
def outer():
x = "outer"
def inner():
print(x)
inner()
outer()
print(x)

More Common Pitfalls exercises

All Python exercises · 50 practice problems · Advanced Python quiz