Q.Write the output of the following Python code: def Update(X=10): X += 15 print('X = ', X) X=20 Update() print('X = ', X)
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 →Python function parameters create local scope: changes inside a function don't affect variables outside. The output is X = 25 then X = 20.
Why scope matters in Python
When you call a function with a parameter, Python creates a local variable inside that function's namespace. Any modifications to that variable stay confined to the function—they never reach back out to change variables in the outer (global) scope, even if they share the same name.
This is fundamentally about namespaces: the X inside Update and the X in the global scope are two distinct entities that happen to have the same identifier. The function parameter X shadows the global X for the duration of the function call.
Tracing the execution
Let's walk through what Python does, line by line:
-
Function definition
def Update(X=10): X += 15 print('X = ', X)This defines a function
Updatewith a default parameter value of10forX. The parameterXis local to this function. -
Global assignment
X = 20A global variable
Xis created and set to20. This lives in the module's global namespace. -
Function call
Update()Since no argument is passed, the default value kicks in: the local
Xstarts at10.Then
X += 15increments the localXto10 + 15 = 25.The
printstatement outputs:X = 25When the function returns, the local
Xis destroyed. -
Global print
print('X = ', X) ``` …
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.