Can please someone enlight me why is the output of the following code is 0? a= [2,3] b=[2,6] a[1]*=3 b[1]+=3 print(int(a is b)) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can please someone enlight me why is the output of the following code is 0? a= [2,3] b=[2,6] a[1]*=3 b[1]+=3 print(int(a is b))

4th Dec 2019, 12:05 PM
Shamil Erkenov
Shamil Erkenov - avatar
5 Answers
+ 7
The reason why there are different objects means they are the same type of object (list), but each individual object in python does have an individual object id(). In our case a and b get assigned with different values, so they have individual object id's. a= [2,3] b=[2,6] print(id(a)) # a -> 98446604 print(id(b)) # b ->124334828 a[1]*=3 b[1]+=3 # result for object id: these numbers can be total different on your device.
4th Dec 2019, 3:14 PM
Lothar
Lothar - avatar
+ 9
Code is a little bit weired' but anyway: a= [2,3] b=[2,6] a[1]*=3 b[1]+=3 print(int(a is b)) Line 1 > 2,3 is assigned to a Line 2 > 2,6 is assigned to b Line 3 > a[1] which is 3 will be multiplied by 3 > is now 9 Line 4 > b[1] which is 6 will be added with 3 > is now 9 Line 5 'a is b' returns to False, as a and be are different objects with different id() Line 5 > int(False) > is now 0, because False == 0
4th Dec 2019, 12:45 PM
Lothar
Lothar - avatar
+ 2
Their are different objects because one "9" was made by multiplication and another was made by addition? 🤔
4th Dec 2019, 12:59 PM
Shamil Erkenov
Shamil Erkenov - avatar
+ 1
Шамиль Эркенов "Is" is not " ==". "Is" just as both variables point to the same memory location.
4th Dec 2019, 2:15 PM
Petr
+ 1
Lothar thank you, man 💪
4th Dec 2019, 3:20 PM
Shamil Erkenov
Shamil Erkenov - avatar