So I am learning the difference between global and local vars in Python 2.7 and as per my understanding
local var is one that is defined within a function and globar var is one that is defined outside of the function.
I created this simple function to test how these local and global vars work when used in combination
def f():
global s
print s
s = "Python is great."
print s
Before I ran the function, I declared the global s
s = "I love python!"
f()
The output was:
>>> f()
I love python
Python is great
I understand till this part, but what I don't understand is, when I call the run a print s
outside the function, why is it printing the local variable instead of the global one. Does this mean that the global var s
is used once and discarded?
>>> print s
Python is great
can someone please explain this?