Different Comparison operators in Python

Comparison operators, also known as relational operators in Python, compare the values on either side of them and returns a boolean value. They tell whether a statement is True or False according to the condition. In this tutorial, we will discuss 6 different types of comparison operators in Python.

List of comparison operators in Python

Greater than

Denoted by >, the greater than operator checks if the value on the left side is greater than the value on the right side. It returns True if the condition is satisfied, otherwise returns False.

Input:

x = 5
y = 10
res = x > y
res1 = y > x
print (res)
print (res1)

Output:

FALSE
TRUE

Less than

The less than operator is denoted by < sign and compares the values present on either side. If the value present on the left side is smaller than the value on the right side, it returns True otherwise it returns False.

Input:

x = 5
y = 10
res = x < y
res1 = y < x
print (res)
print (res1)

Output:

TRUE
FALSE

If you compare two different data types, for example, int (5) and float (5.0), both greater than and less than operator will return False as both values are equal.
And when comparing strings like, “Nick” and “nick”, the operators compare their ASCII values. Since the ASCII value of “A” is 65 and “a” is 97, “nick” is greater than “Nick”.

Equal to

This operator is denoted by == and it returns True if both the values present on either side are equal.

Input:

x = 5
y = 5
z =5’
res = x == y
res2 = x == z
print (res)
print (res2)

Output:

TRUE
FALSE

This operator returns False when x and z are compared and that is because x is an integer and z is a string. Hence, they are unequal.

Not equal to

Symbolic representation of Not equal to operator is != and it returns True if one value is not equal to the other present in the condition.

Input:

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

Output:

FALSE

Greater than or equal to

This operator (>=) only returns True if the value on the left side is greater or equal to the value on the right side.

Input:

x = 5
y = 5
z = 10
res = x >= y
res2 = x >= z
print (res)
print (res2)

Output:

TRUE
FALSE

Less than or equal to

The last operator in the list is less than or equal to (<=). It compares the values and returns True if the value on the left side is smaller than or equal to the value to the value on the right side.

Input:

x = 5
y = 5
z = 10
res = x <= y
res1 = x <= z
print (res)
print (res1)

Output:

TRUE
TRUE

We can also compare Tuples with these operators. The Tuple with more elements will be greater and if both have the same number of elements then the operator compares elements with each other.

Input:

tup1 = (1,2,3)
tup2 = (1,2,3)
tup3 = (1,2)
tup4 = (1,5,3)
print (tup1 == tup2)
print (tup3 = tup4)

Output:

In the 3rd case, the tup1 and tup4 have the same number of elements. Then, the operator compares elements. It compares the first element (1 and 1). Since both are equal, it moves to the second element (2 and 5). Now that 5 is greater than 2, it stops here and returns False.

Closing Thoughts

We discussed 6 different types of Comparison operators in Python. The comparison operators compare the value and return a boolean value. One can read about other Python concepts here.