Python LEGB Rule: Variable Scope Explained

Understanding Variable Scope in Python (LEGB Rule) Explored how Python resolves variable names using the LEGB hierarchy: Local (L): Variables defined inside a function Enclosing (E): Variables in the scope of enclosing (nested) functions Global (G): Variables declared at the module level Built-in (B): Python’s built-in identifiers like len(), type(), print() Practiced how nested functions access enclosing variables, why outer scopes cannot access inner variables, and how the nonlocal keyword enables modification of enclosing-scope variables. Also worked with the global keyword to understand how global variables can be accessed and modified inside functions and why it should be used carefully. #Python #ProgrammingFundamentals #LEGB #SoftwareEngineering #LearningInPublic #DataAnalytics

To view or add a comment, sign in

Explore content categories