a = 0
def f():
global a
a = 1
print(a)
print(a, f(), a)
The output of the above code turns out to be the following:
1
0 None 1
Why is the function f called before printing the first argument a? Why is the value of the first argument 0, even after the function call?
What you get as output is:
1
0 None 1
When you call print() it computes the elements to be printed first. As you have print(a) inside your f() what you get first is 1. Then, it starts printing. The value of a is 0 before you call the function. When you print a function that doesn't return anything, you get None. As you change the value globally, you get 1 at the end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With