Scope of Variables¶
About This Lesson¶
Understand variable scope and namespaces in Python. Learn about local, global, and nonlocal scopes, and how Python resolves variable names.
What You'll Learn¶
- Local vs global scope
- The LEGB rule (Local, Enclosing, Global, Built-in)
- The global keyword
- The nonlocal keyword
- Namespace concepts
- Variable lifetime
Navigation Tips¶
- Use the MkDocs navigation on the left to move between lessons
- The notebook below is fully interactive - you can run and edit code
- Your changes are saved in your browser's local storage
Interactive Notebook¶
Having trouble with the embedded notebook? Try one of these: