Python Namespace and Scope

1) Local Scope

Write a function that defines a variable inside it and prints it. Call the function.


2) Global Scope

Create a global variable and print it outside any function.


3) Access Global Inside Function

Create a global variable x = 10. Write a function that prints x without redefining it.


4) Local vs Global with Same Name

Define value = 5 globally. Inside a function, create a variable with same name value = 10 and print both inside and outside the function.


5) Modify Global Inside Function

Use the global keyword to modify a global variable inside a function. Print before and after.


6) Nested Function & Enclosing Scope

Define a function inside another function. Have the inner function print a variable from the outer function.


7) Enclosing Scope Modification

Use the nonlocal keyword to modify a variable in the enclosing function scope.


8) Function Parameters & Local Scope

Write a function with a parameter a. Print a inside and call the function.


9) UnboundLocalError

Create a function that tries to modify a global variable without global (should raise error). Observe the exception.


10) Builtin Namespace

Print a builtin function like len, then call it inside a function.


11) Shadowing Builtins

Define a variable named len and observe how it affects calling actual len().


12) Scope Test with Loop

Create a loop that defines a variable and print that variable after the loop.


13) Scope in List Comprehension

Use a list comprehension that references an outer variable; print the result and outer variable afterward.


14) Multiple Global Variables

Set 3 global variables outside any function. Inside a function, print them using global.


15) Read-Only Global

Have a function read a global value but not modify it. Verify global stays same.


16) Local Variable Persistence

Write a function that defines a local variable and call the function twice β€” observe that the local does not persist between calls.


17) Function Default Parameter Scope

Define a function with a default parameter referencing a global variable; change the global, then call the function.


18) Return vs Print

Write a function that defines a local variable and returns it. Print the result outside the function.


19) Variable Created Inside If Block

Create a variable inside an if block (not in function) and try printing it outside. Observe that it works.


20) Nested Globals & Locals

Inside a function, create a small inner function that tries to print both a global and outer local variable.


πŸ“ Notes & Concepts Practiced

Concept
What You Learn

Local scope

Variables inside functions

Global scope

Variables outside functions

global keyword

Modify outer variables

nonlocal keyword

Modify enclosing function variables

Shadowing

When a local hides a global/builtin

Builtins

Python’s default global namespace

Name resolution (LEGB)

Local β†’ Enclosing β†’ Global β†’ Builtin


πŸ“Œ Mini Code Hints

Global vs Local

Modify Global

Nonlocal Example


canvil: 333f6c7d-6876-4f7e-b6ad-1bdb2233f5c1

Last updated