Comparison operators allow us to compare two values and determine if they are equal, different, greater, less, greater/equal, or less/equal.
The results of the comparisons are boolean values (True
or False
), and they are fundamental for flow control logic.
List of comparison operators
Equality (==
)
The equality operator (==
) is used to check if two values are equal.
a = 5
b = 5
is_equal = (a == b) # True, since a and b are equal
Inequality (!=
)
The inequality operator (!=
) is used to check if two values are not equal.
a = 5
b = 3
not_equal = (a != b) # True, since a is not equal to b
Greater than (>
)
The greater than operator (>
) is used to check if one value is greater than another.
a = 10
b = 5
is_greater = (a > b) # True, since a is greater than b
Less than (<
)
The less than operator (<
) is used to check if one value is less than another.
a = 3
b = 7
is_less = (a < b) # True, since a is less than b
Greater than or equal to (>=
)
The greater than or equal to operator (>=
) is used to check if one value is greater than or equal to another.
a = 10
b = 10
is_greater_equal = (a >= b) # True, since a is equal to b
Less than or equal to (<=
)
The less than or equal to operator (<=
) is used to check if one value is less than or equal to another.
a = 5
b = 7
is_less_equal = (a <= b) # True, since a is less than b