The short answer is because lists are mutable and integers are immutable.
You cannot mutate an integer in place, so we call it 'immutable'. With this in mind, things like addition on an integer do not modify the original object, but rather return a new value- so your original variable will remain the same. Therefore, if we store a reference to an integer, they will only be the same object as long as we have not changed either one of them:
>>> foo = 1
>>> bar = foo
>>> foo is bar
True
>>> foo += 2
3
>>> foo
3
>>> bar
1
>>> foo is bar
False
On the other hand lists are 'mutable' (can modify that same object reference), and operations like pop()
mutate the list
in-place, changing the original. This also means that if you edit a reference to a mutable object such as a list
, the original will be changed as well:
>>> baz = [1, 2, 3, 4, 5]
>>> qux = baz
>>> qux is baz
True
>>> baz.pop()
5
>>> qux
[1, 2, 3, 4]
>>> baz
[1, 2, 3, 4]
>>> qux is baz
True