Parameters In Depth

In [3]:
num = 10  # num is a global variable
print(num)

def square():
    # num is in a "global scope"
    # I can access within any function defined
    msg = "We're done!"
    for i in range(num):
        print(i, i**2)
    print(msg)
    
    # 'msg' and 'i' are local variables inside 'square'


square()
print(msg, i)  # These variables are out of scope
10
0 0
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
We're done!
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-8e7fd0422d43> in <module>()
     14 
     15 square()
---> 16 print(msg, i)  # These variables are out of scope

NameError: name 'msg' is not defined