Comparison operator is used to compare the values of two variables and based on that it will provide you the result either as true if the condition satisfied or false if the condition is not satisfied. In this article you’ll learn about Python comparison operators and how to use them to compare two values.
Comparison operators in Python
There are total 6 different comparators operators that are mentioned below which will be discussed in this article along with the syntax and examples. In other terms they are also known as relational operators because they are used to find the relation between two different variables.
Greater than (>)
The “greater than” operator is one of Python’s comparison operators that is used to compare the values of two variables by putting a “>” between them. If the value of the variable which is on the left side is greater than on the right side, then the output is true otherwise it will be false.
Example:
x = 10 y = 5 x > y Output: True x = 5 y = 10 x > y Output: False |
In the above example of Greater than operator we are using two variable “x” and “y” and assigning them values as x = 10 and y = 5. When the value of x is greater than the value of y the result is True otherwise it will show False in x = 5 and y = 10 case.
Less than (<)
The “Less than” operator is one of Python’s comparison operators that is used to compare the values of two variables by putting a “<” between them. If the value of the variable which is on the left side is less than on the right side, then the output is true otherwise it will be false.
Example:
x = 5 y = 10 x < y Output: True x = 10 y = 5 x < y Output: False |
As you can see example of less than operator, we are using two variable “x” and “y” and assigning them values x = 5 and y = 10. When the value of x is less than the value of y the result is True otherwise it will show False in case of x = 10 and y = 5.
Greater than or equal to (>=)
This operator combines two different operators together which are the greater than “>” and equals to “=”. This means that if the value of the variable is either greater or equals to the value of the variable which is on the right side then the value will be true otherwise it will be false.
Example:
x = 10 y = 5 x >= y Output: True x = 10 y = 10 x >= y Output: True x = 5 y = 10 x >= y Output: False |
In above example of “greater than or equal to” since the values of variables x and y are equal, the answer returned is True or when value of x is greater than the value y, otherwise it returns False. As a practice, execute the above code with just the “greater-than ” operator and see the answer.
Less than or equal to (<=)
The less-than equal-to operator combines the equal-to and less-than comparison operators in Python. The operator “less than equal to” return True if the value on the left right-hand side is smaller than or equal to the value on the right.
Example:
x = 5 y = 10 x <= y Output: True x = 10 y = 10 x <= y Output: True x = 10 y = 5 x <= y Output: False |
In above example of “less than or equal to” since the values of variables x and y are equal, the answer returned is True or when value of x is less than the value of y, otherwise it returns False. As a practice, execute the above code with just the “less-than” operator and see the answer.
Equal to (==)
The equal to operator in Python returns True only if two variables under consideration are equal, otherwise the result is False. Two equal marks, i.e., “==”, signify the “equal to” operator.
Example:
x = 10 y = 10 x == y Output: True x = 5 y = 10 x == y Output: False |
As expected, since x is equal to y (with values 10 and 10), the answer returned is True. Otherwise, it returns False in case of x = 10 and y = 5.
Not Equal to (!=)
The not equal to operator in Python returns True only if two variables under consideration are not equal, otherwise the result is False. “!=”, signify the “not equal to” operator.
Example:
x = 10 y = 5 x != y Output: True x = 10 y = 10 x == y Output: False |
Now in this example of “not equal to” operator in python only return True when the value of x is not equal to value of y. However, when both the value of x and y are same it will return False.
Conclusion
Comparison is one the python operators that is used to compare the values of two variables and provides you the result based on that. There are total 6 different comparison that we have discussed in this article.