Free tools Windows power users keep installed

One-click scans. No signup required.

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

Python’s is operator checks whether two references point to the very same object; == checks whether their values are equal. Some integer expressions may share an object because an interpreter reuses it, but that behavior is an implementation detail—not a rule to rely on. For integer value comparisons, use ==.

What is and == actually compare

Every Python object has an identity, a type, and a value. The Python data model defines is as an identity test: it is true when both expressions refer to the same object. By contrast, == asks whether the objects compare equal.

Two integers can therefore have the same numeric value without being the same object. Equal values do not imply equal identities.

Why some equal integers appear to be identical

An interpreter can reuse an existing object when computing an immutable value. If two expressions use the same reused integer object, is may be true; if they produce separate objects with the same value, it may be false. The result reflects how that interpreter handled those expressions, not a different meaning for is.

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

Small-integer reuse is an optimization documented by implementations such as PyPy. It does not turn identity into a test for numeric equality. A Python issue report likewise illustrates that equal values need not be identical and describes caching as an implementation detail; its examples should not be treated as a rule for all Python releases.

Is the familiar −5 to 256 range guaranteed?

No. The often-repeated range of −5 through 256 is not a portable Python language guarantee. The language documentation does not promise that boundary, and whether two integer expressions share identity can depend on how the value is produced. Do not use this range to decide when is is safe for integers.

Compare integer values with ==

For example:

a = 1000
b = int("1000")

print(a == b)  # True: the values compare equal
print(a is b)  # Do not rely on this result

The equality comparison expresses the question “Do these integers have the same value?” The identity comparison asks a different question: “Are these references to the same object?”

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When should you use is?

Use is when object identity itself matters or when your program guarantees that two references point to the same object. The Python FAQ explains that assignment and storing an object reference in a container preserve identity. Those cases are different from independently computing integers and assuming the interpreter will reuse an object.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Use == to compare integer values.
  • Use is only when identity is the condition you intend to test, or identity is assured by the program.

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.