Skip to content

is vs ==

Difference

  • is checks if two variables are pointing to the same object in memory.
  • == checks if the object pointed by each of the variable are equal via __eq__().

Example

>>> a = [1, 2, 3]
>>> b = a
>>> b is a 
True
>>> b == a
True

# Make a new copy of list `a` via the slice operator, 
# and assign it to variable `b`
>>> b = a[:] 
>>> b is a
False
>>> b == a
True

# `is` accidently works as CPython caches small integer objects.
# However, a SyntaxWarning will be produced in Python 3.8+.
>>> 1000 is 10**3
<stdin>:1: SyntaxWarning: "is" with 'int' literal. Did you mean "=="?
True
>>> 1000 == 10**3
True

References