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

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Kiran KN
  • 85
  • 1
  • 10

2 Answers2

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 .

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • Out of curiosity, what makes `str` greater than `int`? It isn't alphabetical so how do they order the type names? – 2016rshah Jun 26 '15 at 15:19
  • 2
    @2016rshah what has brought you to the conclusion that `'str' > 'int'` **isn't** alphabetical? – jonrsharpe Jun 26 '15 at 15:21
  • 1
    They are alphaetical, where did you see its not alphabetical? For object of a custom class, the type is `instance` . – Anand S Kumar Jun 26 '15 at 15:23
  • 1
    Oh wait never mind I am not entirely sure what brought me to that conclusion. Silly mistake, sorry. Should I delete my comment? – 2016rshah Jun 26 '15 at 15:23
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").

Vadim Pushtaev
  • 2,332
  • 18
  • 32