Comparison operators in python

Comparison operators are used when you have to compare two values. Following comparison operators in python are present:

  1. > (greater than) operator
  2. < (less than) operator
  3. >= (greater than or equal to) operator
  4. <= (less than or equal to) operator
  5. == (equal) operator

> operator:

> operator is called as greater than operator. > operator is used when you have to check whether variable value is greater than specific value.

Example:

marks = 45
if marks > 40:
    print('You passed the exam.')
else:
    print('You failed the exam.')

Output:

You passed the exam.

< operator:

< operator is called as less than operator. < operator is used to check whether variable value is less than specific value.

Example:

marks = 35
if marks < 40:
    print('You failed the exam.')
else:
    print('You passed the exam.')

Output:

You failed the exam.

>= operator:

>= operator is called as greater than or equal to operator. >= operator is used to check whether variable value is greater than or equal to specific value.

Example:

if marks >= 40:
    print('You passed the exam.')
else:
    print('You failed the exam.')

Output:

You passed the exam.

<= operator:

<= operator is called as less than or equal to operator. <= operator is used to check whether variable value is less than or equal to specific value.

Example:

marks = 39
if marks <= 39:
    print('You failed the exam.')
else:
    print('You passed the exam.')

Output:

You failed the exam.

== operator:

== is called as equal operator. It is used to check whether two values are equal. It returns result into boolean value i.e either True or False.

Example:

marks = 40
if marks == 40:
    print('You passed the exam.')
else:
    print('You failed the exam.')

Output:

You passed the exam.

Conclusion:

In this article, you learned when and how to use comparison operators in python. You also learned the different operators present in comparison operators in python.

If you have problems while implementing code, you can ask me through the comment section.

See Also:

Arithmetic operators in python

Logical operators in python

Leave a Reply

Your email address will not be published. Required fields are marked *