Comparison (Relational) Operators in Python

Comparison operators in Python are binary operators that compare between two numeric values, strings, lists, objects, or two quantities and return a boolean value.

In other words, comparison operators compare values stored in two variables and return a boolean value.

They determine the relationship between them by comparing operands (or values). Therefore, we also know the comparison operators as relational operators in Python.

For example, to know which number is greater, comparing the age of two persons, comparing the price of two items, etc.

We can perform these kinds of comparisons in Python with the help of comparison (relational) operators.

Since the comparison operator is a binary operator, it requires two operands. The outcome of all comparison operators is a boolean value, either True or false.

In Python, relational operators are mainly used to create conditions in decision-making statements or loops in order to perform actions only when a certain condition met. For example:

if(condition_is_true) 
    statement to be executed
# We can implement this statement in Python program like this:
if(x > y):
    print(x)

Types of Comparison Operators in Python


Python supports six types of relational or comparison operators. We have listed them below:

  • Equal (==)
  • Not equal (!=)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)

1. Equal to (==)


This operator evaluates that the value of left and right operands is equal or not. If the left operand’s value is equal to the right operand’s value, the condition becomes true, i.e. the equality operator returns true. Otherwise, returns false.

In other words, the equality operator compares and returns true if two objects are equal to each other. Consider the following example code below.

# Comparison between two numeric values using equal operator.
x = 20
y = 20
print("Returned value of (x == y): ", x == y)

# Comparison between two string values using equal operator.
p = 'My name is John'
q = 'My name is John'
print("Returned value of (p == q): ", p == q)

r = 'Text'
s = 'text'
print("Returned value of (r == s): ", r == s)

# Comparison between two lists using equal operator.
t = [1, 2, 3, 4]
u = [1, 2, 3]
print("Returned value of (t == u): ", t == u)

List1 = [10, 20, 30, 40, 50]
List2 = List1
print("Returned value of (List1 = List2): ", List1 == List2)
Output:
     Returned value of (x == y): True
     Returned value of (p == q): True
     Returned value of (r == s): False
     Returned value of (t == u): False
     Returned value of (List1 = List2): True

a) In the above code, the string “Text” is not equal to the string “text” because they are not exactly the same; one starts with uppercase letter ‘T’ and the other with lowercase letter ‘t’. Therefore, both reference variables r and s are pointing to different objects ‘Text’ and ‘text’.

b) The statement (t == u) returned False because both reference variables t and u are pointing to different lists.

c) The statement (List1 == List2) returned True because both reference variables List1 and List2 are pointing to the same object.

Try It Yourself

x = 50.0
y = 50
print(x == y)

str1 = 'My name is John'
str2 = str1
print(str1 == str2)

a = True
b = 1
print(a == b)

p = False
q = 0
print(p == q)

r = '90'
s = 90
print(r == s)

2. Not equal to (!=)


This operator checks that the left side operand is not equal to the right side operand. If the values on both sides of the operator are not equal to each other, the not equal operator returns true.

In other words, the not equal operator compares and returns true if two objects are not equal else returns false. For example:

x = 50
y = 90
print("Returned value of (x != y): ", x != y)

str1 = 'My name is John'
str2 = str1
print("Returned value of (str1 != str2): ", str1 != str2)

a = True
b = 1
print("Returned value of (a != b): ", a != b)

p = False
q = 0
print("Returned value of (p != q): ", p != q)

r = '50'
s = 50
print("Returned value of (r !=s ): ", r != s)
Output:
      Returned value of (x != y): True
      Returned value of (str1 != str2): False
      Returned value of (a != b): False
      Returned value of (p != q): False
      Returned value of (r !=s ): True

Try It Yourself

List1 = [10, 20, 'Python', '30']
List2 = [10, 20, 'Python', 30]
print(List1 != List2)

Dict1 = {
     1: 'a',
     2: '20'
}
Dict2 = {
    1: 'a',
    2: '20'
}
print(Dict1 != Dict2)

3. Greater than (>)


This operator checks that the value of the left operand is greater than the value of the right operand. If the value on the left side of operator is greater than the value on the right side, the operator returns true. Otherwise, it returns false.

In other words, the greater than (>) operator compares and returns true if the first object is greater than the second object, else returns false. For example:

num1 = 60
num2 = 50
print(num1 > num2)
print(num1 + 20 - 20 > num2 + 20)

n1 = 20
n2 = n1
print(n1 > n2)
Output:
      True
      False
      False

In Python, we can also use the comparison operators (>, <, >=, <=) to compare two strings. Python compares these strings lexicographically using the ASCII value of characters.

