How does Python determine whether a variable is local or global?
Python follows the LEGB rule (Local, Enclosing, Global, Built-in).
When you reference a variable, Python checks in this order:
-
Local – variables inside the current function
-
Enclosing – variables in outer functions (nested functions)
-
Global – variables at the top level of the script
-
Built-in – Python’s built-in names
This resolution process is essential for beginners who want to Learn Python Online, as understanding LEGB helps you write cleaner, bug-free code. The first match determines the scope.
-
What is the purpose of virtual environments in Python?
2 weeks ago
-
What is the difference between append() and extend()?
3 weeks ago
-
What is the difference between np.zeros(), np.ones(), and np.empty()?
3 weeks ago
-
What are Python’s data structures?
1 month ago
-
Is Python still relevant for jobs in 2025?
1 month ago
Recently viewed by users: kerina 1 hour ago.
Latest Post: What salary can a TOSCA Automation Engineer expect? Our newest member: williamcooper Recent Posts Unread Posts Tags
Forum Icons: Forum contains no unread posts Forum contains unread posts
Topic Icons: Not Replied Replied Active Hot Sticky Unapproved Solved Private Closed