🚀 Hurry! Offer Ends In
00 Days
00 Hours
00 Mins
00 Secs
Enroll Now
X

निम्नलिखित पायथन कोड का आउटपुट क्या होगा?

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:

  1. x = 50: This sets the global variable x to 50.

  2. Function Call: func(x):

    • The function is called with the global x (which is 50).
    • Inside the function:
      • The first print('x is', x) prints the value of the parameter x (which is 50).
      • The local x is changed to 2 (this does not affect the global x).
      • The second print('Changed local x to', x) prints the new value of the local x (which is 2).
  3. print('x is now', x):

    • After the function call, it prints the global x, which remains unchanged at 50, because the local change to x inside the function does not affect the global variable.

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

Latest Updates