In Python 3 the attempt to order a string and an int (e.g. 1 > "1") throws a TypeError.
Why does comparing a string with an int for equality not throw an error? (e.g. 1=="1")
What is an example where comparing a string with an int makes sense?
Why do JavaScript and SQL take a different approach?
In Python 3 the attempt to order a string and an int (e.g. 1
Share
This allows you, for example, to have a dictionary with keys of mixed types.
If you couldn’t compare
1and"1"for equality, you wouldn’t be able to use them as keys into the same dictionary.As things stand, you can compare them, and they always compare unequal: