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.
Operator | Example | Description |
---|---|---|
is | a is b | Returns True if both variables share same memory location, i.e. are same objects. |
is not | a is not b | Returns 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)
In [2]:
a = 20
b = 30
print(a is b)
In [3]:
a = 'Logical'
b = 'Logical'
print(a is b)
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)
In [2]:
a = 20
b = 10
print(a is not b)
In [3]:
a = 'Logical'
b = 'Logical'
print(a is not b)