Skip to content

Comparison Operators

What are Comparison Operators

Also know us relational operators, are those operators that are used to compare values for equality, inequality and so on.

Equalality operator

The equality operator represented by == checks if two object are equals, then either returns True if the two objects(operands) are equal, else returns False when they are equal.

Example

>>>
>>> # returns True because both values are of the same data type and value
>>> 15 == 15
True
>>>
>>> # returns False because both values do have the same value
>>> # though they are of the same data type
>>> 20 == 15
False
>>>
>>> #
>>> 15.0 == 15
False
>>>

Inequality operator

Inequality operator represented by != checks whether two objects(operands) are not equal, then it returns True if they are equal, else it returns False.

Example

>>>
>>> # returns False because both values are equal
>>> 10 != 10
False
>>>
>>> # returns True because both values are not of the same data type
>>> 10 != 10.0
True
>>>
>>>

Greater than

Greater than operator symbolised by > compares if the left operand(value) is greater than the right operand(value). Returns True if greater than and False if not

Example

>>>
>>> # returns False
>>> 68 > 86
False
>>>
>>> # returns True
>>> 20 > 10
True
>>>
>>> # returns True
>>> -20 < 1
True
>>>

Less than

Less than operator represented by < compares if the left operand(value) is less than the right operand(value). Returns True if less than and False if not

Example

>>>
>>> #
>>> 30 < 60
True
>>>
>>> #
>>> 19.1 < 10.9
False
>>>

Greater than or equal

Greater than or equal operator checks if the operand(value) on the left side is greater than or equals to the operand(value) on the right side.

Example

>>>
>>> #
>>> 12 >= 11
True
>>>
>>> #
>>> 16.9 >= 17.7
False
>>>

Less than or equal

Less than or equal operator symbolised by <= checks if the operand(value) on the left side is less than or equals to the operand(value) on the right side.

Example

>>>
>>> #
>>> 12 <= 29
True
>>>
>>> #
>>> 20.3 <= 10.9
False
>>>