Updated June 17, 2023
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 objects have the same name and can be differentiated only using their memory location. There are two types of identity operators, namely the ‘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 the 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 the 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:
Although the value looks the same, the data type is different; hence, they get stored at different memory locations. For “m,” it’s an integer, while for “n,” it is a string. Memory location 12546 holds the number 70, and memory location 12575 contains the string “70”. Therefore, the program proceeds to calculate the value of “else” and, as a result, prints “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 as 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 possess 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 that is applied, and hence else, the value is printed on the console.
Conclusion
As we saw above, the identity operator’s role in Python is determining the class type or comparing two object values. There are many operators available in Python that can help simplify tasks. 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 –