What is the difference between is and ==?
In Python, both is and == are used for comparison, but they serve different purposes. Here’s a detailed explanation of each:
== (Equality Operator)
- Purpose: Compares the values of two objects to check if they are equal.
- Usage: Checks if the contents or data of two objects are the same.
- Example:
a = [1, 2, 3] b = [1, 2, 3] print(a == b) # Output: True (since the lists have the same content)
is (Identity Operator)
- Purpose: Compares the identity of two objects, checking if they refer to the same object in memory.
- Usage: Checks if two objects are the exact same instance (i.e., have the same memory address).
- Example:
a = [1, 2, 3] b = [1, 2, 3] print(a is b) # Output: False (because a and b are two different objects in memory) # Comparing with the same object reference c = a print(a is c) # Output: True (because a and c refer to the same object)
Key Differences:
==checks value equality, i.e., whether the objects have the same data or value.ischecks object identity, i.e., whether the objects refer to the same memory location.
Example with Lists
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True: the values in the lists are the same
print(a is b) # False: they are different objects in memory
Example with Small Integers
x = 1000
y = 1000
print(x == y) # True: the values are the same
print(x is y) # False: they are different objects in memory, because Python caches small integers, but this behavior can vary
Example with Small Integers within a Specific Range
x = 100
y = 100
print(x == y) # True: the values are the same
print(x is y) # True: small integers (-5 to 256) are cached by Python, so they refer to the same object
Summary:
- Use
==when you want to check if two objects have the same value. - Use
iswhen you want to check if two objects are the exact same instance (memory address).
# Equality (==) vs Identity (is) Example
# Example 1: Using `==` with lists
a = [1, 2, 3]
b = [1, 2, 3]
print("Using == with lists:")
print(a == b) # Output: True (the lists have the same content)
print(a is b) # Output: False (different objects in memory)
# Example 2: Using `is` with the same object reference
c = a
print("\nUsing is with the same object reference:")
print(a is c) # Output: True (a and c refer to the same object)
# Example 3: Using `==` and `is` with small integers
x = 1000
y = 1000
print("\nUsing == and is with large integers:")
print(x == y) # Output: True (the values are the same)
print(x is y) # Output: False (different objects in memory)
# Example 4: Using `==` and `is` with small integers within a specific range
x = 100
y = 100
print("\nUsing == and is with small integers (cached by Python):")
print(x == y) # Output: True (the values are the same)
print(x is y) # Output: True (small integers are cached by Python, so they refer to the same object)
# Example 5: Changing values in a list and comparing with `==` and `is`
a = [1, 2, 3]
b = [1, 2, 3]
print("\nUsing == and is after modifying lists:")
a[0] = 10 # Modifying list a
print(a == b) # Output: False (values are different)
print(a is b) # Output: False (they are still different objects in memory)
Interview angle
- “
isor==?” -iscompares identity,==compares value via__eq__. Useisonly forNone,True,Falseand sentinel objects. - “Why does
a is bsometimes work for numbers and strings?” - interning. CPython caches small integers (-5 to 256) and some string literals, so identity accidentally matches value. It’s an implementation detail, and code relying on it breaks with different values or across versions. - “How do you compare floats?” - not with
==. Usemath.isclosewith an appropriate tolerance, because binary representation means0.1 + 0.2 != 0.3.