Introduction to Identity Operators in Python
As the name says, identity operators are used for locating the memory unit of the objects, especially when both the objects have the same name and can be differentiated only using its memory location. There are two types of identity operators, namely ‘Is’ operator and ‘Is Not’ operator, where Is operator is used for comparing if the objects are in the same location while returning ‘true’ or ‘false’ values as a result, and Is Not operator is used for comparing if the objects perform the contrary operation with similar return values.
Types of Identity Operators in Python
There are two types of identity operators in python:
- Is
- Is Not
1. Is Operator
Is operator helps users to know if two objects are the same or not? If two objects refer to the same memory location, then Is operator returns “True”. However, if two objects refer to separate memory locations, the “Is” operator will return “False”.
Examples of Is Operator
Given below are the examples o:
Example #1
m = 70
n = 70
if ( m is n ):
print("Result: m and n have same identity")
else:
print("Result: m and n do not have same identity")
Output:
Here both m and n refer to the object, hence if calculates to true(in above code), and prints “Result: m and n have the same identity”.
Below is the pictorial representation of it.
Here m and n refer to value “70”, which has a memory location of 12546.
Example #2
m = 70
n = "70"
if ( m is n ):
print("Result: m and n have same identity")
else:
print("Result: m and n do not have same identity")
Output:
Here although the value looks the same, the data type is different, and hence they get stored at the different memory locations. For “m”, it’s an integer, while for “n”, it is a string. Integer 70 is stored at memory location 12546, while string “70” stored at memory location 12575. Hence, else gets calculated and results in printing “Result: m and n do not have the same identity”.
Example #3
Let’s compare the memory location of the object now.
m = 70
n = 70
if ( id(m) is id(n) ):
print("Result: m and n have same identity")
else:
print("Result: m and n do not have same identity")
print(id(m))
print(id(n))
Output:
One can see the id() function to see the memory location, which shows m and n two different identifiers.
Example #4
Let’s see the use of the identity operator through class implementation:
class AA(object):
def foo(self):
pass
a = AA()
if id(a.foo) is id(a.foo):
print("True")
else:
print("False")
Output:
Example #5
One can also test the data type of variable, other than the comparison of two objects through an identity operator.
x = 50
if (type(x) is int):
print ("True:If executed")
else:
print ("False:Else executed")
Output:
Here type of x is checked if it is an integer if it gets executed. However, if it’s not an integer, else the statement will get executed.
Same way, it can be implemented for other data types.
x = "50"
if (type(x) is str):
print ("True:If executed")
else:
print ("False:Else executed")
Output:
2. Is Not Operator
Is Not operator works in the opposite way of is operator? This returns true if the memory location of two objects is not the same. This returns False if the memory location of two objects is the same.
Examples of Is Not Operator
Given below are the examples of Is Not Operator:
Example #1
m = 70
n = 70
if ( m is not n ):
print("Result: m and n have same identity")
else:
print("Result: m and n do not have same identity")
Output:
As shown above. Is Not operator behaving just the opposite of Is operator? Hence printing “Result: m and n do not have the same identity” because m and n posses the same value.
Example #2
m = 70
n = "70"
if ( m is not n ):
print("Result: m and n have same identity")
else:
print("Result: m and n do not have same identity")
Output:
Here integer 70 and string “70” are different. So is not operator printing “Result: m and n have the same identity”. The memory location of integer 70 and string 70 is different, i.e. “m is not n”.
Example #3
x = 50
if (type(x) is not int):
print ("True:If executed")
else:
print ("False:Else executed")
Output:
As one can notice, the value of x is an integer. However is not operator is applied, and hence else the value is printed on the console.
Example #4
x = "50"
if (type(x) is not str):
print ("True:If executed")
else:
print ("False:Else executed")
Output:
As one can notice, the value of x is a string. However, it is not an operator is applied, and hence else the value is printed on the console.
Conclusion
As we saw above, how the identity operator plays an important role in Python is determining the class type or comparing two object values. There are ample of operators that help you ease your task to be done in python. One should go through these identities and then to other operators to get the full understanding and grip over it.
Recommended Articles
This has been a guide to Identity Operators in Python. Here we discuss the introduction, types, and examples of identity operators in python. you may also have a look at the following articles to learn more –
- Arithmetic Operators in Python
- Indentation Error in Python
- Queue in Python
- Control Statements in Python
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses