September 9, 2026
Why Does 0.1 + 0.2 != 0.3 in Python?
This trips up almost everyone the first time they see it, and it isn't unique to Python — JavaScript, Java, C, and every other language using IEEE 754 floating-point numbers has the exact same behavior.
Computers can't represent most decimals exactly in binary
0.1 and 0.2 look simple in decimal, but in binary floating-point — the format almost every language uses for decimal numbers — they can't be represented exactly, similar to how 1/3 can't be written exactly as a finite decimal. The tiny rounding error from that approximation is what surfaces as 0.30000000000000004 instead of a clean 0.3.
How to compare floats correctly
import math print(round(0.1 + 0.2, 2)) print(math.isclose(0.1 + 0.2, 0.3))
0.3 True
round(0.1 + 0.2, 2) rounds away the tiny error for display. math.isclose() is the right tool when you actually need to compare two floats — it checks that they're close enough within a small tolerance, rather than checking for exact equality, which floating-point numbers essentially never satisfy after arithmetic.
For the full rundown on Python's number types and arithmetic operators, see the Numbers and Basic Math Operators lesson in our free Python course.