Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
ICSM
05-Oct-2025The 4 Types of Scope (LEGB Rule)
Python follows the LEGB rule, which stands for:
len,print,sum) — always available.localx = 5inside a functionenclosingglobalglobal xbuilt-inprint(),len()Example: Understanding Scope
Output:
Modifying Variables Across Scopes
If you want to modify a variable from an outer scope, use:
global→ to modify a global variable.nonlocal→ to modify an enclosing variable in a nested function.Example:
Output: