GNU Info

Info Node: (python2.1-tut.info)Comparing Sequences and Other Types

(python2.1-tut.info)Comparing Sequences and Other Types


Prev: More on Conditions Up: Data Structures
Enter node , (file) or (file)node

Comparing Sequences and Other Types
===================================

Sequence objects may be compared to other objects with the same
sequence type.  The comparison uses _lexicographical_ ordering: first
the first two items are compared, and if they differ this determines
the outcome of the comparison; if they are equal, the next two items
are compared, and so on, until either sequence is exhausted.  If two
items to be compared are themselves sequences of the same type, the
lexicographical comparison is carried out recursively.  If all items of
two sequences compare equal, the sequences are considered equal.  If
one sequence is an initial sub-sequence of the other, the shorter
sequence is the smaller one.  Lexicographical ordering for strings uses
the ASCII ordering for individual characters.  Some examples of
comparisons between sequences with the same types:

     (1, 2, 3)              < (1, 2, 4)
     [1, 2, 3]              < [1, 2, 4]
     'ABC' < 'C' < 'Pascal' < 'Python'
     (1, 2, 3, 4)           < (1, 2, 4)
     (1, 2)                 < (1, 2, -1)
     (1, 2, 3)             == (1.0, 2.0, 3.0)
     (1, 2, ('aa', 'ab'))   < (1, 2, ('abc', 'a'), 4)

Note that comparing objects of different types is legal.  The outcome
is deterministic but arbitrary: the types are ordered by their name.
Thus, a list is always smaller than a string, a string is always
smaller than a tuple, etc.  Mixed numeric types are compared according
to their numeric value, so 0 equals 0.0, etc.(1)

---------- Footnotes ----------

(1)  The rules for comparing objects of different types should not be
relied upon; they may change in a future version of the language.


automatically generated by info2www version 1.2.2.9