The doubt of python mutable object

recently I encountered some confusion when I was learning Python . I don"t know how to understand it for a while, as follows:

use version python3.6

suppose you have the following three pieces of code:

l = [1]
print(id(l))
l = l + [2]
print(id(l))

the above code finds that the address has changed

my understanding is limited to knowing that the list is a mutable object, but the above three pieces of code do not know how to explain the principle. I still feel that there are still some places that do not understand clearly.

I would like to have an answer from my seniors. I would appreciate it

Oct.20,2021
The internal principles of the three ways of

are different. + = and append operate in place, that is, adding elements directly to the list, while the list is a reference type, just an address, so his id () is the same. Finally, the two lists are combined into a new list to return, and the new list must point to a different address, so id has changed

Menu