# Re: [Tutor] Sequence Comparison

Magnus Lycka magnus at thinkware.se
Wed Jan 14 15:59:18 EST 2004

>   i was reading the Sequence comparison section of
> python documentation. but i really didnot understand
> from it. if anyne of you can explain it to me then it
> will be very kind of you. the below is the examples
> with the questions i have in mind.

If you think of < as a tool for sorting, I think
things will fall into place. In a sorted list, an
item X will always come before another item Y, if
X < Y. Ok?

The concept of "less than" and the "<" symbol is of
mathematical origin, and in that context only numbers
are compared, but in programming we have broadened the
concept a bit.

> >>> (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

Would section 1.2.3 come before section 1.2.4 in a
manual? I think so...

It's reasonable to think of (1,2,4) in a similar way
that you think of 123 or "ABC". I.e. the first element
is of greater importance than the following, and only
if the first elements are equal do you compare the rest.

Imagine that the < was only defined for simple values
and you would use a function less_than(a,b) instead of
a < b for compound values. Then you could write it a
bit like like this:

def less_than(a, b):
# Compare first element in sequence.
if a[0] < b[0]:
# First element is less
return True
elif a[0] == b[0]:
# Starts equal, compare the rest
return less_than(a[1:], b[1:])
else:
# First element is greater
return False

The obvious problem with the code above is that it doesn't
handle empty sequences, so it won't work in real life. (But
who cares, we don't need it anyway! :)

> >>> '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

Forget about size. Think about how these would be sorted
in an alphabetical list. You wouldn't put C before ABC in
an alphabetical index, would you?

> >>> (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

Would section 1.2.3.4 come before 1.2.4 in a book?

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

Anything is more than nothing... Why is that difficult
to understand.

> >>> (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)

How objects of different types compare to each other is
rather arbitrary. You can't really say what is "right"
in this case, but there is a point in making sure that
python behaves in a consistent way. You should always be
able to sort a python list and get a consistent result.
Sorting depends on comparision.

http://www.python.org/doc/current/ref/comparisons.html

"The operators <, >, ==, >=, <=, and != compare 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. "

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

--
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus at thinkware.se