`a is b` checks that a and b are literally the same object, comparing their identities rather than just checking that they're equal in value. This distinction matters for mutable objects: two separately created lists with identical contents are equal but not the same object. is is the correct, idiomatic way to check for None, True, and False specifically, since Python guarantees there's only ever one instance of each of those in a running program.
1Understanding Identity Operators
a is b checks that a and b are literally the same object, comparing their identities rather than just checking that they're equal in value. This distinction matters for mutable objects: two separately created lists with identical contents are equal but not the same object. is is the correct, idiomatic way to check for None, True, and False specifically, since Python guarantees there's only ever one instance of each of those in a running program.
Never use is to compare numbers or strings for equality — small integers and short strings happen to be cached and reused by CPython, which can make is appear to 'work' by accident, but that behavior isn't guaranteed and shouldn't be relied on.
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)
print(a is b)2Practical Example
Here is a real-world application of Identity Operators showing how it is used in production Python code.
value = None
if value is None:
print("No value provided")3Best Practices
Follow these guidelines when working with Identity Operators:
1. Use is None / is not None instead of == for checking None
2. Use ==, not is, to compare the value/content of two objects, like two lists or two custom instances
3. Don't rely on small-integer or string caching behavior with is — it's a CPython implementation detail, not a language guarantee
Tip: Never use is to compare numbers or strings for equality — small integers and short strings happen to be cached and reused by CPython, which can make is appear to 'work' by accident, but that behavior isn't guaranteed and shouldn't be relied on.
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)
print(a is b)