Global vs Local
Multiple Choice Medium
Question
How does Python determine variable scope?
Hint
LEGB is the scope resolution order.
Explanation
Python uses the LEGB rule for name resolution: Local (inside function), Enclosing (outer function), Global (module level), Built-in (Python builtins). Use global x to modify a global inside a function, nonlocal x for enclosing scope variables. Without these, assignment creates a local variable. Reading accesses the LEGB chain.
Related Resources
Related Lessons
- Variables
Learn Python variables.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.