Python | Comparison Operators

Comparison operators always return True or False because they just compare the values of the operands.

Let’s consider for the all examples here that a = 5 and b = 3.

The Equal to operator returns True if the values of both operands are equal. Its symbol is double equal signs (==).

>>>a == b
False

The Not equal to operator returns True if the values of the operands are not equal. Otherwise it returns False. Its symbol is an exclamation mark followed by an equal sign (!=).

>>>a != b
True

The Greater than operator returns True if the left operand is greater than the right operand. Otherwise it returns False. Its symbol is a ‘greater than’ symbol (>).

>>>a > b
True

The Less than operator returns True if the left operand is less than the right operand. Otherwise it returns False. Its symbol is a ‘less than’ symbol (<).

>>>a < b
False

The Greater than or equal to operator returns True if the left operand is greater than or equal to the right operand. Otherwise it returns False. Its symbol is a 'greater than' symbol followed by an equal sign (>=).

>>>a >= b
True

The Less than or equal to operator returns True if the left operand is less than or equal to the right operand. Otherwise it returns False. Its symbol is a 'less than' symbol followed by an equal sign (<=).

>>>a <= b
False

Leave a Reply