0

why is the answer false false ?

a = [1,[2,3]] b= [1,2,3] c=a[:] d = b[:] print(c is a , d is b )

15th Oct 2020, 2:09 PM
Curious Ant
Curious Ant - avatar
5 Respostas
+ 5
[:] makes a copy of a list. The new list will be equal - like a twin. But it's not the original same list. Therefore 'is' gives False. If you write c==a, you get True. For more details, read this: https://code.sololearn.com/c89ejW97QsTN/?ref=app
15th Oct 2020, 2:11 PM
HonFu
HonFu - avatar
+ 3
https://code.sololearn.com/cUVsE6fxgkG4/?ref=app
15th Oct 2020, 2:14 PM
Slick
Slick - avatar
+ 3
Slick, the fun begins when you add this line to your code: print(a[1] is b[1])
15th Oct 2020, 2:20 PM
HonFu
HonFu - avatar
+ 2
HonFu i wonder why that is, they also have the same id! The madness
15th Oct 2020, 2:23 PM
Slick
Slick - avatar
+ 1
Slick, you will find the reason in my tutorial. 😉
15th Oct 2020, 2:40 PM
HonFu
HonFu - avatar