Identity Operators

The Identity operators in Python are used to determine whether a value is of a certain type or class. It is also used for differentiating the same value by their memory locations.

There are two types of Identity Operators in python, are listed below :-

  1. is
  2. is not

is Operator in python

Python is Operator is used to check identity, It means two objects are the same or not. If the two objects or value refer to the same memory location then the is operator will return True else it will return False.

For example :-

a = 20
b = 20
c = 30
print(a is b)   # line 1
print(a is c)   # line 2

The result of the first line will be True as a and b are assigned to the same memory location (as numbers are immutable in python), whereas c has a different memory location.

So, the output will be :-

True
False

You can print the memory location of a, b and c using id() function in python. The id( ) function takes exactly one argument at a time.

So, the function can be written as :-

print(id(a))
print(id(b))
print(id(c))

We can also check type of values for example :-

x = 10
print( type(x) is int)   # line 1
print( type(x) is float) # line 2

The type() function returns the class type of value passed as a parameter.

So, the output will be :-

True
False

The type of the value in the right operand points to the same type in the left operand then it will be True.

is not Operator in python

is not Operator works just opposite to the is Operator. This means that  the is not Operator will return True if they are not locating the same memory location, else it will return False.

For example :-

x = 10
y = 20
z = 20
print( x is not y)  # line 1
print(y is not z)   # line 2

The result of the first line will be True as they aren’t locating the same memory locations but the second line will be False.

So, the output will be :-

True
False