Skip to main content

Python Numbers

Data Types Focus

Python Numbers

Integers, floats, complex, Decimal, and Fraction give you the range to model everything from counters to currency.

Numeric types

TypeDescriptionExample
intArbitrary precision integer42
floatIEEE 754 double precision3.1415
complexReal + imaginary parts1 + 2j
decimal.DecimalBase-10 with exact precisionDecimal("0.10")
fractions.FractionRational numbersFraction(1, 3)

Precision

0.1 + 0.2 == 0.30000000000000004
  • Floats can't represent some decimals exactly.
  • Use Decimal for money and Fraction for rational arithmetic.

Useful modules

import math
math.sqrt(2)
math.isclose(a, b)

import statistics
statistics.mean(values)

Next up in your learning path