Q. What will be the output of following programs?


(i)

num = 1

def myfunc ():

    return num

print(num)

print(myfunc())

print(num)


(ii)

num = 1

def myfunc():

    num = 10

    return num

print (num)

print(myfunc())

print(num)


(iii)

num = 1

def myfunc ():

    global num

    num = 10

    return num

print (num)

print(myfunc())

print(num)


(iv)

def display():

    print("Hello", end = ' ')

display()

print("there!")



Answer =

(i)
Output:-

1
1
1
>>>

(ii)
Output:-

1
10
1

(iii)
Output:-

1
10
10

(iv)
Output:-

Hello there!

20 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. Is the 2nd ans because of local scope variable ?

    ReplyDelete
  2. i) is wrong
    f should not be capitalised
    output:
    1
    1
    1

    ReplyDelete
  3. can someone explain the 3rd one?
    global num means it should be using 1 right? cause 1 is in global scope and 10 is in local scope? I don't understand why we are using local variable? Can someone explain

    ReplyDelete
    Replies
    1. global num
      Change value of num in whole program.

      Delete
  4. Can u explain i)
    as it should be 1
    None
    1

    ReplyDelete
    Replies
    1. Please copy it and run it in your python software.

      Delete
  5. I've never seen such an interactive and responsive website author..

    ReplyDelete
  6. can u explain 4

    ReplyDelete
  7. the first answer should be 1 10 1 because you have called the func in statement 2 nd i have tried it on editor

    ReplyDelete
  8. In 1) it should be
    1
    None
    1

    ReplyDelete
    Replies
    1. You are wrong because myfunc is returning num.

      Delete
  9. I dont understand

    ReplyDelete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post