Python Is Not

The Python is not operator is used to test if two variables don’t refer to the same object.

>>> x = [1, 2]
>>> x = [1, 2]
>>> x is not y
True
>>> x = [1, 2]
>>> x = y
>>> x is not y
False

It’s not the same as the != operator, which tests if the values are not equal. With is not it doesn’t matter if the values are not equal, it has to actually not refer to the same object.

Note that constants refer to the same object in Python

>>> x = 3
>>> y = 3
>>> x is not y
False