Find the output of the following Python code: def PlayWithValues(A, B): global…

2026

Find the output of the following Python code:

def PlayWithValues(A, B):
global X
A += 5
X += 10
B += 5
print("Inside A:", A) # Output 1A
print("Inside B:", B) # Output 1B

X = 50
Y = 20
PlayWithValues(Y, X)

print("Global X:", X) # Output 1C
print("Global Y:", Y) # Output 1D

Show answer & explanation
  • Function PlayWithValues(A, B) uses global X, so changes in X affect the global variable.

  • Initially, X = 50 and Y = 20. Function is called as PlayWithValues(Y, X)A = 20, B = 50.

  • A += 5A = 25 (local change).

  • X += 10 → global X = 60.

  • B += 5B = 55 (local copy, does not affect global X).

  • Outputs inside function:

    • Inside A: 25

    • Inside B: 55

  • Final outputs:

    • Global X: 60

    • Global Y: 20

Explore the full course: Hpsc Pgt Computer Science