Greater and less than operators [was Re: [Tutor] beginning to code]

Chris Angelico rosuav at gmail.com
Wed Sep 20 20:25:28 EDT 2017


On Thu, Sep 21, 2017 at 10:16 AM, Rick Johnson
<rantingrickjohnson at gmail.com> wrote:
>> >>> 'xyz' < 'abc'
>> False
>
> Interesting. But now we need to learn yet another set of
> arbitrary rules. Consider this:
>
>     >>> 'azzz' > 'zzz'
>     False
>
> Now, i'm not sure how Python decided that 'azzz' is not
> greater than 'zzz' (and not because 'azzz' has a length of
> 4!) , unless of course, Python is comparing the chars
> positionally and then discarding the fourth 'z' (a la zip()
> function behavior), but if we sum the chars in those strings
> using the return value of `ord()`, this is what we get:
>
>     >>> sum(map(ord, 'azzz'))
>     463
>     >>> sum(map(ord, 'zzz'))
>     366
>
> And now we can make it a one liner:
>
>     >>> sum(map(ord, 'azzz')) > sum(map(ord, 'zzz'))
>     True
>
> Ah. That's much more intuitive. And i just hate when my
> Python tells lies. ;-)

Uhhm.... have you ever used a dictionary? A phone book? An index (the
sort in a book, not in a database)? While there are some subtleties to
the actual sorting used, they're all broadly sorted by first letter,
and then subsequent letters within that.

ChrisA



More information about the Python-list mailing list