Skip to content
Programs · Program 7-15

Q.Program 7-15: Write a program to access any variable outside the function.

Tamil Nadu DgeTextbookSubjective· 2mImportance★★★★★est
43% · 17/40 Questions
🔒 Locked · start free trial →

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

  1. The call executes num = 10 — that creates a local num; the global is not consulted at all.
  2. The print inside sees the local → 10.
  3. The local dies when the function returns; the global num was 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.