# [Tutor] Sequence Comparison

Michael Janssen Janssen at rz.uni-frankfurt.de
Tue Jan 13 05:25:03 EST 2004

```On Mon, 12 Jan 2004, Hameed Khan wrote:

> >>> (1, 2, 3)              < (1, 2, 4)
> True
> # Q.1 why it returns True when 1 is not lower then 1
> and 2 is not lower then 2. only 3 is lower then 4

python doesn't use the tuples directly for comparing but the elements
inside both tuples (same for lists).

python compares each element from left to right until it founds a pair
of element, where the condition (<) is true:
1 < 1 ---> python find's: not true therefore:
2 < 2 ---> not true, next try
3 < 4 ---> true. Overall condition is true

> >>> [1, 2, 3]              < [1, 2, 4]
> True
> # the above question (Q.1) similarly apllies to this
> comparison result too

yes.

> >>> 'ABC' < 'C' < 'Pascal' < 'Python'
> True
> # Q.2 what is the procedure of comparison in different
> size of sequences because 'ABC' is greater then 'C' in
> size and 'Pascal' is greater then 'C' but equal to
> 'Python' in size

it's not string-length what get compared here: Python compares - as
Danny stated - the lexicographic weight of *each element*. 'ABC' is
smaller than 'C' because 'A' is smaller than 'C'.

> >>> (1, 2, 3, 4)           < (1, 2, 4)
> True
> # the above 2 question (Q.1 and Q.2) applies to this
> comparison because this tuples have different sizes

yes. To make it completly clear: after python found a pair of elements
which compares "smaller then" it doesn't look any further.

> >>> (1, 2)                 < (1, 2, -1)
> True
> # i totally didnot understand it

-1 from the second tuple is compared against None (because first tuple
runs out of elements). None is allways smaller than anything.

> >>> (1, 2, 3)             == (1.0, 2.0, 3.0)
> True
> # i understand it a little bit that the elements of
> second tuple converted to the type of elements of
> first tuple and then compared but what if we compare
> (1,"string",3) == (1.0,2.0,3.0)

python doesn't need to compare elements of the same type. It's completly
sane to compare integers with floats or strings. When you compare
integers with floats, each are compared by it's numerically value.
Strings allways compare greater than integers or floats.

Note: the rules for comparisions between different types (eg. strings
allways greater than integers; tuples allways greater then lists)
shouldn't be used in if-conditions. Normally, it indicates a bug when
you compare different types within a if clauses. But it's fine to sort a
list of arbitrary types by type and maybe print it out.

> >>> (1, 2, ('aa', 'ab'))   < (1, 2, ('abc', 'a'), 4)
> True
> # may be i will understand it if someone explains me
> the above comparisons

you very likly will :-) Nevertheless I describe what python does: since
to tuples are to be compared, python compares them element by element.
First two elements compares equal. Third elements are both tuple and
therefore compared elem by elem. 'aa' and 'abc' are both strings and
compared char by char. second 'a' of 'aa' compares smaller than 'b' of
'abc'

some other cases:

>>> [1,2,3] < (1,2,3)
True
---> python regards lists smaller than tuples

>>> (0,) > 99         # (0,) is a single element tuple
True
---> tuples are allways greater than integers

As you see, comparisions between different types are tricky. Better not
use them in conditions.

Michael

```