!= - Is not equal to
== - Is equal to
>- greater than
<- less than
>=- greater than or equal to
<=- less than or equal to
is- test if objects are the exact same object
is not = test if objects are not the exact same object
|x||First item to be compared|
|y||Second item to be compared|
You can compare multiple items with multiple comparison operators with chain comparison. For example
is just a short form of:
This will evaluate to
True only if both comparisons are
The general form is
OP represents one of the multiple comparison operations you can use, and the letters represent arbitrary valid expressions.
0 != 1 != 0evaluates to
True, even though
0 != 0is
False. Unlike the common mathematical notation in which
x != y != zmeans that
zhave different values. Chaining
==operations has the natural meaning in most cases, since equality is generally transitive.
There is no theoretical limit on how many items and comparison operations you use as long you have proper syntax:
The above returns
True if each comparison returns
True. However, using convoluted chaining is not a good style. A good chaining will be "directional", not more complicated than
As soon as one comparison returns
False, the expression evaluates immediately to
False, skipping all remaining comparisons.
Note that the expression
a > exp > b will be evaluated only once, whereas in the case of
exp will be computed twice if
a > exp is true.
Common Gotcha: Python does not enforce typing
In many other languages, if you run the following (Java example)
... you'll get an error.
You can't just go comparing strings to integers like that. In Python, this is a perfectly legal statement - it'll just resolve to
A common gotcha is the following
This comparison will evaluate to
False without an error, every time, potentially hiding a bug or breaking a conditional.
In order to compare the equality of custom classes, you can override
!= by defining
__ne__ methods. You can also override
>). Note that you only need to override two comparison methods, and Python can handle the rest (
== is the same as
not < and
not >, etc.)
Note that this simple comparison assumes that
other (the object being compared to) is the same object type. Comparing to another type will throw an error:
isinstance() or similar will help prevent this (if desired).
Comparison by `is` vs `==`
A common pitfall is confusing the equality comparison operators
a == b compares the value of
a is b will compare the identities of
is can be thought of as shorthand for
id(a) == id(b).
Beyond this, there are quirks of the run-time environment that further complicate things. Short strings and small integers will return
True when compared with
is, due to the Python machine attempting to use less memory for identical objects.
But longer strings and larger integers will be stored separately.
You should use
is to test for
A use of
is is to test for a “sentinel” (i.e. a unique object).
This expression evaluates if
y are the same value and returns the result as a boolean value. Generally both type and value need to match, so the int
12 is not the same as the string
Note that each type has to define a function that will be used to evaluate if two values are the same. For builtin types these functions behave as you'd expect, and just evaluate things based on being the same value. However custom types could define equality testing as whatever they'd like, including always returning
True or always returning
Greater than or less than
These operators compare two types of values, they're the less than and greater than operators. For numbers this simply compares the numerical values to see which is larger:
For strings they will compare lexicographically, which is similar to alphabetical order but not quite the same.
In these comparisons, lowercase letters are considered 'greater than' uppercase, which is why
"gamma" < "OMEGA" is false. If they were all uppercase it would return the expected alphabetical ordering result:
Each type defines it's calculation with the
> operators differently, so you should investigate what the operators mean with a given type before using it.
Not equal to
y are not equal and otherwise returns