Comparison operators are used when you have to compare two values. Following comparison operators in python are present:
- > (greater than) operator
- < (less than) operator
- >= (greater than or equal to) operator
- <= (less than or equal to) operator
- == (equal) operator
> operator:
> operator is called as greater than operator. > operator is used when you have to check whether a variable value is greater than a 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 checking whether a variable value is less than a 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 checking whether a variable value is greater than or equal to a 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 checking whether a variable value is less than or equal to a 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 an equal operator. It is used to check whether two values are equal. It returns the result into a 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.