Variable Scope
x = 2
def printx():
global x
x = 1
print(x)
printx()
print(x)
1
1
Because the global keyword was used first, the assignment inside the function used the global x and thus changed its value.
x = 2
def printx():
global x
x = 1
print(x)
printx()
print(x)
1
1
Because the global keyword was used first, the assignment inside the function used the global x and thus changed its value.