Python Comparison Operators with Syntax and Examples

1. Python Comparison Operators

In our previous article, we talked about Python bitwise operators. Today, we focus our words on Python Comparison Operators. These are also called relational operators in Python. Along with this, we will learn different types of Comparison Operators in Python: less than, greater than, less than, greater than, equal to, and not equal to with their syntax and examples.

So, let’s start the Python Comparison Operators Tutorial.

Python Comparison Operators with Syntax and Examples

Python Comparison Operators with Syntax and Examples

2. Python Comparison Operators

A comparison operator in python, also called python relational operator, compares the values of two operands and returns True or False based on whether the condition is met. We have six of these, including and limited to- less than, greater than, less than or equal to, greater than or equal to, equal to, and not equal to. So, let’s begin with the Python Comparison operators.

3. Python Less Than (<) Operator

The first comparison operator in python we’ll see here is the less than operator. Denoted by <, it checks if the left value is lesser than that on the right.

>>> 3<6

Output: True

Since 3 is lesser than 6, it returns True.

>>> 3<3

Output: False

Because 3 is equal to 3, and not less than it, this returns False.

Recommended Reading – Python Operator Precedence

But let’s see if we can apply it to values other than ints.

>>> 3<3.0

Output: False

Here, 3 is an int, and 3.0 is a float, but 3 isn’t lesser than 3.0, or vice versa.

>>> 3.0<3

Output: False

Now, let’s try it on strings.

>>> 'Ayushi'<'ayushi'

Output: True

This one results in True because when comparing strings, their ASCII values are compared. The ASCII value for ‘A’ is 65, but that for ‘a’ is 97. Hence, ‘A’ is lesser than ‘a’. Likewise, ‘Ayushi’ is lesser than ‘ayushi’.

But does it work with Python Booleans?

>>> 0.9999999<True

Output: True

Yes, it does. But what’s fascinating is that it works on containers like tuples as well. Let’s see some of these.

>>> (1,2,3)<(1,2,3,4)

Output: True

>>> (1,3,2)<(1,2,3)

Output: False

>>> (1,2,3)<(1,3,2)

Output: True

>>> ()<(0,)

Output: True

But you can’t compare tuples with different kinds of values.

>>> (1,2)<('One','Two')

Traceback (most recent call last):

File “<pyshell#84>”, line 1, in <module>

(1,2)<(‘One’,’Two’)

TypeError: ‘<‘ not supported between instances of ‘int’ and ‘str’

However, if you get comparable elements at the same indices, it is possible to compare two tuples.

Must Check – Python Tuples vs Lists

>>> (1,'one')<(2,'two')

Output: True

And when we say same indices, we mean it.

>>> (1,'one')<('two',2)

Traceback (most recent call last):

File “<pyshell#86>”, line 1, in <module>

(1,’one’)<(‘two’,2)

TypeError: ‘<‘ not supported between instances of ‘int’ and ‘str’

Finally, these work on lists, and set, but not dictionaries.

>>> [0]<[False]

Output: False

>>> {1,2,3}<{1,3,2}

Output: False

Here, because the other set rearranges itself to {1,2,3}, the two sets are equal. Consequently, it returns False.

>>> {1:'one',2:'two'}<{1:'three',2:'four'}

Traceback (most recent call last):

File “<pyshell#91>”, line 1, in <module>

{1:’one’,2:‘two‘}<{1:‘three‘,2:‘four‘}

TypeError: ‘<‘ not supported between instances of ‘dict‘ and ‘dict

If you face any doubt in Python Comparison Operators? Please Comment.

4. Python Greater Than (>) Operator

Let’s see the Greater than Python Comparison Operator

Now that we’ve seen which constructs we can apply these operators to, we will focus on the operators now on. The greater than an operator, denoted by >, checks whether the left value is greater than the one on the right.

We recommend you to read Python Decision Making

>>> 0.5>False

Output: True

>>> 3,4,5>3,4,5.0

Output: (3, 4, True, 4, 5.0)

Hey, this created a tuple, when all we wanted to do was compare. This is because it took 5>3 as a value (True). It put this as a value in the tuple. So let’s try to find our way around this.

>>> 3,4,5 > 3,4,5.0

Output: (3, 4, True, 4, 5.0)

So we see that spaces didn’t do it. Let’s try something else.

>>> 3,4,5>(3,4,5.0)

Traceback (most recent call last):

File “<pyshell#96>”, line 1, in <module>

3,4,5>(3,4,5.0)

TypeError: ‘>’ not supported between instances of ‘int’ and ‘tuple’

Hmm, we think we need to put parentheses around both tuples.

>>> (3,4,5)>(3,4,5.0)

Output: False

Yes, it works now. We told you earlier that it’s okay to skip parentheses while declaring a tuple. But in this situation, it took 3, 4, and 5 to be ints, and believed that we were declaring a tuple, and not comparing two. You should take care of such situations by coding carefully.

Check – How much you know about Numbers in Python

5. Less Than or Equal To (<=) Operator

We guess the next two operators won’t be much of a problem with you. We will quickly learn how to write less than or equal to in Python.

The less than or equal to operator, denoted by <=, returns True only if the value on the left is either less than or equal to that on the right of the operator.

>>> a=2
>>> a<=a*2

Output: True

6. Equal To or Greater Than – Python (>=) Operator

Likewise, this operator returns True only if the value on the left is greater than or equal to that on the right.

>>> from math import pi
>>> 3.14>=pi

Output: False
Any doubt in Python Comparison Operators? Please Ask us in the comment.

7. Python Equal To (==) Operator

The final two operators we’ll be looking at are equal to (==) and not equal to (!=).

The equal to operator returns True if the values on either side of the operator are equal.

>>> 3=='3'

Output: False

As we know, 3 is an integer, and ‘3’ is a string. Hence, they’re unequal. Let’s take about a couple more examples.

>>> {1,3,2}=={1,2,3}

Output: True

Like you know, a set rearranges itself. This is why this returns True.

>>> 0==False

Output: True

Of course, False has an integer value of 0. Therefore, it returns True.

Beneficial to learn – Python Operator Overloading

Python Interview Questions

8. Python Not Equal Operator (!=) Operator

Finally, we’ll discuss the not equal to operator. Denoted by !=, this does the exact opposite of the equal to operator. It returns True if the values on either side of the operator are unequal.

>>> 3!=3.0

Output: False

>>> 3==3.0

Output: True

Note that the operator <> for the same purpose is no longer functional.

This is all about the Python Comparison Operators.

9. Conclusion: Python Comparison Operators

Concluding for today, we learned six comparison operator in python. These are- python less than, python greater than, Less Than or Equal To, Equal to or greater than, Python Equal To and Python Not Equal Operator. Their functioning is actually very easy to understand, but feel free to ask a doubt or add to the article in the comments. Hope you like the Python Comparison Operators Tutorial. See you again.

See also – 

Python Ternary Operator

Reference for Python

2 Responses

  1. JYOTI SHARMA says:

    >>> (1,2,3)<(1,3,2)

    How does the above expression evaluate to True?
    I believe this is comparing the value in tuples as per their position( like 1<1, 2<3,3<2)

    • DataFlair Team says:

      Hi Jyoti,
      Thanks for the query for Python comparison operator, in tuple comparison for Python, (1,2,3)<(1,3,2) evaluates to True. Let's see how.

      It compares 1 and 1; these are equal. So now, it compares 2 and 3 (second elements in the tuples); since 2 is less than 3, it stops here and returns True. If both values were 2 instead, it would move on to compare the third elements (3 and 2).
      Hope, it will help you!
      Regards,
      DataFlair

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.