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?
Thank you for your question and experimenting with the Python language!
x are not the same objects. Notice that your
c twice and that is why you get the same id printed out. Typing
print(c is x)
would indeed return
x are not the same objects. However, the following
print(c == x)
True, as both objects store the same value.
Hope this helps! :)
id function was new for me, thanks for the lovely information you deliver 👏🏼