What is the difference between == and is in Python?
In Python, both `==` and `is` are used for comparisons, but they serve fundamentally different purposes. Understanding when to use each is crucial for writing correct and efficient Python code.
Understanding `==` (Equality Operator)
The == operator compares the *values* of two objects. It checks if the objects have equivalent content. This comparison is typically implemented by the __eq__ method of the objects involved.
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = [4, 5, 6]
print(f"list1 == list2: {list1 == list2}") # True (values are equal)
print(f"list1 == list3: {list1 == list3}") # False (values are different)
str1 = "hello"
str2 = "hello"
print(f"str1 == str2: {str1 == str2}") # True
Understanding `is` (Identity Operator)
The is operator compares the *identity* of two objects. It checks if two variables refer to the *exact same object in memory*. This means they must have the same memory address. It is equivalent to comparing the result of the id() function for both objects.
list1 = [1, 2, 3]
list2 = [1, 2, 3] # A new list object is created
list_ref = list1 # list_ref now refers to the same object as list1
print(f"list1 is list2: {list1 is list2}") # False (different objects in memory)
print(f"list1 is list_ref: {list1 is list_ref}") # True (same object in memory)
# For immutable objects like small integers and strings, Python might
# intern them, leading to 'is' returning True for value-equal objects.
a = 10
b = 10
c = 20
print(f"a is b: {a is b}") # True (often for small integers due to interning)
print(f"a is c: {a is c}") # False
str1 = "python"
str2 = "python"
str3 = "Python"
print(f"str1 is str2: {str1 is str2}") # True (often for identical strings due to interning)
print(f"str1 is str3: {str1 is str3}") # False
Key Differences
==compares the *values* of objects.iscompares the *identity* (memory address) of objects.==can be overridden by the__eq__method, defining custom equality logic.iscannot be overridden; it always checks for object identity.- Two objects can have equal values (
==is True) but be different objects in memory (isis False). - If two objects are the same object in memory (
isis True), their values will always be equal (==is also True).
When to Use Which
Use == when you want to compare if two objects have the same content or value. This is the most common comparison operator for most data types. Use is when you specifically need to check if two variables refer to the exact same instance of an object in memory. A common use case for is is checking if a variable is None (e.g., if x is None:), because None is a singleton object.
| Feature | `==` (Equality Operator) | `is` (Identity Operator) |
|---|---|---|
| Purpose | Compares values | Compares object identity (memory address) |
| Behavior | Checks if objects have equivalent content | Checks if variables refer to the exact same object |
| Overridable | Yes, via `__eq__` method | No |
| `None` comparison | Generally `x == None` (though `x is None` is preferred) | Recommended for `None` (e.g., `x is None`) |
| Example Output (`[1,2]` vs `[1,2]`) | True (values are equal) | False (different objects) |
| Example Output (`a = [1,2]`, `b = a`) | `a == b` is True | `a is b` is True |