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

Do NOT Compare different types!

x = 12
y = 3
result = "Yes" if x > y else "No"
print(result) # Yes

x = "12"
y = "3"
print("Yes" if x > y else "No") # No

x = "12"
y = 3
print("Yes" if x > y else "No") # Yes

x = 12
y = "3"
print("Yes" if x > y else "No")  # No

In Python 2 please be careful and only compare the same types. Otherwise the result will look strange.

Yes
No
Yes
No

In Python 3, comparing different types raises exception:

Yes
No
Traceback (most recent call last):
  File "examples/other/compare.py", line 11, in <module>
    print("Yes" if x > y else "No") # Yes
TypeError: '>' not supported between instances of 'str' and 'int'