Table of Contents
Toggle
Python identity operators are essential tools for comparing objects in Python. They help determine whether two objects share the same memory location and are of the same type. In this blog, we’ll dive deep into the two identity operators in Python: is and is not. We’ll explore their usage, differences, and practical examples to help you master these operators.
The is operator evaluates to True if both operands refer to the same object in memory. This is determined by comparing the memory addresses of the objects using the id() function. If the memory addresses are the same, the is operator returns True.
a = [1, 2, 3, 4, 5]
b = [1, 2, 3, 4, 5]
c = a
print(a is c) # Output: True
print(a is b) # Output: False
print("id(a):", id(a))
print("id(b):", id(b))
print("id(c):", id(c))
In this example, a and c refer to the same object, so a is c returns True. However, a and b are two different objects with different memory addresses, so a is b returns False.
The is not operator is the opposite of the is operator. It evaluates to True if the operands do not refer to the same object in memory. This operator is useful when you want to check if two objects are distinct.
a = [1, 2, 3, 4, 5]
b = [1, 2, 3, 4, 5]
c = a
print(a is not c) # Output: False
print(a is not b) # Output: True
print("id(a):", id(a))
print("id(b):", id(b))
print("id(c):", id(c))
Here, a is not c returns False because a and c refer to the same object. On the other hand, a is not b returns True because a and b are different objects.
Let’s look at some practical examples to understand how identity operators work in different scenarios.
a = "TutorialsPoint"
b = a
print("id(a), id(b):", id(a), id(b))
print("a is b:", a is b) # Output: True
print("b is not a:", b is not a) # Output: False
In this example, a and b refer to the same string object, so a is b returns True.
a = [1, 2, 3]
b = [1, 2, 3]
print("id(a), id(b):", id(a), id(b))
print("a is b:", a is b) # Output: False
print("b is not a:", b is not a) # Output: True
Here, a and b are two different list objects, even though they contain the same elements. Therefore, a is b returns False.
a = [10, 20, 30]
b = [10, 20, 30]
print("id(a[0]), id(a[1]), id(a[2]):", id(a[0]), id(a[1]), id(a[2]))
print("id(b[0]), id(b[1]), id(b[2]):", id(b[0]), id(b[1]), id(b[2]))
In this case, the individual elements of the lists a and b share the same memory addresses because integers are immutable in Python. However, the lists themselves are different objects.
Python identity operators, is and is not, are powerful tools for comparing objects based on their memory addresses. They are particularly useful when you need to determine if two variables refer to the same object. By understanding these operators and their behavior, you can write more efficient and reliable Python code.
Whether you’re working with strings, lists, or other data types, identity operators provide a clear and concise way to compare objects in Python. Keep practicing with different examples to solidify your understanding and become a Python pro!
