I'm currently learning Python, and couldn't get my mind around this variable position "paradox" (which exists only to the level of my "beginners knowledge" in Python).
Example 1
#creating a function which returns the elements of a list called i.
def var_i1():
i = [1,2,3,4]
for elt in i:
print(elt)
var_i1()
#This successfully prints :
1
2
3
4
Example 2
#creating a function which returns the elements of a list called i. i is outside of the body var_i2()
i = [1,2,3,4]
def var_i2():
for elt in i:
print(elt)
var_i2()
#allthough i is outside of the function body (correct me if I'm wrong) this also successfully prints :
1
2
3
4
Example 3
i = 4
def var_i3():
if i >0:
i += 1
print(i)
var_i3()
# UnboundLocalError: local variable 'i' referenced before assignment
#I don't understand why Python is okay with variable i as a list but not variable i as an integer