Last answered:

16 Feb 2023

Posted on:

19 Aug 2022

0

What does the is operator return after comparing?

Hi, this is not a question related to this topic but something I encountered when playing with python

a = 10
b = 10
d = 3
c = a/d
x = a/d

print(c, id(c))
print(c, id(c))
print( c is x)

Even though, both c and x variable references the same value and points towards the same memory location (c is x) statement will return False. But if I explicitly mention

c = 10/3
x = 10/3

It will return True. Why is this happening?

2 answers ( 0 marked as helpful)
Instructor
Posted on:

22 Aug 2022

1

Hey Nirmal,

Thank you for your question and experimenting with the Python language!

The variables c and x are not the same objects. Notice that your print functions print the id of object c twice and that is why you get the same id printed out. Typing

print(c is x)

would indeed return False, as c and x are not the same objects. However, the following

print(c == x)

would return True, as both objects store the same value.
image.png
Hope this helps! :)

Kind regards,
365 Hristina

Posted on:

16 Feb 2023

1

id function was new for me, thanks for the lovely information you deliver 👏🏼

Submit an answer