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.
Comparison Operators
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
The comparison operator '!=' is used to check if two values are .
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
The comparison operator '>' is used to check if the left value is the right value.
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
The comparison operator '<' is used to check if the left value is the right value.
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
The comparison operator '<=' is used to check if the left value is the right value.