DEV Community

Cover image for Comparison Operators in python
Aswin Barath
Aswin Barath

Posted on • Edited on

Comparison Operators in python

So, basically, comparison operators are used to comparing two values that are numbers.

If we level up to be geeky, comparison operators can also be used to compare other data types.

Now, let's start with equality checks and I hope you like spider-man movies.

== Equal comparison operator

Alt Text
Output:

False
True
Enter fullscreen mode Exit fullscreen mode

!= Not Equal comparison operator

Alt Text
Output:

True
False
Enter fullscreen mode Exit fullscreen mode

Alright, I'm sure that you are aware of how to use other operators to compare two number values, right?
OK, now's the time to level up to be geeky.

For the rest of the operators let us compare the letters from the Alphabet.
Wait, what?!
You heard me right!

Let me explain it at the end of this post.

> Greater than comparison operator

Alt Text
Output:

False
True
False
Enter fullscreen mode Exit fullscreen mode

< Less than comparison operator

Alt Text
Output:

True
True
False
Enter fullscreen mode Exit fullscreen mode

>= Greater than or equal to comparison operator

Alt Text
Output:

False
True
True
Enter fullscreen mode Exit fullscreen mode

<= Less than or equal to comparison operator

Alt Text
Output:

False
True
True
Enter fullscreen mode Exit fullscreen mode

Here's the answer for the above craziness.

When we compare two letters (or characters), it gets converted into ASCII code. You can check the link where the table contains 'DEC' (Decimal values) for the characters from Alphabet.

Now that the characters are converted into ASCII code, which is nothing but numbers and we are back to square one.
That is, we can compare the values as numbers and return True or false.

Best Resources

Who Am I?

I’m Aswin Barath, a Software Engineering Nerd who loves building Web Applications, now sharing my knowledge through Blogging during the busy time of my freelancing work life. Here’s the link to all of my socials categorized by platforms under one place: https://linktr.ee/AswinBarath

Thank you so much for reading my blog🙂.

Top comments (0)