80

Is it better to use the "is" operator or the "==" operator to compare two numbers in Python?

Examples:

>>> a = 1
>>> a is 1
True
>>> a == 1
True
>>> a is 0
False
>>> a == 0
False
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
argentpepper
  • 4,202
  • 3
  • 33
  • 45
  • http://stackoverflow.com/questions/1504717/python-vs-is-comparing-strings-is-fails-sometimes-why/1504742#1504742 – SilentGhost Feb 10 '10 at 19:38
  • http://stackoverflow.com/questions/2209755/python-operation-vs-is-not – Thomas Wouters Feb 10 '10 at 19:42
  • 1
    Python 3.8 (still in development at the time of this comment) introduces a `SyntaxWarning` for using `is` or `is not` with a literal: https://bugs.python.org/issue34850 – argentpepper Feb 05 '19 at 16:29
  • This question specifies **number**, but answers from 2008 (preceding this question), in the duplicate, already cover the question in relation to numbers, so the question is a duplicate. Also **Is it better to use** is asking for an opinion. – Trenton McKinney Oct 30 '21 at 13:38

6 Answers6

119

Use ==.

Sometimes, on some python implementations, by coincidence, integers from -5 to 256 will work with is (in CPython implementations for instance). But don't rely on this or use it in real programs.

crizCraig
  • 8,487
  • 6
  • 54
  • 53
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 8
    Classes that define `__int__` won't work properly with `==` either; they'd need to define `__eq__` or `__cmp__` :) – Thomas Wouters Feb 10 '10 at 19:43
  • 2
    The reason for this being that Python automatically creates those integers prior to runtime rather than constructing them on the fly in order to save time, and thus these particular integers have ids before being needed in the program. – Ben Mordecai Jan 11 '13 at 19:56
  • 2
    the `is` operator *works* with integers outside that range, but they just have different identities (as an implementation detail); also I thought the range was from `-5` not `-1` – Chris_Rands Jun 28 '17 at 13:23
  • 1
    @Chris_Rands: I believe the range may have changed at least once, but I'm not certain about that. – Ignacio Vazquez-Abrams Jun 28 '17 at 13:24
  • @IgnacioVazquez-Abrams You're probably right, as it's not guaranteed, might be work an edit to expand a bit :) – Chris_Rands Jun 28 '17 at 13:28
33

Others have answered your question, but I'll go into a little bit more detail:

Python's is compares identity - it asks the question "is this one thing actually the same object as this other thing" (similar to == in Java). So, there are some times when using is makes sense - the most common one being checking for None. Eg, foo is None. But, in general, it isn't what you want.

==, on the other hand, asks the question "is this one thing logically equivalent to this other thing". For example:

>>> [1, 2, 3] == [1, 2, 3]
True
>>> [1, 2, 3] is [1, 2, 3]
False

And this is true because classes can define the method they use to test for equality:

>>> class AlwaysEqual(object):
...     def __eq__(self, other):
...         return True
...
>>> always_equal = AlwaysEqual()
>>> always_equal == 42
True
>>> always_equal == None
True

But they cannot define the method used for testing identity (ie, they can't override is).

user2357112
  • 260,549
  • 28
  • 431
  • 505
David Wolever
  • 148,955
  • 89
  • 346
  • 502
  • `is` is not exactly like Javascript's `===`, or at least not in relation to the question. For example, for me `2 ** 12 is 2 ** 12` is `False` (this is implementation dependent), but in Javascript `Math.pow(2, 12) === Math.pow(2, 12)` is `true`. – Paul Draper Nov 11 '13 at 04:45
20
>>> a = 255556
>>> a == 255556
True
>>> a is 255556
False

I think that should answer it ;-)

The reason is that some often-used objects, such as the booleans True and False, all 1-letter strings and short numbers are allocated once by the interpreter, and each variable containing that object refers to it. Other numbers and larger strings are allocated on demand. The 255556 for instance is allocated three times, every time a different object is created. And therefore, according to is, they are not the same.

Wim
  • 11,091
  • 41
  • 58
  • 2
    The only safe use of `is` for comparisons is for the `None` object. And I suppose the `...` object. – Chris Lutz Feb 10 '10 at 19:40
  • `is` will work for all strings, not only 1-letter strings. This is referred to as string interning in the Python documentation. – sttwister Feb 10 '10 at 19:41
  • 3
    @ujukatzel - Wrong. When I run `a = "this is one hell of a string"; b = "this is one hell of a string"; a is b` I get `False` as the result. Python (specifically CPython) only interns some small strings. – Chris Lutz Feb 10 '10 at 19:44
  • 2
    @Chris Lutz - Although the example you posted does return `True` to me, indeed you're right, it does fail for some bigger strings. – sttwister Feb 10 '10 at 19:47
  • It's worth pointing out that (in CPython 1.5-3.7, at least), `255556 is 255556` _is_ true. That's because the compiler folds certain immutable constant values in the same compilation unit into a single value (as explained [on the other question](https://stackoverflow.com/a/49472348/908494)). But your conclusion is the important part: Python is allowed to merge any two provably-immutable values it wants, and also allowed not to do so, and there's rarely any good reason to care about whether a particular implementation actually does so. – abarnert May 05 '18 at 22:34
  • @ChrisLutz surely `is True` is valid as well? – Rob Grant Jul 17 '18 at 20:54
8

That will only work for small numbers and I'm guessing it's also implementation-dependent. Python uses the same object instance for small numbers (iirc <256), but this changes for bigger numbers.

>>> a = 2104214124
>>> b = 2104214124
>>> a == b
True
>>> a is b
False

So you should always use == to compare numbers.

sttwister
  • 2,279
  • 1
  • 19
  • 23
1

== is what you want, "is" just happens to work on your examples.

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
0
>>> 2 == 2.0
True
>>> 2 is 2.0
False

Use ==

Christopher Bruns
  • 9,160
  • 7
  • 46
  • 61