Comparison Operators
Comparison and Relational Operators in Python
In Python, comparison operators are used to compare values and determine the relationship between them. These operators return either True or False based on the comparison.
Relational operators are a subset of comparison operators. While comparison operators include all operators used to compare two values, relational operators specifically compare the order or magnitude of values.
| Operator | Meaning | Type |
|---|---|---|
| < | Less than | Relational |
| > | Greater than | Relational |
| <= | Less than or equal to | Relational |
| >= | Greater than or equal to | Relational |
| == | Equal to | Comparison |
| != | Not equal to | Comparison |
Equal to (==)
Returns True if the values on both sides of the operator are equal, otherwise returns False.
# Equal to (==)
x = 5
y = 5
print(x == y) # Output: True
What is the comparison operator used to check if two values are equal in Python?
Not equal to (!=)
Returns True if the values on both sides of the operator are not equal, otherwise returns False.
x = 5
y = 10
print(x != y) # Output: True
Greater than (>)
Returns True if the value on the left side of the operator is greater than the value on the right side, otherwise returns False.
x = 10
y = 5
print(x > y) # Output: True
Greater than or equal to (>=)
Returns True if the value on the left side of the operator is greater than or equal to the value on the right side, otherwise returns False.
x = 10
y = 10
print(x >= y) # Output: True
Which comparison operator is used to check if one value is greater than or equal to another in Python?
Less than (<)
Returns True if the value on the left side of the operator is less than the value on the right side, otherwise returns False.
x = 5
y = 10
print(x < y) # Output: True
Less than or equal to (<=)
Returns True if the value on the left side of the operator is less than or equal to the value on the right side, otherwise returns False.
x = 5
y = 5
print(x <= y) # Output: True
Review: Fill in the Blanks
The operator (==) checks if the values on both sides are . If they are, it returns True; otherwise, it returns False. For example, if x is 5 and y is 5, then x == y will output .
The (!=) operator serves to determine if the values on both sides are . This operator returns True if the values differ. For instance, if x is 5 and y is 10, the expression x != y will yield .
The operator (>) checks if the value on the left side is than the value on the right side. If this condition is met, it returns True; otherwise, it returns False. Similarly, the (<) operator returns True if the left value is than the right value.
Complete! Ready to test your knowledge?
Comparison Operators
- Comparison and Relational Operators in Python
- Equal to (==)
- Not equal to (!=)
- Greater than (>)
- Greater than or equal to (>=)
- Less than (<)
- Less than or equal to (<=)