What are different Identity operators types in Python?



Each Python object is assigned a unique identification number when it is stored in memory. It can be fetched by id() function.

The is operator compares id() of two objects and returns True if both objects have same value otherwise it returns false.

The is not operator on the other hand returns false if id() of objects is same and true otherwise. Following piece of interpreter activity will show the behaviour −

>>> a = 10
>>> b = a
>>> id(a), id(b)
(1581561184, 1581561184)
>>> a is b
True
>>> a is not b
False

>>> a = 10
>>> b = 20
>>> id(a), id(b)
(1581561184, 1581561504)
>>> a is b
False
>>> a is not b
True

Advertisements