Can someone please explain why the for loop acts differently in these 2 situations? (if it operates on a copy of the variable, why the values change in scenario 1? if not, why the values don’t change in scenario 2?
Scenario 1:
class Person:
height = 100
people = []
for i in range(2):
people.append(Person())
for i in people:
print(i.height)
for i in people:
i.height += 20
for i in people:
print(i.height)
Results:
100
100
120
120
Scenario 2:
b = [1, 2, 3]
for i in b:
i += 5
print(b)
Results:
[1, 2, 3]
Shouldn't it act the same way? I am confused!