Python Comparison Operators – Learn how to compare values in Python

In this article, we are going to learn about the comparison operators. Python comparison operators are also known as relational operators. It’s very important to understand everything about them as you will be using them a lot when writing codes.

Keeping you updated with latest technology trends, Join TechVidvan on Telegram

Python Comparison Operatorscomparison operators in python

There are many types of Python comparison operators. They include Less than(<), Greater than(>), Less than or equal to(<=), Greater than or equal to(>=), Equal to(==) and Not equal to (!=). The comparison operators return True or False by evaluating the expression.

Before moving ahead, take an insight into TechVidvan’s Python operators to ease your programming.

Types of Python Comparison Operators

1. Less than (<)

The first comparison operator we will see is the less-than operator. It’s denoted by ‘<’ and it is used to check if the left value is less than the right value or not.

8<10

Output:

True

8 is less than 10 so, it returns True.

20<20

Output:

False

It results in False as 20 is equal to 20 and not less than 20.

Float values can also be evaluated with these operators.

5.2<5.4

Output:

True

Also, you can try this operator with strings.

‘a’ < ‘b’

Output:

True
‘abcd’ < ‘abca’

Output:

False

A fascinating thing about this is we can also compare tuples using this operator. Let’s see them in action.

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

Output:

True
(1,2,3) < (1,2,3)

Output:

True

This also works on lists. It compares list index by index.

[1,2,3] < [1,2,4]

Output:

True
[5,9,10] < [5,8,5]

Output:

False

This doesn’t work on dictionaries.

2. Greater than (>)

Let’s see the Python greater than symbol. It’s denoted by ‘>’ symbol and it checks whether the value on the left side is greater than the right side.

0.5 > False

Output:

True

Since False is considered as zero and 0.5 is greater than 0.

‘a’>’A’

Output:

True

In the case of strings, Python compares the ASCII values of the characters. Here, ‘a’ ASCII value is 97 and ‘A’ ASCII value is 65 that’s why ‘a’ is greater than ‘A’.

3. Less than or equal to (<=)

Now you get the idea of comparison operators, we can quickly understand the code with examples. The less than or equal to operator, denoted by ‘<=’ returns True when the left side operand is either less than or equal to the right side operand.

5<=10

Output:

True
10<=10

Output:

True

As you see, in just less than operator 10<10 would give us false. But for conditions when we also need to check for equality, we will use <=.

Any doubts in Python comparison operators till now? Mention in the comment section.

4. Greater than or equal to (>=)

The greater than or equal to operator is just like less than or equal to. The only difference is that it checks that the left side value should be greater than or equal to the right side value.

14>=10

Output:

True
4>=5

Output:

False

5. Equal to (==)

The final two operators are equal to (==) and not equal to (!=)

The equal to operator will return True when both the values on either side of the operator are equal. You can compare integers, float, and also strings.

23 == 23

Output:

True
2 == 2.0

Output:

True
13 == ‘13’

Output:

False

Here, 13 is an integer value and ‘13’ is a string, that’s why they are not equal.

“TechVidvan” == “TechVidvan”

Output:

True

You can also compare lists and set.

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

Output:

True

To understand this, you should know about the set. Python set data structure holds unique values and they rearrange themselves in a sorted order.

6. Not equal to (!=)

The not equal to operator (!=) is opposite to the equal to operator. It returns true when the values on either side are unequal to each other.

“26” != 26

Output:

True
“Python” != “Python”

Output:

False

Note: Python also had <> operator which had the same purpose as not equal to operator but it is now been removed from Python 3 versions.

Summary

In today’s article, we saw the six comparison operators of Python named as less than, greater than, less than or equal to, greater than or equal to, equal to and not equal to operator.

These operators are self-explanatory and very easy to understand. Later on, you will use these operators a lot in decision-making statements.

Now its time to know another type of operators which is Python bitwise operators.

If you find TechVidvan’s Python comparison article helpful, do recommend us on facebook.

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.