Introduction

Python is a powerful object-oriented programming language that is widely used for developing a wide range of applications. One of the most important features of Python is its ability to compare values using relational operators. Relational operators are used to compare the relationship between two values, such as whether they are equal, greater than, less than or not equal to each other. Python provides a set of relational operators that programmers can use to compare values and make decisions based on the results. In this tutorial, we will explore the different types of relational operators in Python and learn how to use them effectively in your programs.

Table of Contents :

  • Relational operators in Python
    • Greater than Operator '>'
    • Less than operator '<'
    • Equal to operator '=='
    • Not equal to operator '!='
    • Greater than or equal to operator '>='
    • Less than or equal to operator '<='

Relational operators in Python

  • Relational operators are also known as comparison operators. 
  • Relational operators performs a comparison between the two operands. 
  • Relational operators return a boolean value True or False as per the result of the comparison.
  • Relational Operators are usually used within conditional statements.
  • In Python we have six relational operators :
    • Greater than Operator '>'
    • Less than operator '<'
    • Equal to operator '=='
    • Not equal to operator '!='
    • Greater than or equal to operator '>='
    • Less than or equal to operator '<='

Greater than operator in Python '>' in Python : 

  • It returns  True  if the value of left operand is greater than the value of right operand.
  • Code Sample :

x = 10
y = 20
res = y > x
print(res)

# Output
# True

# Here the Greater than operator compares x and y
# as 'y is greater than x' it returns True
# The result True is then printed

  • We can use comparison operators to compare more than two values as well.
    x > y > z returns True if x is greater than y, and y is greater than z.
  • Code Sample :

x = 10
y = 20
z = 30
res = z > y > x
print(res)

# Output
# True

Less than operator '<' in Python : 

  • It returns True if the value of left operand is less than the value of right operand.
  • Code Sample :

x = 30
y = 20
res = y < x
print(res)

# Output
# True

# Here the Less than operator compares x and y
# as 'y is less than x' it returns True
# The result True is then printed

  • We can use comparison operators to compare more than two values as well.
    x < y < z returns True if x is lesser than y, and y is lesser than z.
  • Code Sample :

x = 10
y = 20
z = 30
res = x < y < z
print(res)

# Output
# True

Equal to operator '==' in Python : 

  • It returns True if values of both the operands are equal.
  • Code Sample :

x = 10
y = 20
res = y == x
print(res)

# Output
# False 

# Here the Equal to operator compares x and y
# as 'y is not equal to x' it returns False
# The result False is then printed

Not equal to operator '!=' in Python : 

  • It returns True if values of both the operands are not equal.
  • Code Sample :

x = 10
y = 20
res = y != x
print(res)

# Output
# True

# Here the Equal to operator compares x and y
# as 'y is not equal to x' it returns True
# The result True is then printed

Greater than or equal to operator '>=' in Python : 

  • It returns True if the value of left operand is greater than or equal to the value of right operand.
  • Code Sample :

x = 10
y = 20
z = 10 
res = y >= x
resz = z >= x
print(res)
print(resz)

# Output
# True
# True

# Here the Greater than or equal to operator compares x and y
# as 'y is greater than x' it returns True
# The result True is then printed
# as 'z is equal to x' it returns True
# The result True is then printed


Less than or equal to operator '<=' in Python : 

  • It returns True if the value of left operand is less than or equal to the value of right operand.
  • Code Sample :

x = 10
y = 10
z = 20
res = y <= x
resz = z <= x
print(res)
print(resz)

# Output
# True
# False

# Here the Less than or equal to operator compares x and y
# as 'y is equal to x' it returns True
# The result True is then printed
# as 'z is greater than x' it returns False
# The result False is then printed

Prev. Tutorial : Assignment operators

Next Tutorial : Logical Operators