Home » Relational Operators in Python

Relational Operators in Python

by Online Tutorials Library

Relational Operators in Python

In this tutorial, we will discuss how different relational operators can be used in Python programs.

The relational operators are also known as comparison operators, their main function is to return either a true or false based on the value of operands.

Following are the relational operators-

  1. >
  2. <
  3. ==
  4. !=
  5. >=
  6. <=

Let’s start with the first one-

1. Greater or less than (>, <)

The program given below illustrates how it can be done-

Output:

The sum of a and b is 30  True  False  False  True  

Explanation-

It’s time to have a look at the explanation of the above program-

  1. In the first step, we have initialized the values of the variables of a and b is 20 and 10.
  2. In the next step, we have calculated the sum of a and b.
  3. Finally, we printed the values evaluated based on greater than or less than.
  4. If a is greater than b, it gives the output as ‘True’, else it gives ‘False’ and vice versa.
  5. On executing the given program, the desired output is displayed.

2. Equals to (==)

The equals to ‘==’ operator return a True value if values stored in two variables are the same.

The following program illustrates its usage

Output:

The sum of a and b is 30  The difference of d and b is 20  True  False  True  True  

Explanation-

Let’s understand what happened in the above program-

  1. In the first step, we have initialized the values of the variables of a and b is 20 and 10.
  2. In the next step, we have calculated the sum of a and b and the difference of d and b.
  3. Finally, we printed the values evaluated based on greater than, less than, and equals to.
  4. If c is equal to d, it gives the output as ‘True’, else it gives ‘False’ and vice versa.
  5. On executing the given program, the desired output is displayed.

3. Not Equal to (!=)

The not equals to ‘!=’ operator returns a True value if values stored in two variables are different.

Consider the program given below-

Output:

The sum of a and b is 30  The difference of d and b is 20  True  True  False  True  

Explanation-

Let’s have a glance at the explanation of the above program-

  1. In the first step, we have initialized the values of the variables of a and b is 20 and 10.
  2. In the next step, we have calculated the sum of a and b and the difference of d and b.
  3. Finally, we printed the values evaluated based on greater than, less than, equals to, and not equals to.
  4. If a is not equal to b, it gives the output as ‘True’, else it gives ‘False’ and vice versa.
  5. On executing the given program, the desired output is displayed.

Finally, we will discuss the last relational operator which is greater than equal to or less than equal to.

4. Greater than or less than equal to (>=, <=)

The greater than equal to returns ‘True’ if the value in any of the two variables is greater than or equal to the second one.

The ‘lesser than equal to’ works in a similar way.

Let us see how it can be used in a Python program,

Output:

The sum of a and b is 30  The difference of d and b is 20  True  True  True  True  

Explanation-

The program given below illustrates how it can be done-

  1. In the first step, we have initialized the values of the variables of a and b is 20 and 10.
  2. In the next step, we have calculated the sum of a and b and the difference of d and b.
  3. Finally, we printed the values evaluated based on greater than, less than, equals to, not equals to, greater than equal to, and less than equal to.
  4. If a is greater than equal to b, it gives the output as ‘True’, else it gives ‘False’ and vice versa.
  5. On executing the given program, the desired output is displayed.

Conclusion

In this tutorial, we learned how relational operators can be used in a Python program.


You may also like