Python Relational Operators

Python Relational Operators

A relational operator is used to compare two values. Based on the operator and values, the operator returns either True or False.

In Python, there are six Relational Operators. They are

SymbolNameExampleDescription
==Equal tox == yReturns True if x and y have same value, else returns False.
>Greater thanx > yReturns True if value of x is greater than value of y, else returns False.
<Less thanx < yReturns True if value of x is less than value of y, else returns False.
!=Not equal tox != yReturns True if x and y do not have same value, else returns False.
>=Greater than or equal tox >= yReturns True if value of x is greater than or equal to value of y, else returns False. Equivalent to ( x>y or x==y )
<=Less than or equal tox <= yReturns True if value of x is less than or equal to value of y, else returns False. Equivalent to ( x>y or x==y )

Equal to Operator

Equal to operator == takes two operands and returns boolean value of True, if both the operands are equal in value, else it returns False.

In the following example, we will take two integer values such than both are equal in value, and check the result of equal to operator with these two integers provided as operands to the equal to operator.

Python Program

x = 41
y = 41
result = x == y
print(result)
Run

Output

True

In the following example, we will take two integer values such than both are not equal in value, and check the result of equal to operator with these two integers provided as operands to the equal to operator.

Python Program

x = 41
y = 63
result = x == y
print(result)
Run

Output

False

Greater than Operator

Greater than operator > takes two operands and returns boolean value of True, if the left operand is greater than the right operand in value, else it returns False.

In the following example, we will take two integer values such that left operand is greater than the right operand, and check the result of greater than operator.

Python Program

x = 87
y = 63
result = x > y
print(result)
Run

Output

True

In the following example, we will take two integer values such that left operand is less than the right operand, and check the result of greater than operator.

Python Program

x = 41
y = 63
result = x > y
print(result)
Run

Output

False

Less than Operator

Less than operator < takes two operands and returns boolean value of True, if the left operand is less than the right operand in value, else it returns False.

In the following example, we will take two integer values such that left operand is less than the right operand, and check the result of less than operator.

Python Program

x = 41
y = 63
result = x < y
print(result)
Run

Output

True

In the following example, we will take two integer values such that left operand is greater than the right operand, and check the result of less than operator.

Python Program

x = 92
y = 63
result = x < y
print(result)
Run

Output

False

Not Equal to Operator

Not Equal to operator != takes two operands and returns boolean value of True, if both the operands are not equal in value, else it returns False.

In the following example, we will take two integer values such that both are not equal in value, and check the result of not equal to operator with these two integers provided as operands to the not equal to operator.

Python Program

x = 92
y = 63
result = x != y
print(result)
Run

Output

True

In the following example, we will take two integer values such that both are equal in value, and check the result of not equal to operator with these two integers provided as operands to the not equal to operator.

Python Program

x = 63
y = 63
result = x != y
print(result)
Run

Output

False

Greater than or Equal to Operator

Greater than or Equal to operator >= takes two operands and returns boolean value of True, if the left operand is greater than or equal to the right operand in value, else it returns False.

In the following example, we will take two integer values such that left operand is greater than or equal to the right operand, and check the result of greater than or equal to operator.

Python Program

x = 97
y = 63
result = x >= y
print(result)
Run

Output

True

In the following example, we will take two integer values such that left operand is less than the right operand, and check the result of greater than or equal to operator.

Python Program

x = 42
y = 63
result = x >= y
print(result)
Run

Output

False

Less than or Equal to Operator

Less than or Equal to operator <= takes two operands and returns boolean value of True, if the left operand is less than or equal to the right operand in value, else it returns False.

In the following example, we will take two integer values such that left operand is less than the right operand, and check the result of less than or equal to operator.

Python Program

x = 42
y = 63
result = x <= y
print(result)
Run

Output

True

In the following example, we will take two integer values such that left operand is greater than the right operand, and check the result of less than or equal to operator.

Python Program

x = 92
y = 63
result = x <= y
print(result)
Run

Output

False

Summary

In this tutorial of Python Examples, we learned about Relational Operators in Python using examples.