Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python Comparison Operators

Operators in Python are used to execute or manipulate certain tasks. Python comparison operators, also known by the name relational operators, are used in comparing two values and to apply conditions respectively.

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

Python Comparison Operators Flowchart 1
Python Comparison Operators Flowchart

Types of Comparison Operators in Python

There are 6 types of comparison operators :

  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 smaller value as compared with the other number or variable. If the provided number or a variable is smaller than the given number or variable. Then, the Less Than operator will return True. Else, it will return false.

a = 10
if (a < 10)
 print("Yes")
 else
  print("No")

Output: No


2. Greater Than ( > )

It 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, the Greater Than operator will return True. Else, it will return false.

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

Output: False


3. Equal To ( == )

This Operator checks for equal values. If the entered value and given value 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 ( != or <> )

It 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 evaluates 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 than or equal to a")

Output: b is either less than or equal to a


6. Greater than or Equal to (>=)

This operator evaluates 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 than or equal to a")

Output: b is either greater than or equal to a


Python Comparison Operators Example

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 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 b"
else:
   print "b is neither greater than  nor equal to b"

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

References