+ 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))
5 odpowiedzi
+ 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.
+ 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
+ 2
Their are different objects because one "9" was made by multiplication and another was made by addition? 🤔
+ 1
Шамиль Эркенов
"Is" is not " ==". "Is" just as both variables point to the same memory location.
+ 1
Lothar thank you, man 💪