Comparison Operators in Python

Comparison Operators in Python

3 mins read1.8K Views Comment
clickHere
Updated on Nov 22, 2022 19:28 IST

Operators are symbols that are used to perform operation on variables and values. In this article we will discuss comparison operators in Python.

2022_07_Comparison-Operator-in-Python.jpg

In this article, we are going to talk about yet another type of operator in Python – the Comparison operator. We’ll learn how the comparison operators perform operations in Python through examples. We have also discussed the following Python operators in previous articles:

We will be covering the following sections today:

What are Comparison Operators?

Comparison operators in Python, also called relational operators, are used to compare two operands. They return a Boolean True or False depending on whether the comparison condition is true or false.

The following table describes the different comparison operators in Python with their names, descriptions, and syntax –

Operator Name Syntax
< Less than a < b
> Greater than a > b
<= Less than or equal to a <= b
>= Greater than or equal to a >= b
== Equal to a = (b+c)
!= Not equal to a != b

Must Read: What is Python?

Must Check: Python Online Course & Certification

Types of Comparison Operators in Python

Below, we are going to look at the Python implementation of each comparison operator one by one:

Less than

This operator is simply denoted by the “<“ sign. Returns True if the left operand is less than the right operand.

 
3 < 7
Copy code

Output

True

Because 3 is less than 7. Hence, the expression is true.

 
3 < 3
Copy code

Output

False

Because 3 cannot be less than itself. Hence, the expression is false.

 
'Sam' < 'sam'
Copy code

Output

True

In this case, the result is true because the ASCII code for ‘S’ is 83 which is less than the ASCII code for ‘s’ which is 115. Rest of the characters are same hence only this makes a difference.

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

Output

True

 
(1,3,2) < (1,2,3)
Copy code

Output

False

NOTE: We can compare tuples having only the same type of data.

Must Read: What is Perfect Square?

Must Read: Rock Paper Scissors Game in Python

Greater than

This operator is simply denoted by the “>” sign. Returns True if the left operand is greater than the right operand.

 
13 > 9
Copy code

Output

True

 
10 > 20
Copy code

Output

False

 
3,4,5 > 3,4,5.0
Copy code

Output

(3, 4, True, 4, 5.0)

In this case, (5 > 3) is considered an expression and since this is true, we get the above output.

 
0.5 > False
Copy code

Output

True

Boolean False stands for 0. So, 0.5 > 0. Hence, the output is true.

Must Read: Type Casting in Python

Must Read: Literals in Python

Less than or equal to

This operator is simply denoted by the “<=” sign. Returns True if the left operand is either less than or equal to the right operand.

 
a = 9
a <= a + 9
Copy code

Output

True

 
x = 20
x <= 20 - x
Copy code

Output

False

Must Read: How to Convert Celsius to Fahrenheit in Python

Must Read: Abstraction in Python

Greater than or equal to

This operator is simply denoted by the “>=” sign. Returns True if the left operand is either greater than or equal to the right operand.

 
a = 9
b = 9.0
a >= b
Copy code

Output

True

 
from math import pi
3.14 >= pi
Copy code

Output

False

Must Read: Validating Passwords using Python Regex Match

Must Read: Python if-else statement

Equal to

This operator is simply denoted by the “==” sign. Returns True if the left operand is exactly equal to the right operand.

 
a = 9
b = 9.0
a == b
Copy code

Output

True

 
3 == '3'
Copy code

Output

False

 
{1,2,3} == {1,3,2}
Copy code

Output

True

 
1.0 == True
Copy code

Output

True

Must Read: Recursion Functions in Python

Must Read: Dice Roll Generator in Python

Not equal to

This operator is simply denoted by the “!=” sign. Returns True if the left operand is not equal to the right operand.

 
a = -9
b = 9.0
a != b
Copy code

Output

True

 
7 != '7'
Copy code

Output

False

Must Read: Python Program to check Leap Year

Must Read: Python Program to swap two variables

Conclusion

In this article, we have discussed one of the operators in python i.e. comparison operators in python with help of examples.

To learn more about Python and practice Python programming, you can explore related articles here.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio

Comments