What is the difference between == and is

This is a fundamental and very important concept in Python.

In short:

  • == is the equality operator. It checks if the values of two objects are the same.

  • is is the identity operator. It checks if two variables point to the exact same object in memory.

Let's break this down with examples.


1. The == Operator (Value Equality)

The == operator compares the data or values inside the objects. It doesn't matter if they are stored in different places in memory; if their contents are the same, == will return True.

Example:

python
list_a = [1, 2, 3]
list_b = [1, 2, 3] # A different list with the same contents

print(list_a == list_b) # Output: True
# Explanation: The values [1, 2, 3] are the same as [1, 2, 3].


2. The is Operator (Identity / Memory Address)

The is operator checks if two variable names point to the exact same object in your computer's memory. It's like asking, "Are these two names just aliases for a single, unique thing?"

Example:

python
list_a = [1, 2, 3]
list_b = [1, 2, 3] # A different list with the same contents
list_c = list_a     # list_c is a new name for the SAME object list_a points to

print(list_a is list_b) # Output: False
# Explanation: Even though the contents are identical, list_a and list_b are two separate list objects stored in different memory locations.

print(list_a is list_c) # Output: True
# Explanation: Both list_a and list_c are names that reference the exact same underlying list.

You can use the built-in id() function to see an object's unique memory address, which makes this clear:

python
print(id(list_a)) # e.g., 140247661942464
print(id(list_b)) # e.g., 140247661946304 (Different from list_a!)
print(id(list_c)) # e.g., 140247661942464 (Same as list_a!)


The Special Case with Small Integers and Strings

Due to performance optimization in Python (a concept called interning), small integers and some strings are cached. This means Python creates only one instance of these common values in memory.

This can be a common "gotcha" for beginners:

python
a = 256
b = 256
print(a is b) # Output: True (Python reuses the same object for small integers)

a = 257
b = 257
print(a is b) # Output: False (For larger integers, this may not be true)

a = "hello"
b = "hello"
print(a is b) # Output: True (Short strings are often interned)

a = "this is a much longer string that might not be interned!"
b = "this is a much longer string that might not be interned!"
print(a is b) # Output: False (Behavior can be unpredictable)

⚠️ Crucial Warning: Never rely on interning. Always use == for comparing values of integers and strings. The is behavior for these types is an implementation detail and not guaranteed.


Summary Table

Feature== (Equality)is (Identity)
ChecksIf values are the sameIf memory addresses are the same
Method Used.__eq__()id(a) == id(b)
Analogy"Are these two books have the same text?""Is this the exact same physical book?"
Use CaseComparing the data of two objects (e.g., do two lists have the same numbers?)Checking if two variables reference one object (e.g., checking for NoneTrueFalse)

When to Use Which?

  • Use == by default for comparing values. This is what you need 99% of the time when checking if two numbers, strings, lists, etc., are equivalent.

  • Use is only when you care about identity. The most common and correct uses are for comparing to singletons like:

    • if variable is None:

    • if variable is True:

    • if variable is False:

Using is for these singletons is preferred because there is exactly one instance of NoneTrue, and False in the Python runtime.

To Top