Identity operators in Python with Example

Identity operators in Python are special type of binary operators that compare the memory locations of two objects.

They are useful to verify if two objects, variables, or values are stored on the same part of the memory location.

Python language supports two types of identity operators. They are:

  • is
  • is not

Let’s have a look at both identity operators one by one with the help of examples.

Identity Is operator (is)


This identity operator compares the objects and evaluates to true if both objects are exactly the same and stored in the same memory location.

In other words, the is identity operator gives the output true if the variables on left and right side of the operator refer to the same object and otherwise return false value. The operator symbol is ‘is’.

Let’s take some example programs based on the use of ‘is’ identity operator in Python.

Example 1:

x = 20
y = 20
result = x is y
print(result)

str1 = "Python"
str2 = "Python"
result = str1 is str2
print(result)

name1 = "John"
name2 = "Jack"
result = name1 is name2
print(result)

a = True
b = 1
result = a is b
print(result)

p = "20"
q = 20
result = p is q
print(result)
Output:
       True
       True
       False
       False
       False

Explanation:

a) In this example, x and y are integer objects with the same values. Therefore, they are equal, identical, and stored in the same memory location. Hence, ‘is’ identity operator returned true value.

b) Similarly, in the case of str1 and str2 (strings).

c) In the case of strings name1 and name2, both are not identical because they are different objects. Hence, the ‘is’ identity operator returned false value.

d) Similarly, a is a boolean object with true value, whereas b is an integer object with value 1. Therefore, they are different objects and occupying the different memory locations. Hence, the ‘is’ id operator returned false value.

Example 2:

list1 = [10, 20.5, 30, 'text']
list2 = [10, 20.5, 30, 'text']
result = list1 is list2
print(result)

dict1 = {
    'name': 'Jack',
    'age': 22,
}
dict2 = {
    'name': 'Jack',
    'age': 22,
}
result = dict1 is dict2
print(result)

tuple1 = (1, 2.5, 3, 'Technology')
tuple2 = (1, 2.5, 3, 'Technology')
result = tuple1 is tuple2
print(result)
Output:
      False
      False
      True

Explanation:

a) In this example, the variables list1 and list2 refer to the lists. Although they are equal (with the same values), but not identical. Since the list is mutable (changeable), the Python interpreter stores them separately in different memory locations. Hence, the ‘is’ identity operator returned false value.

b) Similarly, in the case of dict1 and dict2 dictionaries.

c) The variables tuple1 and tuple2 refer to the tuples. They are equal (with the same values), and identical. Since the tuple is immutable (unchangeable), the Python interpreter stores them in the same memory locations. Hence, the ‘is’ identity operator returned true value.

Identity Is not operator (is not)


This identity operator compares the objects and evaluates to false if both objects are exactly the same and stored in the same memory location.

In other words, the ‘is not’ identity operator gives the output false if the variables on left and right side of the operator refer to the same object and otherwise return true value. The operator symbol is ‘is not’.

Let’s take some example programs based on the use of ‘is not’ identity operator in Python.

Example 1:

num1 = 30
num2 = 40
print(num1 is not num2)

num2 = 30
print(num1 is not num2)

str1 = 'Python'
str2 = 'Language'
print(str1 is not str2)

list1 = [2, 4, 6, 8, 10]
list2 = [12, 14, 16]
print(list1 is not list2)
Output:
      True
      False
      True
      True

Example 2:

num1 = 20
num2 = 20
print("num1 = ", num1, " ", "id(num1): ", id(num1))
print("num2 = ", num2, " ", "id(num2): ", id(num2))

if(num1 is num2):
    print('num1 and num2 have the same identity')
else:
    print('num1 and num2 do not have the same identity')

if(id(num1) == id(num2)):
    print('num1 and num2 have the same identity')
else:
    print('num1 and num2 do not have the same identity')

num2 = 40
print("num1 = ", num1, " ", "id(num1): ", id(num1))
print("num2 = ", num2, " ", "id(num2): ", id(num2))

if(num1 is not num2):
    print('num1 and num2 do not have the same identity')
else:
    print('num1 and num2 have the same identity')
Output:
      num1 =  20   id(num1):  2405657215824
      num2 =  20   id(num2):  2405657215824
      num1 and num2 have the same identity
      num1 and num2 have the same identity
    
      num1 =  20   id(num1):  2405657215824
      num2 =  40   id(num2):  2405657216464
      num1 and num2 do not have the same identity

In this tutorial, we have covered about identity operators in Python with the help of example programs. Hope that you will have understood the basic points of ‘is’ and ‘is not’ identity operators and practiced all example programs.
Thanks for reading!!!
Next ⇒ Membership Operators in Python⇐ Prev Next ⇒