L[0]
is a name, and when you create the list L
, you assign an object to that name, the integer 1. a
is also a name, and when you assign a as in a = L[0]
, you make a
to point to the same object that L[0]
points to.
But when you later do a = a + 1
, this is another assignment. You are not modifying the object that a
points to -- the =
sign can't do that. You are creating a new object, the integer 2, and assigning that to a
.
So in the end, you have two objects in memory; one is referred to by L[0]
and the other is referred to by a
.
Integers are immutable, which means that there is no possible way to change the properties of the objects in this example; however, that's not salient in this example exactly, because even if the object was mutable it wouldn't change the fact that you're doing assignment (with the =
sign). In a case where the object in question was mutable, you could theoretically change the properties of the object when it is still referenced by L[0]
and a
, instead of doing any additional assignment with =
as you are doing. At that point, you would see the properties change regardless of which name you used to inspect the object.