Compare string and number



examples/pitfalls/compare.py
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()