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)usesglobal X, so changes inXaffect the global variable.Initially,
X = 50andY = 20. Function is called asPlayWithValues(Y, X)→A = 20,B = 50.A += 5→A = 25(local change).X += 10→ globalX = 60.B += 5→B = 55(local copy, does not affect globalX).Outputs inside function:
Inside A: 25
Inside B: 55
Final outputs:
Global X: 60
Global Y: 20