DEV Community

loading...
Cover image for Comparison Operators in python

Comparison Operators in python

aswin2001barath profile image Aswin Barath Updated on ・2 min read

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.

Code along and have fun ;)

Discussion (0)

pic
Editor guide