Identity Operators in Python

In all programming languages you will not find identity operators. Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location. There are two identity operators in Python.

Now, let’s see the identity operators in Python:

Operator Description Example
is Returns True if both variables are same object x is y
is not Returns True if both variables are not same object x is not y

Python example-1 with identity operators:

m = ["Minhajur", "Rahman", "Khan"]
r = ["Minhajur", "Rahman", "Khan"]
k = m
if m is k:
    print("same object")
else:
    print("is not same object")
    #value may be same but hey are not same object
Output:
same object

Python example-2 with identity operators:

m = ["Minhajur", "Rahman", "Khan"]
r = ["Minhajur", "Rahman", "Khan"]
k = m
if m is r:
    print("same object")
else:
    print("is not same object")
    #value may be same but hey are not same object
Output:
is not same object

In this tutorial, I have shown identity operators in Python. Hope you have enjoyed the tutorial. If you want to get updated, like my facebook page http://www.facebook.com/freetechtrainer and stay connected.

Add a Comment