-5
  1. maximum = max(1, 1.25, 3.14, 'a', 1000) - why is it giving 'a' as the answer? Shouldn't 'a' get converted to ASCII and be checked?

  2. maximum = max(1, 2.15, "hello") gives "hello" as answer. How does this answer come?

0

2 Answers 2

12

From the documentation -

CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address

Hence str is always greater than int .

Some more examples -

>>> class test:
...     pass
... 
>>> t = test()
>>> 'a' > 5
True
>>> t > 'a'
False
>>> type(t)
<type 'instance'>
>>> t > 10
False
>>> type(True)
<type 'bool'>
>>> True > 100
False
>>> False > 100
False

Please note the type name of test class' object is instance that is why t > 5 is False .

Sign up to request clarification or add additional context in comments.

4 Comments

Out of curiosity, what makes str greater than int? It isn't alphabetical so how do they order the type names?
@2016rshah what has brought you to the conclusion that 'str' > 'int' isn't alphabetical?
They are alphaetical, where did you see its not alphabetical? For object of a custom class, the type is instance .
Oh wait never mind I am not entirely sure what brought me to that conclusion. Silly mistake, sorry. Should I delete my comment?
8

Because strings in Python 2 are always greater than numbers.

>>> "a" > 1000
True

In Python3 it's actually fixed, they are incomparable now (because there is actually no way to compare 42 and "dog").

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.