Untitled
unknown
python
2 years ago
1.2 kB
24
Indexable
x = 10 # Global variable (immutable)
def change():
x = 20 # This is a new local variable, not changing the global 'x'
print(x)
change() # prints: 20
print(x) # prints: 10 (the global variable 'x' remains unchanged)
----- END OF FILE -----
x = 10 # Global variable
def change():
global x # This is the global 'x'
x = 20 # Modifying the global 'x'
print(x)
change() # prints: 20
print(x) # prints: 20 (the global variable 'x' has been changed)
----- END OF FILE -----
x = [1, 2, 3] # Global variable (mutable)
def change():
x.append(4) # This modifies the global variable 'x'
print(x)
change() # prints: [1, 2, 3, 4]
print(x) # prints: [1, 2, 3, 4] (the global variable 'x' has been changed)
def reassign():
x = [4, 5, 6] # This is a new local variable, not changing the global 'x'
print(x)
reassign() # prints: [4, 5, 6]
print(x) # prints: [1, 2, 3, 4] (the global variable 'x' remains unchanged)
----- END OF FILE -----
x = [1, 2, 3] # Global variable
def change():
global x # This is the global 'x'
x = [4, 5, 6] # Reassigning the global 'x'
print(x)
change() # prints: [4, 5, 6]
print(x) # prints: [4, 5, 6] (the global variable 'x' has been changed)
Editor is loading...