Numbers
Prerequisites: Data types
Python has two main numeric types: int for whole numbers with no size
limit, and float for numbers with a decimal point.
whole = 42
decimal = 3.14
big = 42_000_000 # underscores are allowed as visual separators
Mixing an int and a float in a calculation always produces a float:
3 + 2.0 # 5.0
Common mistakes
- Expecting
floatmath to be perfectly exact —0.1 + 0.2prints0.30000000000000004due to how decimals are stored in binary. This is true of nearly every programming language, not a Python bug.