/ Python

Comparison in Python3 and Python2

Every language, human or programming, has some weirdness to it. And while others might hate on the language, I learned to accept and embrace it as is. However, sometimes we can do better by choosing a different option. Not necessarily a different choice, but a different version of the language. Well, more like a different interpreter perhaps. For instance, choosing Python3 over Python2.

Again, this is not because of hate towards a language. It is merely because of sometimes we can have a better option.


In Python2 you can compare anything to anything. For instance, you can compare '123' as a string to 123 as a number.

'123' > 123
# True

The same with the max function.

max('123', 123)
# '123'

This is odd, and a source of bugs obviously.

This is not possible in Python3. If we tried the previous functions, we would get an error telling us that the comparison is not supported between instances of 'int' and 'str', which exactly what I would expect.


Anas Shekhamis

Anas Shekhamis

A software engineer who specializes in architecting and developing web applications. On a daily basis, I use Python, JavaScript, Ruby, and PHP to design and implement API's and build web applications.

Read More
Comparison in Python3 and Python2
Share this