1

So I came across some strange behaviour in python :

>>> 2+1 is 3
True
>>> 2000+1 is 2001
False

It doesn't use the correct logic when large integers are used, why is this?

tombam95
  • 343
  • 2
  • 14

1 Answers1

2

is will return True if two variables point to the same object. So that there id.

In [21]: id(3)
Out[21]: 15538056

In [22]: id(2+1)
Out[22]: 15538056

In [23]: id(2001), id(2000+1)
Out[23]: (52399576, 54526360)
Vishnu Upadhyay
  • 5,043
  • 1
  • 13
  • 24