Click for All Topics
These operators in Python provide us with the benefit of checking if the two values allotted refer to the same memory location or not. There are two types of identity operators in Python that are discussed below.
When comparing the identities of two objects in Python, this operator is used. It determines whether two objects are the same if they both refer to the same location in memory. Let’s have a look at the example
x = 5
y = 5
z = [1, 2, 3]
w = [1, 2, 3]
print(x is y)
# Output of the above Python code:
#---> True
print(z is w)
# Output of the above Python code:
#---> False
This operator allows us to check if two objects do not have the same identity. The usage of this operator is just like the previous one with a slight difference that is shown below with the help of an example
x = 5
y = 10
print(x is not y)
#Output of the above Python code:
#---> True
Here the two variables ‘x’ and ‘y’ are given their respective values ‘5’ and ‘10’ which are totally different from each other. Since they have different values, they are considered to have separate identities.
Hence, on running the code for ‘x is not y’ we get true. indicating that both of them are different objects and not the same.
Article written by Aakarsh Pandey, Team edSlash
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India