While learning python, I have an understanding that python runs Line by line and unless a line of code is not executed it does not create or assign variable and all was fine with this theory, until I tried something like:
X = 1
def method1():
print (X) # Why global X, is not printed here
X = 20
print (X) # and then use local X here
I know global keyword can solve it also if I remove the assignment of X inside method1() would solve it and global X gets printed, but I am not able to understand how python knows before hand that I have a var assignment of same name somewhere down the code in function?
Any help would be appriciated.