Skip to content
Question 87 of 97

Q.Write the output of the following Python code: def Update(X=10): X += 15 print('X = ', X) X=20 Update() print('X = ', X)

Yanam CbseCBSE Class XII Board 2020Subjective· 1mImportance★★★★★
90% · 87/97 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 →

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:

  1. Function definition

    def Update(X=10):
        X += 15
        print('X = ', X)
    

    This defines a function Update with a default parameter value of 10 for X. The parameter X is local to this function.

  2. Global assignment

    X = 20
    

    A global variable X is created and set to 20. This lives in the module's global namespace.

  3. Function call

    Update()
    

    Since no argument is passed, the default value kicks in: the local X starts at 10.

    Then X += 15 increments the local X to 10 + 15 = 25.

    The print statement outputs:

    X =  25
    

    When the function returns, the local X is destroyed.

  4. 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.