Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Compare string and number

x = 2
y = "2"

print(x > y)
print(x < y)

Python 2 - compares them based on the type of values (wat?)

$ python examples/pitfalls/compare.py
False
True

Python 3 - throws exception as expected.

$ python3 examples/pitfalls/compare.py
Traceback (most recent call last):
  File "examples/pitfalls/compare.py", line 4, in <module>
    print(x > y)
TypeError: unorderable types: int() > str()