When making comparisons of variables you can use both == and is, although they do not work in the same way. In general, we have:

  • You use == to compare values. It is used when you want to know if two variables have the same value.
  • You use is to compare objects. It is used when you want to know if two variables (literally) refer to the same object.

For example:

>>> a = 19999990
>>> b = 19999989 +1
>>> a is b
False
>>> a == b
True
>>> c = 500
>>> d = 500
>>> c is d
False
>>> c == d
True

In this case a is not the same object as b but they have the same value.

Note: Because of the way the CPython reference implementation works, you'll get inconsistent and unexpected results if you mistakenly use is to compare for equality on integers. The reference implementation of Python caches integer objects in the range (-5 to 256 inclusive) as singleton instances for performance reasons.

Here's an example demonstrating this:

>>> e = 250
>>> f = 250
>>> e is f
True
>>> e == f
True
>>> g = 257
>>> h = 257
>>> g is h
False
>>> g == h
True
>>>