Python Comparison Operators

Python Comparison Operators called Relational operators, and they are mostly used either in IF Statements or Python Loops. Comparison Operators in Python are usually used to check the relationship between two variables. If the relation is true, it returns TRUE, and if the relation is false, then it will return output as FALSE. The below table shows all the Python Comparison Operators.

Python Relational OperatorsUsageDescriptionExample
>a > ba is greater than b7 > 3 returns True (1)
<a < ba is less than b7 < 3 returns False (0)
>=a >= ba is greater than or equal to b7 >= 3 => True (1)
<=a <= ba is less than or equal to b7 <= 3 return False (0)
==a == ba is equal to b7 == 3 returns False (0)
!=a != ba is not equal to b7 != 3 returns True(1)

Python Comparison Operators Example

In this Python Relational Operators example, we declared two variables a and b and their values are 9 and 4. Next, we perform various comparison operations in Python. I mean, we checked these values against each comparison or relational operator in Python

>>> a = 9
>>> b = 4
>>> print(" The Output of 9 > 4 is : ", a > b )
>>> print(" The Output of 9 < 4 is : ", a < b )
>>> print(" The Output of 9 <= 4 is : ", a <= b )
>>> print(" The Output of 9 >= 4 is : ", a >= b )
>>> print(" The Output of 9 Equal to 4 is : ", a == b )
>>> print(" The Output of 9 Not Equal To is : ", a != b )
Python Comparison Operators 1

Use Python Comparison Operators in IF Statement

Using Python Relational operators in If statements. For this Python example, We use two variables x and y, and their values are 10 and 25. Here, we use these two variables in If Statement to check the conditions using one of the comparison operator.

>>> x = 10
>>> y = 25
>>> if x >= y:
	print(" x value is Greater than or Equal to y ")
    else:
	print(" x value is Less than or Equal to y ")
x value is Less than or Equal to y

In this Python realtional operator example, We assigned 2 integer values x, y, and assigned the values 10 and 25.

>>> x = 10
>>> y = 25

If Condition: If x is greater than or equal to y, then the first print statement will execute

print(" x value is Greater than or Equal to y ")

If the first condition fails, then the second one executes.

print(" x value is Less than or Equal to y ")

Here, 10 is neither greater nor equal to 25. So, the Second statement printed. Refer to Python IF Statements and Python Loops in Python.