There are eight comparison operations in Python. They all have the same priority (which is higher than that of the Boolean operations). Comparisons can be chained arbitrarily; for example,
x < y <= z is equivalent to
x < y and y <= z, except that
y is evaluated only once (but in both cases
z is not evaluated at all when
x < y is found to be false).
This table summarizes the comparison operations:
||strictly less than|
||less than or equal|
||strictly greater than|
||greater than or equal|
||negated object identity|
Objects of different types, except different numeric types, never compare equal. Furthermore, some types (for example, function objects) support only a degenerate notion of comparison where any two objects of that type are unequal. The
>= operators will raise a
TypeError exception when comparing a complex number with another built-in numeric type, when the objects are of different types that cannot be compared, or in other cases where there is no defined ordering.
Non-identical instances of a class normally compare as non-equal unless the class defines the
Instances of a class cannot be ordered with respect to other instances of the same class, or other types of object, unless the class defines enough of the methods
__ge__() (in general,
__eq__() are sufficient, if you want the conventional meanings of the comparison operators).
The behavior of the is and is not operators cannot be customized; also they can be applied to any two objects and never raise an exception.