Identity Operators in Python

These operators are used to check if two objects are at the same memory location.

Notice that they do not compare values, but memory location.

They are:

  • is: returns True if both objects are identical
  • is not: returns True if both objects are not identical

Let’s see a program that shows how each of them is used.

x = 5
y = 5

list_one = [4, 7]
list_two = [4, 7]

print(x is y)

print(list_one is not list_two)

print(list_one is list_two)

print(list_one == list_two)
True

True

False

True

The last two examples demonstrate that even though is returns False, == returns True, since list_one is equal to list_two.

The behavior for int and list is different because lists are mutable.