0
how come the output is 12, I still don't really understand how y[0] value was also changed
y=[5] z=y z[0]+=1 print(y[0]+z[0]) #prints: 12
2 Answers
+ 3
When you say z=y you are not creating a new copy of the list [5]. You are only creating a second name (z) that refers to the same list object as the first name (y). So y[0] and z[0] refer to the same thing, not only the same number originally but the same memory location. If you change z[0] you also change y[0] because they are two references to the same object.
+ 1
Jay Matthews but how y[0] increments too?