Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use == to compare objects according to their equality behavior; use is to check whether two references point to the very same object. For the common check that a value is missing, write value is None or value is not None. Do not use is to compare ordinary strings or numbers.

What is the difference between == and is?

Python’s == operator asks whether two objects are equal under the equality rules for their types. The is operator asks whether both expressions refer to the same object. The Python Language Reference defines identity this way: “x is y is true if and only if x and y are the same object.” Python Language Reference: Expressions

a = [1, 2]
b = [1, 2]

print(a == b)  # True: the lists compare equal
print(a is b)  # False: they are distinct list objects

The lists contain the same elements, so they compare equal. But creating one list and then another creates distinct objects. Neither operator is a substitute for the other: equality is about a type’s comparison behavior, while identity is about object sameness.

When should you use ==?

Use == when your question is whether two values compare equal for the task at hand—for example, whether a string matches a target, a count is zero, or two lists have equal contents.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
status = "ready"
if status == "ready":
    print("Continue")

if item_count == 0:
    print("Nothing to process")

Equality is not universally a simple comparison of contents or an intrinsic “same value” test. Built-in types define their own behavior, and a user-defined class can customize equality. By default, object supplies identity-based equality; classes can instead implement rich comparison methods such as __eq__. Python Language Reference: Data Model

A comparison result also need not be a Boolean in every case. When a comparison is used in a conditional, Python truth-tests its result. If you define __eq__, make sure its behavior matches the equality contract your code needs.

When should you use is?

Use is when object identity itself matters: your code needs to know whether two references name the same object, not merely whether the objects compare equal. The everyday use is checking for the None singleton.

if result is None:
    print("No result was supplied")

if result is not None:
    process(result)

Python’s documentation recommends identity checks for singletons such as None. Python Programming FAQ You can also use identity checks with a deliberately created sentinel when identity is the contract:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
MISSING = object()
value = MISSING

if value is MISSING:
    print("No value was provided")

Why not use is for strings or numbers?

Two separate expressions can produce equal strings or numbers without being guaranteed to produce the same object. Python implementations may reuse some objects in particular circumstances, so an identity check can appear to work in a small example and fail elsewhere. That behavior is not a reliable way to compare values.

name = "Ada"
if name == "Ada":  # Compare string values
    print("Match")

Use == for ordinary string and numeric comparisons. The same distinction applies to containers: two different lists or dictionaries can have equal contents while remaining different objects.

Why is is None better than == None?

None represents the absence of a value and is a singleton, so value is None directly tests whether the value is that singleton. It also avoids relying on an object’s custom equality behavior when the question is specifically whether the value is None.

Do not replace this check with if not value unless you intend to treat every false-like value as missing. Zero, False, an empty string, and an empty container are false in a conditional but are not None.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
value = 0

if value is None:
    print("Missing")  # Does not run

if not value:
    print("False-like")  # Runs for zero
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What is the NaN exception?

Equality does not always mean that a value compares equal to itself. For a floating-point NaN (“not a number”), Python documents that float('nan') == float('nan') is false. Python Language Reference: Expressions This is one reason to think of == as a type’s equality behavior rather than a universal sameness test. Use identity only if your question is whether the references point to the same object; use the appropriate numeric handling when checking for NaN.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.