You need to learn to separate the notion of an "object" from a "name". Your first statement creates an integer object, and binds it to the name a
. Your second statement binds that same object to the name b
. Your third statement creates a NEW integer object with the value 3 and binds it to the name a
. The name b
is still bound to the old object.
The assignment operator always creates a new object binding. Compare this to modifying a list in place:
a = [1,2]
b = a
a[0] = 3
print(b)
produces [3,2], because we haven't changed the bindings. We have modified the single list object that both names are bound to.
I actually wrote an article about this, since it is such a key concept:
https://github.com/timrprobocom/documents/blob/main/UnderstandingPythonObjects.md