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?
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.
Hope this helps! :)
Kind regards,
365 Hristina
id function was new for me, thanks for the lovely information you deliver 👏🏼