0

I am confused by the meaning of "=" in python, like codes below:

q=[1,2,3,4]
p=q
x=p.pop()
print p,q
p.append(x)
print p,q
p.insert(1,0)
print p,q
p.remove(4)
print p,q

It seems that each time I changes p, then q also changes as well! How can that happen?

Xin Zhang
  • 251
  • 1
  • 4
  • 11
  • [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html) – chepner Feb 13 '16 at 22:11
  • 2
    How on earth did you infer that this is this any different from C? In C, you would be passing around a pointer to the array. Changing the contents of `*p` would change the contents`*q`, because they reference the same block of memory. – Oka Feb 13 '16 at 22:14
  • 1
    Thanks for clarification – Xin Zhang Feb 13 '16 at 22:24

0 Answers0