निम्नलिखित पायथन कोड का आउटपुट क्या होगा?
What will be the output of the following Python code ?
x = 50
def func(x):
print('x is', x)
x = 2
print('Changed local x to', x)
func(x)
print('x is now', x)
A)
B)
C)
D)
Explanation:
Let's break down the code step by step:
x = 50 # Global variable x
def func(x):
print('x is', x) # Prints the value of x passed to the function
x = 2 # Local variable x is changed inside the function
print('Changed local x to', x) # Prints the new value of the local x
func(x) # Calls the function with the global x (which is 50)
print('x is now', x) # Prints the global x
Explanation:
-
x = 50
: This sets the global variablex
to50
. -
Function Call:
func(x)
:- The function is called with the global
x
(which is50
). - Inside the function:
- The first
print('x is', x)
prints the value of the parameterx
(which is50
). - The local
x
is changed to2
(this does not affect the globalx
). - The second
print('Changed local x to', x)
prints the new value of the localx
(which is2
).
- The first
- The function is called with the global
-
print('x is now', x)
:- After the function call, it prints the global
x
, which remains unchanged at50
, because the local change tox
inside the function does not affect the global variable.
- After the function call, it prints the global
Final Output:
x is 50
Changed local x to 2
x is now 50
Answer:
(A) x is 50 Changed local x to 2 x is now 50