Why does 4 < \'3\'
return True
in Python 2?
Is it because when I place single quotes around a number Python sees it as a string and stri
From Python v2.7.2 documentation
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.
When you order two strings or two numeric types the ordering is done in the expected way (lexicographic ordering for string, numeric ordering for integers).
When you order a string and an integer the type names are ordered. "str" is lexicographically after "int", "float", "long", "list", "bool", etc. However a tuple will order higher than a string because "tuple" > "str":
0 > 'hi'
False
[1, 2] > 'hi'
False
(1, 2) > 'hi'
True
also see comparison uses lexicographical ordering from docs.python.org
In Python 3.x the behaviour has been changed so that attempting to order an integer and a string will raise an error:
>>> '10' > 5
Traceback (most recent call last):
File "", line 1, in
'10' > 5
TypeError: unorderable types: str() > int()