Why would the following two examples have different results? I thought slicing a list would result in a (shallow) copy of the list elements, so a should not be changed in both cases.
>>> a = [1, 2, 3, 4, 5]
>>> a[3: 5] = [0, 0] # example 1
>>> a
[1, 2, 3, 0, 0] # elements in the original list are changed
>>> b = a[3: 5] # example 2
>>> b = [100, 100]
>>> a # elements in the original list are unchanged
[1, 2, 3, 0, 0]
bis its own list - not the same asa. useid(a)andid(b)to print its ids - you see they differ.id(a[3: 5])would also be different than id(a), i.e.id(a),id(b)andid(a[3:5])are all unique, but why doesa[3:5]directly alterawhilebdoesn't?a[3:5] = [....]does altera-id(a[3:5])is a new shallow copied lista[3:5] =<-- emphasis on the=sign--there's an assignment happening to that slice. Buta[3:5]where no equals sign is involved is not going to modifya.