Python is vs == Operator: What Is the Difference?

Short answer
== checks whether two objects have equal values, while is checks whether they are the exact same object in memory; use == to compare values and is only for None (and other singletons like True and False).

Two lists can hold the same items and still be two separate objects. The == operator asks "do these look equal?", and is asks "are these literally the same thing?". Mixing them up is one of the most common sources of puzzling bugs, especially because is sometimes appears to work on small numbers and short strings.

In day-to-day code you will use == for almost every comparison, as covered in Python operators. The is operator has one everyday job: checking for None.

Python
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)
print(a is b)

is vs == comparison table

Aspect`==``is`
Question it answersDo the two values compare equal?Are both names bound to the same object?
ComparesValues (calls the object's __eq__ method)Identity (object location in memory)
Can be customised?Yes, a class can define __eq__No, it cannot be overridden
Two equal lists [1] and [1]TrueFalse (two separate objects)
Same list through two namesTrueTrue
Check for NoneWorks, but not recommendedRecommended: x is None
Negated form!=is not

When to use == (value comparison)

Use == whenever you care about the content: numbers, strings, lists, dictionaries or your own objects that define equality.

  • Checking user input: answer == "yes".
  • Comparing results in conditions such as if total == 100:.
  • Comparing collections: [1, 2] == [1, 2] is True.

If you are unsure which operator to use, use ==.

When to use is (identity comparison)

Use is when you really mean "the same object".

  • if value is None: and if value is not None:. None exists only once in a program, so this is exact and cannot be fooled by a class that customises ==.
  • Checking whether two names share one mutable object, for example to confirm that a list was not copied.

Do not use is to compare numbers or strings. See the truthy and falsy values lesson for the related idea of testing emptiness.

Why is sometimes gives True for equal numbers and strings

CPython caches small integers (from -5 to 256) and may reuse some identical strings and constants, so is can happen to return True for equal values. This is an implementation detail, not a language guarantee, and it differs between situations. Python 3.8 and later also raise a SyntaxWarning when you write x is 5 or x is "abc" with a literal. Never rely on it; use == for values.

Examples you can run

Press Run, then edit the code and run it again.

Equal values, different objects

copy() creates a new list with the same content, so == is True while is is False.

Equal values, different objects
cart = ["pen", "book"]
copy = cart.copy()
alias = cart
print(cart == copy)
print(cart is copy)
print(cart is alias)
Expected outputTrue
False
True

Checking for None with is

is None is the standard way to test for a missing value, and it stays correct even for falsy values like 0.

Checking for None with is
def describe(score):
    if score is None:
        return "no score"
    return f"score {score}"

print(describe(None))
print(describe(0))
Expected outputno score
score 0

A class can change what == means

The class below defines equality by code, but is still checks identity.

A class can change what == means
class Product:
    def __init__(self, code):
        self.code = code

    def __eq__(self, other):
        return self.code == other.code

p1 = Product("A1")
p2 = Product("A1")
print(p1 == p2)
print(p1 is p2)
Expected outputTrue
False

Common mistakes

Comparing strings or numbers with is

✗ Wrong

answer = "yes"
if answer is "yes":
    print("match")
SyntaxWarning: "is" with 'str' literal. Did you mean "=="?

✓ Right

answer = "yes"
if answer == "yes":
    print("match")

Use == for values. is on literals is unreliable.

Using == None

✗ Wrong

value = None
if value == None:
    print("missing")
Works for built-in types, but a class that customises __eq__ can make `== None` return True by mistake.

✓ Right

value = None
if value is None:
    print("missing")

Write value is None; it checks identity and cannot be overridden.

Python is vs == Operator: What Is the Difference?: questions people ask

What is the difference between == and is in Python?

== tests whether two objects have equal values, and is tests whether they are the same object. Equal objects are not always identical.

Why does `is` work for small numbers but not large ones?

CPython keeps ready-made objects for small integers, so equal small numbers are often the same object. That is an implementation detail you should not depend on.

Should I use `is None` or `== None`?

Use is None. There is only one None object, so an identity check is exact, faster and recommended by the Python style guide (PEP 8).

Keep learning

Step-by-step lessons:
Python OperatorsPython Logical ConditionsPython Booleans

Related:
Python Mutable vs Immutable Objects ExplainedPython Deep Copy vs Shallow Copy: What Is the Difference?Copying Lists and DictsPython Truthy and Falsy Values

More in operators and syntax:
Python Division (/) vs Floor Division (//)Python break vs continue vs pass: Differences See all →