Python interpreter compares the ASCII values for each character in both strings, such as the first character of each string, second character of each string, and so on, until we find the character with the maximum ASCII value.

Uppercase letter A is represented in ASCII by the number 65, B by 66, C by 67, and so on. Whereas, lowercase letter a is represented in ASCII code by 97, b by 98, c by 99, and so on.


Let’s take an example program based on the string comparison using relational operators in Python.

str1 = 'Python'
str2 = 'Pythe'
print(str1 > str2)
Output:
      True

In the above code, the statement (str1 > str2) returns True because Python interpreter compares the first two characters (P and P) from both strings.

As they are equal, the second two characters (y and y) are compared in both strings. Since they are also equal, the third two character (t and t) are compared, and so on.

When the interpreter reaches on comparing the fifth character from both strings, then ‘o’ has greater ASCII value than ‘e’, str1 is greater than star2. We can also compare a string against an empty string.

Comparison between Items having different Data types


Python 3 does not allow to compare the items of different data types. In Python 2, the mixed type is permitted. For example, we can compare a string with an integer, but in Python 3, it is not allowed.

For example:

str1 = 'Python'
str2 = 20
print(str1 > str2)
Output:
      TypeError: '>' not supported between instances of 'str' and 'int'

Try It Yourself

str1 = 'Python'
str2 = '20'
print(str1 > str2)

p = True
q = 2
print(q > p)

r = False
s = -1
print(r > s)

List1 = [10, 20, 30]
List2 = [10, 20, 40]
print(List1 > List2)

4. Less than (<)


This operator evaluates whether the value of the left operand is less than the value of the right operand. If the value on the left side of operator is less than the value on the right side, the outcome becomes true.

In other words, the less than operator (<) compares and returns true if the first object is less than the second object else returns false. For example:

num1 = 20
num2 = 20.05
print(num1 < num2)

exp1 = 20 * 20 % 20
exp2 = 20 + 20 / 20
print(exp2 < exp1)
Output:
      True
      False

Try It Yourself

str1 = 'Python'
str2 = 'Text'
print(str1 < str2)

p = True
q = 0
print(q < p)

r = False
s = -2
print(r < s)

List1 = [1, 2, 3]
List2 = [1, 2, 4]
print(List1 < List2)

5. Greater than or equal to (>=)


This operator checks whether the value of the left operand is greater than or equal to the value of the right operand. If the value on the left side of the operator is greater than or equal to the value on the right side, then it returns true. Otherwise, returns false. For example:

num1 = 20.20
num2 = 20.0200
print(num1 >= num2)

x = True + 2
y = False + False + False
print(x >= y)

p = 'abcd'
q = 'abcde'
print(p >= q)
Output:
      True
      True
      False

6. Less than or equal to (<=)


This operator checks whether the value of the left operand is less than or equal to the value of the right operand. If the value on the left side of the operator is less than or equal to the value on the right side, then it returns true. Otherwise, returns false. For example:

num1 = 40.40
num2 = 40.400
print(num1 <= num2)

x = True + 2
y = False + False + False
print(x <= y)

p = 'A'
q = 'B'
print(p <= q)
Output:
      True
      False
      True

Use of Comparison Operators in Decision Making Statement


Let’s write a Python program in which we will use the comparison (or relational) operators in the decision-making statement, such as if-else statement.

Example 1:

# Program to illustrate the use of comparison or relational operators. 
p = 50
q = 60
if(p == q):
    print("p is equal to q")
else:
    print("p is not equal to q")
if(p != q):
    print("p is not equal to q")
else:
    print("p is equal to q")
if(p > q):
    print("p is greater than q")
else:
    print("p is not greater than q")
if(p < q): print("p is less than q") else: print("p is not less than q") if(p >= q):
    print("p is greater than or equal to q")
else:
    print("p is not greater than or equal to q")
if(p <= q):
    print("p is less than or equal to q")
else:
    print("p is not less than or equal to q")
Output:
      p is not equal to q
      p is not equal to q
      p is not greater than q
      p is less than q
      p is not greater than or equal to q
      p is less than or equal to q

Example 2:

# Program to check you are eligible for voting or not
# Using comparison or relational operators.
age = int(input("Enter your age: "))
if(age >= 18):
    print("You are eligible for voting.")
else:
    print("You are not eligible for voting.")
Output:
      Enter your age: 22
      You are eligible for voting.

In this tutorial, we have elaborated on the six types of relational or comparison operators in Python with various examples. Hope that you will have understood the basic points of comparison operators and practiced all programs.
Thanks for reading!!!
Next ⇒ Logical Operators in Python⇐ Prev Next ⇒