Python Comparison Operators

Comparison Operators Thumbnail

Operators are symbols in programming languages that perform the operation on the operands and decide the operation type. For instance, the equal to ( == ) operator checks for equal values and returns True if they are equal, and False otherwise, whereas the not equal ( !=) operator does the exact opposite.

In Python, operators can be divided into several categories based on their functionality such as Arithmetic Operators to perform mathematical operations, Comparison Operators to compare two values, Logical Operators to combine multiple conditions and evaluate whether they are True or False, Bitwise Operators to perform operations on the individual bits of a binary number, etc. 

In this tutorial, we will learn about all types of Comparison Operators and demonstrate them with examples to understand their working.

Comparison Operators

Python comparison operators, also known as relational operators, are used in comparing two values and applying conditions respectively. 

Here there can be two possible outputs, either boolean True or False.

Python Comparison Operators Flowchart
Python Comparison Operators Flowchart

There are several types of comparison operators, each performing a unique operation, let’s look at them one by one.

Types of Comparison Operators in Python

There are 6 types of comparison operators in Python:

  1. Less Than ( < )
  2. Greater Than ( > )
  3. Equal To ( == )
  4. Not Equal ( != )
  5. Less Than or Equal To ( <= )
  6. Greater Than or Equal To ( >= )
Python Comparison Operators
Python Comparison Operators

1. Less Than ( < )

It is used to check for the smaller value or variable containing a smaller value as compared with the other number or variable. It will return True if the provided number or a variable is smaller than the given number or variable, otherwise, it will return False.

a = 10
if (a < 10):
 print("True")
else:
  print("False")

Output:

False

2. Greater Than ( > )

Python Greater Than operator is used to check for the greater value or variable containing greater value as compared with the other number or variable. If the provided number or a variable is greater than the given number or variable then it will return True, else, it will return false.

a = 10
if (a > 10):
 print("True")
else:
  print("False")

Output:

False

3. Equal To ( == )

Python Equal To operator checks for equal values. It compares elements and if they are equal then it will return True else False.

a = 10
b = 20
if (a == b):
 print("True")
else:
 print("False")

Output:

False

4. Not Equal ( != )

Python Not Equal operator is 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.

print(3!=3.0)

Output:

False

5. Less than or Equal to (<=)

This operator executes to True only if the value on the left is less than or equal to that on the right.

a = 15
b = 5
if(b <= a):
 print("b is either less or equal to a")

Output:

b is either less or equal to a

6. Greater than or Equal to (>=)

This operator executes to True only if the value on the left is greater than or equal to that on the right.

a = 5
b = 15
if(b >= a):
 print("b is either greater or equal to a")

Output:

b is either greater or equal to a

Python Comparison Operators Example

Let’s write the code to demonstrate each comparison operator we have seen earlier.

a = 10
b = 5
c = 0
 
if a == b:
   print("a is equal to b")
else:
   print("a is not equal to b")
 
if a != b:
   print("a is not equal to b")
else:
   print("a is equal to b")
 
if a < b:
   print("a is less than b")
else:
   print("a is not less than b")
 
if a > b:
   print("a is greater than b")
else:
   print("a is not greater than b")
 
a = 6;
b = 15;
if a <= b:
   print("a is either less than or equal to b")
else:
   print("a is neither less than nor equal to b")
 
if b >= a:
   print("b is either greater than or equal to a")
else:
   print("b is neither greater than nor equal to a")

Output:

a is not equal to b
a is not equal to b
a is not equal to b
a is not less than b
a is greater than b
a is either less than or equal to b
b is either greater than or equal to b

Summary

During this tutorial, we have discussed 6 operators that programmers used to compare values, also called Python comparison operators, let us summarize them one by one.

  1. Less than ( < ) returns True if the value of the left operand is lesser than the value of the right operand.
  2. Greater than ( > )  returns True if the value of the left operand is greater than the value of the right operand.
  3. Equal to ( == ) returns True if the value of two operands is equal. 
  4. Not equal ( != )  is the exact opposite of the equal to operator.
  5. Less than or equal To (<=) returns True if the value on the left is less or equal to that on the right.
  6. Greater than or equal to (>=) returns True if the value on the left is greater than or equal to that on the right.

Hope you now have a clear understanding of Python comparison operators.

Reference

https://docs.python.org/3/reference/expressions.html#comparisons