Python Identity Operators


Introduction to Python Identity Operators

Identity operators are used to compare the memory location of the objects. If two objects are sharing the same memory location, i.e. they are the same objects but may have the same or different names.

OperatorExampleDescription
isa is bReturns True if both variables share same memory location, i.e. are same objects.
is nota is not bReturns True if both variables do not share the same memory location, i.e. are not the same objects.

Identity Operators

is Operator

is operator returns True if two variables point to the same memory location, i.e. they have the same value, else returns false.

In [1]:
a = 20
b = 20
print(a is b)
True
In [2]:
a = 20
b = 30
print(a is b)
False
In [3]:
a = 'Logical'
b = 'Logical'
print(a is b)
True

is not Operator

is not operator returns True if two variables do not point to the same memory location, i.e. they have different values, else returns false.

In [1]:
a = 20
b = 20
print(a is not b)
False
In [2]:
a = 20
b = 10
print(a is not b)
True
In [3]:
a = 'Logical'
b = 'Logical'
print(a is not b)
False

Resources

Handwritten Notes

Python Identity Operators

References:

  1. Python Identity Operators
  2. Python Membership and Identity Operators
  3. Identity Operator in Python