473 views
1 vote
Val = 25

def example():
  global val
  val = 15
  print (val)

print (val)
example()
print (val)



what is the output?

2 Answers

1 vote

Final answer:

The output of the code would be 25, 15, 15.

Step-by-step explanation:

The output of the code would be:

  1. 25
  2. 15
  3. 15

The initial value of val is 25, which is printed first. Then, the function example() is called where the global val is changed to 15 and printed. Finally, after the function call, the value of val is still 15 and is printed again.

User Pandasauce
by
3.9k points
6 votes

Answer:

25 15

15

Step-by-step explanation:

The first, print(val) will be printed, and here the value of val is 25 as set outside the function. Then the print(val) inside the example() will be printed, and here we have declared val as global and set its value to 15, And with last print as well, since the val is now global and with value 15, and hence the output will be 15. And hence the output is as above.

User Dajames
by
4.3k points