+ 3

A= [0, 1, 2] b=a a[0]="spam" print (b[0 ])

I thought the answer should be 0, but under py challenge, the answer is said to be spam. My thought A= [0, 1, 2] has been assigned before a[0] was updated to "spam"

2nd Sep 2020, 7:24 AM
Gbadegesin Muhammed
Gbadegesin Muhammed - avatar
2 Réponses
+ 2
First ,list is mutable(means it is changeable) a=[0,1,2] b=a a[0]="spam" print(b[0]) So, 0 is changed to "spam". As a result, the answer is "spam".
2nd Sep 2020, 7:26 AM
Web-Learner
+ 2
Web-Learner i know list is mutable, my question is that since b has been updated before a[0] was changed, I thought b will maintain the old version of a except it is explicitly updated again
2nd Sep 2020, 7:33 AM
Gbadegesin Muhammed
Gbadegesin Muhammed - avatar