Programs · Program 7-15
Q.Program 7-15: Write a program to access any variable outside the function.
Odisha ChseTextbookSubjective· 2mImportance★★★★★est
43% · 17/40 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Program 7-15 is the flip side of 7-14: assigning to num inside the function creates a local variable that shadows the global, so the function prints 10 while the global outside remains 5.
The idea — assignment decides locality. Python decides a variable's scope at compile time: any name assigned anywhere in a function body is local to that function (unless declared global). So the num inside someFunc() is a different variable from the global num, despite the identical name.
# Program 7-15: assigning inside the function creates a LOCAL variable
def someFunc():
num = 10 # local num is created here
print("Inside the function, num =", num)
num = 5 # global num
someFunc()
print("Outside the function, num =", num)
Expected output
Inside the function, num = 10
Outside the function, num = 5
Reading the run
- The call executes
num = 10— that creates a localnum; the global is not consulted at all. - The print inside sees the local → 10.
- The local dies when the function returns; the global
numwas never touched → the outer print shows 5.
If we truly want the function to modify the global, we must say so explicitly:
def someFunc():
global num …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.