Python Identity Operator(with examples)

Python identity operator

Hello Techies,

This tutorial is based on the Python Identity Operator, In this blog, you will learn How to use the Python identity operator with the help of multiple examples.

Python Identity

The identity operators compare the memory location of two objects. So one can know whether two objects are equal or not.

There are two types of identity operators:

  • is
  • is not

Is operator

  • This operator is used to compare two objects, whether they are the same or not
  • If the memory location of two objects is the same then it returns True otherwise it returns False.

Example is operator

#Example 1:
data = "I Love Python"
if type(data) is str:
    print(True)
else:
    print(False)

Output:

True

In the above example, we have used is operator to check whether the data value is of string type. If the condition is fulfilled then it will return True, otherwise it will return False.

#Example 2:
a = 10
b = 10
print(a is b)

Output:

True
# Example 3:
a = 10
b = "10"
print(a is b)

Output:

False

Is Not Operator

  • This operator works in the opposite way of is operator.
  • If the memory location of two objects is not the same then it returns True and if they are the same then it returns False.

Example of is not operator

# Example 1:
data = 10.03
if type(data) is not int:
    print("Not same")
else:
    print("same")

Output:

Not same

In the above example, we have used is not operator to check whether the data value is not int type. If the condition is fulfilled then it will return Not same, otherwise it will return the same.

# Example 2:
a = 10
b = 10
print(a is not b)

Output:

False
# Example 3:
a = 10
b = "10"
print(a is not b)

Output:

True
Interview Question

Q: What's the difference between “is” and “==” in Python?

Ans: The Equality operator (==) compares the values of both operands and checks the value equality. The 'is' operator checks whether both operands refer to the same object or not.

I hope you understand the concept of Python Identity Operator. If you have any questions please comment below.

Leave a Comment