In Python, == tests whether two objects are equal in value, while is tests whether two names refer to the very same object in memory. Two separate lists with the same contents are equal (== is True) but not identical (is is False).
Use == for almost every comparison, and reserve is for singletons such as None, True, False and sentinel objects.
a == b calls a.__eq__(b). Each type decides what equal means: lists compare element by element, dictionaries compare keys and values, and your own classes can define __eq__.
The lists are two different objects, but their contents match, so == returns True.
a is b is True only when both names point to the same object. It cannot be overridden by a class. In CPython, id(a) == id(b) is the same test.
b = a does not copy the list. It only adds a second name for it, so a is b holds. c is a new list, so it is equal to a but not identical.
| Aspect | == | is |
|---|---|---|
| Question answered | Do these have the same value? | Is this the same object? |
| Can a class customise it? | Yes, via __eq__ | No |
| Typical use | Numbers, strings, lists, custom objects | None, True, False, sentinels |
| Result stable across Python versions | Yes | Not for numbers and strings |
CPython keeps one shared object for small integers (from -5 to 256) and may reuse equal string literals. As a result, is sometimes returns True for equal numbers and strings, and sometimes does not.
This caching is an implementation detail of the interpreter. It can change between Python versions and between implementations such as PyPy, and the compiler may merge constants in the same code block. Never rely on it.
Since Python 3.8, comparing a literal with is (for example if name is "admin":) produces a SyntaxWarning because the result is almost certainly not what you intended. Write name == "admin" instead.
None is a singleton, and a class may define __eq__ so that obj == None returns something surprising. The idiomatic check is:
PEP 8 recommends is None and is not None for this reason.
When None is itself a valid value, create a unique marker object and test it with is:
No caller can accidentally pass an equal object, because only _MISSING itself is identical to _MISSING.
Because True and False are singletons, is True works, but you rarely need it. Write if flag: unless you must tell True apart from other truthy values such as 1.
Without __eq__, == falls back to identity. Defining __eq__ gives value semantics, and dataclasses does it for you.
If you define __eq__ yourself, also think about __hash__, because objects that compare equal must have equal hashes when used in sets or as dictionary keys.
CPython preallocates integers from -5 to 256, so those values are shared. Larger integers are created on demand and may be separate objects. Treat it as an implementation detail.
Use ==. String identity depends on interning and compiler optimisations, so is gives unreliable results.
They give the same result, but x is not None is the idiomatic form and the one PEP 8 recommends.
is is a pointer comparison and is slightly cheaper, but the difference is almost never relevant. Choose by meaning, not speed.
== asks whether values are equal and can be customised with __eq__. is asks whether two names point to the same object and cannot be customised. Use == for values, is for None and sentinels, and never depend on integer or string caching.