Get The Most Affordable Hosting in the World!

Starting at just $1.87/month, Vercaa offers unbeatable pricing for world-class web hosting services.

Fast, reliable, and secure hosting to power your website without breaking the bank. Plus, enjoy a free CDN for faster loading times worldwide!

Get Started Now!

Python has two identity operators is and is not. Both return opposite Boolean values. The "in" operator evaluates to True if both the operand objects share the same memory location. The memory location of the object can be obtained by the "id()" function. If the id() of both variables is same, the "in" operator returns True (as a consequence, is not returns False).

 
a="TutorialsPoint" b=a print ("id(a), id(b):", id(a), id(b)) print ("a is b:", a is b) print ("b is not a:", b is not a)

It will produce the following output −

id(a), id(b): 2739311598832 2739311598832
a is b: True
b is not a: False

The list and tuple objects differently, which might look strange in the first instance. In the following example, two lists "a" and "b" contain same items. But their id() differs.

 
a=[1,2,3] b=[1,2,3] print ("id(a), id(b):", id(a), id(b)) print ("a is b:", a is b) print ("b is not a:", b is not a)

It will produce the following output −

id(a), id(b): 1552612704640 1552567805568
a is b: False
b is not a: True

The list or tuple contains the memory locations of individual items only and not the items itself. Hence "a" contains the addresses of 10,20 and 30 integer objects in a certain location which may be different from that of "b".

print (id(a[0]), id(a[1]), id(a[2])) print (id(b[0]), id(b[1]), id(b[2]))

It will produce the following output −

140734682034984 140734682035016 140734682035048
140734682034984 140734682035016 140734682035048

Because of two different locations of "a" and "b", the "is" operator returns False even if the two lists contain same numbers.

 

The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

 

War diese Antwort hilfreich? 1 Benutzer fanden dies hilfreich (1 Stimmen)