Table of Contents
Introduction
An identity operator of python compares the memory locations of objects. Python provides the two in-built identity operators:
Identity operator | Meaning |
is | Returns True if two objects point to the same memory location |
is not | Returns True if two objects do not point to the same memory location |
Examples
Example 1:
num = 5.0 if (type(num) is int): print("True") else: print("False")
Output:
False
Example 2:
num = 5.0 if (type(num) is float): print("True") else: print("False")
Output:
True
Example 3:
num = 5.1 if (type(num) is float): print("True") else: print("False")
Output:
True
Example 4:
num = 5.1 if (type(num) is not float): print("True") else: print("False")
Output:
False
Example 5:
sample1 = [] sample2 = [] sample3 = sample1 print(id(sample1)) print(id(sample2)) print(id(sample3)) if(sample1 is sample2): print(True) else: print(False) if(sample1 is sample3): print(True) else: print(False) if(sample1 is not sample2): print(True) else: print(False) if(sample1 is not sample2): print(True) else: print(False)
Output:
140600379006464
140600378378560
140600379006464
False
True
True
True
From the above code, we can see that the variable sample1 and sample3 points to the same object whereas sample2 points to another object.
0 Comments