Python Comparison Operators – Learn how to compare values in Python

In this article, we are going to learn about the python 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.

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.

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. If the left operand is indeed smaller, it returns True; otherwise, it returns False. This operator is commonly used in conditional statements and loops to control the flow of a program based on numerical comparisons.

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. If the left operand is larger, it returns True; otherwise, it returns False. This operator is frequently used in conditions and loops to execute code only when certain criteria are met, making it essential for comparison operations.

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. This operator is useful in loops and conditional statements where you want to include boundary values in your comparisons, ensuring comprehensive checks within specified limits.

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 <=.

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 python comparison operators article by TechVidvan, 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.

TechVidvan Team

The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today’s tech industry.