Unlike C, all comparison operations in Python have the same priority,
which is lower than that of any arithmetic, shifting or bitwise
operation. Also unlike C, expressions like
a < b < c have the
interpretation that is conventional in mathematics:
|comparison||::=||or_expr ( comp_operator or_expr )*|
|comp_operator||::=||"<" | ">" | "==" | ">=" | "<=" | "<>" | "!="|
Comparisons yield boolean values:
Comparisons can be chained arbitrarily, e.g.,
x < y <= z is
x < y and y <= z, except that
evaluated only once (but in both cases
z is not evaluated at all
x < y is found to be false).
Formally, if a, b, c, ..., y, z are expressions and opa, opb, ..., opy are comparison operators, then a opa b opb c ...y opy z is equivalent to a opa b and b opb c and ... y opy z, except that each expression is evaluated at most once.
Note that a opa b opb c doesn't imply any kind of comparison
between a and c, so that, e.g.,
x < y > z is
perfectly legal (though perhaps not pretty).
!= are equivalent; for consistency with
!= is preferred; where
!= is mentioned below
<> is also accepted. The
<> spelling is considered
the values of two objects. The objects need not have the same type.
If both are numbers, they are converted to a common type. Otherwise,
objects of different types always compare unequal, and are
ordered consistently but arbitrarily. You can control comparison
behavior of objects of non-builtin types by defining a
method or rich comparison methods like
__gt__, described in
(This unusual definition of comparison was used to simplify the definition of operations like sorting and the in and not in operators. In the future, the comparison rules for objects of different types are likely to change.)
Comparison of objects of the same type depends on the type:
If not equal, the sequences are ordered the same as their first
differing elements. For example,
cmp([1,2,x], [1,2,y]) returns
the same as
cmp(x,y). If the corresponding element does not
exist, the shorter sequence is ordered first (for example,
[1,2] < [1,2,3]).
The operators in and not in test for set
x in s evaluates to true if x
is a member of the set s, and false otherwise.
not in s returns the negation of
x in s.
The set membership test has traditionally been bound to sequences; an
object is a member of a set if the set is a sequence and contains an
element equal to that object. However, it is possible for an object
to support membership tests without being a sequence. In particular,
dictionaries support membership testing as a nicer way of spelling
key in dict; other mapping types may follow suit.
For the list and tuple types,
x in y is true if and
only if there exists an index i such that
x == y[i] is true.
For the Unicode and string types,
x in y is true if
and only if x is a substring of y. An equivalent test is
y.find(x) != -1. Note, x and y need not be the
same type; consequently,
u'ab' in 'abc' will return
Empty strings are always considered to be a substring of any other string,
"" in "abc" will return
Changed in version 2.3:
Previously, x was required to be a string of
For user-defined classes which define the __contains__() method,
x in y is true if and only if
y.__contains__(x) is true.
For user-defined classes which do not define __contains__() and
do define __getitem__(),
x in y is true if
and only if there is a non-negative integer index i such that
x == y[i], and all lower integer indices
do not raise IndexError exception. (If any other exception
is raised, it is as if in raised that exception).