Editor

Predict the Scope Output

Beginner · Predict the output · from Python Variable Scope, Closures, and the LEGB Rule

From the lesson: Python Variable Scope, Closures, and the LEGB Rule

Imagine a house with several rooms. Each room has its own set of objects - a lamp on the bedroom nightstand, a knife in the kitchen drawer. You can use items in the room you're standing in, and you can walk to the hallway to grab something shared.

Read the lesson

Read this code carefully and predict what it prints. Then write print() statements that output the exact result.

x = 10

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

foo()
print(x)

Print the two numbers on separate lines, exactly as the code above would output them.

# Print the exact output of the code above

More Scope and Closures exercises

All Python exercises · 50 practice problems · Functions quiz