Home » Python » How does Python 2 compare string and int? Why do lists compare as greater than numbers, and tuples greater than lists?

How does Python 2 compare string and int? Why do lists compare as greater than numbers, and tuples greater than lists?

Posted by: admin October 4, 2018 Leave a comment

Questions:

The following snippet is annotated with the output (as seen on ideone.com):

print "100" < "2"      # True
print "5" > "9"        # False

print "100" < 2        # False
print 100 < "2"        # True

print 5 > "9"          # False
print "5" > 9          # True

print [] > float('inf') # True
print () > []          # True

Can someone explain why the output is as such?


Implementation details

  • Is this behavior mandated by the language spec, or is it up to implementors?
  • Are there differences between any of the major Python implementations?
  • Are there differences between versions of the Python language?
Answers: