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.
Here at Ibmi Media, we shall look into Python comparison operators and how to use them to compare two values.
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.
1. 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.
For 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.
2. 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.
For 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.
3. 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.
For 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.
4. 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.
For 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.
5. 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.
For 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.
6. 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.
For 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.
This article covers Python Comparison Operators explained with examples. In fact, Python Comparison Operators compare two operands and return a boolean value based on the comparison made.