= 0.15 + 0.15
a = 0.10 + 0.20
b
print(a == b)
print(a >= b)
False
False
Operator | Name |
---|---|
== | Equal |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
True
or False
)int
, float
, complex
), sequences (list
, tuple
, string
) and dictionaries (dict
)What is happening here? How might we perform a reasonable comparison between these floating point numbers?
One way to solve: use math.isclose()
:
True
False
False
True
However, order matters:
(after all, with strings it is also easy to see that Hi
is different than iH
).
If we need to ignore order, and compare only elements, we can compare the sorted lists:
Sequences follow a lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.
For example, all comparisons below are True
:
Differently from most other languages, Python supports operator chaining, like 2 < x < 5
.