Comparison Operators in Python
Operators are symbols that are used to perform operation on variables and values. In this article we will discuss comparison operators in Python.
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:
- Arithmetic Operators in Python
- Bitwise Operators in Python
- Ternary Operators in Python
- Assignment Operators in Python
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
Best-suited Python courses for you
Learn Python with these high-rated online courses
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
Output
True
Because 3 is less than 7. Hence, the expression is true.
3 < 3
Output
False
Because 3 cannot be less than itself. Hence, the expression is false.
'Sam' < 'sam'
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)
Output
True
(1,3,2) < (1,2,3)
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
Output
True
10 > 20
Output
False
3,4,5 > 3,4,5.0
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
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 = 9a <= a + 9
Output
True
x = 20x <= 20 - x
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 = 9b = 9.0
a >= b
Output
True
from math import pi3.14 >= pi
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 = 9b = 9.0
a == b
Output
True
3 == '3'
Output
False
{1,2,3} == {1,3,2}
Output
True
1.0 == True
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 = -9b = 9.0
a != b
Output
True
7 != '7'
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.
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