Comparison operators are used to comparing the values and determines the relation between them. Depending on their relation, we can make certain decisions. These operators are known as relational operators. For example, we may compare the marks of two students, or the age of two persons, and so on. These comparisons can be made with the help of relational operators.
An expression such as a<b or x<20 containing a relational operator is termed as a relational expression. The value of a relational expression is either true or false. For example, if x=5, then x<10 returns true while a>10 returns false.
Python Comparison Operators
Python supports the following comparison operators.
- Equal operator (=)
- Not equal operator (!=)
- Greater than operator (>)
- Less than operator (<)
- Greater than or equal to the operator (>=)
- Less than or equal to (<=)
For example, assuming a =10 and b=20, we compare these values using the following comparison operation.
1) The equal operator returns true if two values are exactly equal.
print(a==b)
Output:
False
2) The Not equal operator returns true if two values are not equal.
print(a!=b)
Output:
True
3) Less than operator returns true if the operand's value on the left side of the operator is less than the value on its right side.
print(a<b)
Output:
True
4) The Greater than operator returns true if the operand's value on the left side of the operator is greater than the value on its right side.
print(a>b)
Output:
False
5) The Greater than or equal to operator returns true if the value in the operand on the left side of the operator is either greater than or equal to the value on its right side
print(a>=b)
Output:
False
6) Less than or equal to operator returns true if the operand's value on the left side of the operator is either less than or equal to the value on its right side.
print(a<=b)
Output:
True
Leave a comment