Python Comparison Operators

COMPARISON OPERATORS IN PYTHON

Now, we are going to have a look at some of the comparison operators (Python Relational operators). Basically, as the name suggests, these operators are going to be used for comparison purposes, like for checking if two values are equal or not, or if one value is greater than the other, and some other such comparisons. Once we have a look at some of the examples, we would have a broader idea about using the comparison operators.

Have a look at the below table, where you can find the comparison operator symbols, with some descriptions, and examples. For the below examples, the value of x is considered to be 5

Python Comparison Operators

OperatorDescriptionExample
==This compares the two values and checks if they are equal.x == 5. This compares the value of 5 with 5. Printing this will give True as output if the values are equal, and False otherwise.
As value of x is 5, the output is going to be True.
!=This compares the two values and checks if they are not equal.x!=4. This compares the value of x with 4. printing this will give True as output, as x has value 5 and 5 is not equal to 4.
So, if the two values are not equal, it will return True, and False otherwise.
>=This compares the two values and checks if one value is greater than or equal to the other value.x>=7. This compares the value of x with 7. printing this will give False as output, as x has value 5 and 5 is not greater than or equal to 7.
So, if one value is greater than or equal to the other value, we get True, and False otherwise.
<=This compares the two values and checks if one value is less than or equal to another value.x<=6. This compares the value of x with 6. printing this will give True as output, as x has value 5 and 5 is less than 6.
So, if one value is less than or equal to the another, the output is True, and False otherwise.
>This compares the two values and checks if one value is greater than the other.x>2. This compares the value of x with 2. printing this will give True as output, as x has a value greater than 2.
So, if one value is greater than the other, the output is True, and False otherwise.
<This compares the two values and checks if one value is less than the other.x<10. This compares the value of x with 10. printing this will give True as output, as x has value less than 10.

So, if some value is less than the other value, the output is True, and False otherwise.

Now, let’s have a look at a simple program, which demonstrates about the different comparison operators.

As you can see in the above program, we are performing different comparison operations with the value. The comparisons are very readable, and useful as well. Just by reading the comparisons, one can identify the outputs as well, since they are going to be True or False. So, Let’s have a look at the output for the above program.
True
True
False
False
True
True

As you can see, we are getting the outputs accordingly. So, whenever we need to perform some comparisons between the values, we can make use of the different comparison operators.