Comparison Operators
Comparison 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.
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
Comparison Operators
- Comparison Operators in Python
- Equal to (==)
- Not equal to (!=)
- Greater than (>)
- Greater than or equal to (>=)
- Less than (<)
- Less than or equal to (<=)