Different Comparison operators in Python
Tags: python, programming, tutorial, beginners
Published: 2022-06-29 11:03:56

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](#greater-than)
*   [Less than](#less-than)
*   [Equal to](#equal-to)
*   [Not equal to](#not-equal-to)
*   [Greater than or equal to](#greater-than-or-equal-to)
*   [Less than or equal to](#less-than-or-equal-to)

## 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)

<span id="section2"></span>

#### 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”.

<span id="section3"></span>  

## 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

<span id="section4"></span>

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)

<span id="section5"></span>

#### 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)

<span id="section6"></span>

#### 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:

<pre><code="language-py">tup1 = (1,2,3)
tup2 = (1,2,3)
tup3 = (1,2)
tup4 = (1,5,3)
print (tup1 == tup2)
print (tup3 <= tup1)
print (tup1 >= tup4)</code="language-py"></pre>

#### 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](https://realpython.com/python-operators-expressions/) in Python. The comparison operators compare the value and return a boolean value. One can read about other Python concepts [here](https://flexiple.com/python/python-exponents).