## Python Comparison Operators

Python Comparison operators compare the two operands on both sides. If the comparison is successful or equal, Boolean value True is returned. While the comparison fails, Boolean value False is returned.

When comparing two numeric type operands, the trailing zeros after the decimal point are truncated; for example,

9 == 9.0

So, after truncation, 9.0 becomes 9; hence LHS and RHS are equal.

When two strings are being compared, the comparison is made lexicographically.

When comparing lists or tuples, the comparison is done assuming that both operands have the same elements in the same order and are equal.

While comparing two floating-point numbers, one thing to remember is that floating-point numbers are stored in binary form in memory. The conversion from binary to decimal is an approximation. This approximation can lead to unexpected results while using comparison operators. For example

0.1 + 0.2 == 0.3 (will return false)

This is because 0.1 + 0.2 = 0.30000000004. is the result of approximation.

These are the supported comparison operators in Python

Operators Operations Examples
> Returns True if the left operand is greater than the right operand Else returns False 4>1 (returns True)
3>7 (returns False)
'mystring' > 'abcde' (returns True)
< Returns True if the right operand is greater than the left operand Else returns False 5<6 (returns True)
6<6 (returns False)
>= Returns True if the left operand is greater than Or equal to the right operand Else returns False 6>=6 (returns True)
5.0 >= 7.5 (returns False)
<= Returns True if the left operand is lesser than Or equal to the right operand Else returns False 5<=6 (returns True)
5.0 <= 7.5 (returns True)
== Returns True if the left operand is equal to the right operand Else returns False 3 == 4 (returns False)
0.1+0.1+0.1 == 0.3 (returns False)
['Thislist'] == ['Thislist'] (returns True)
!= Returns True if the left operand is not equal to the right operand Else returns False 3 != 4 (returns True)
0.1+0.1+0.1 != 0.3 (returns True)
['Thislist'] != ['Thislist'] (returns False)