Fraction stores rational numbers as normalized integers. It helps with proportions, discrete measurements, and algorithms where 1/3 must remain exactly one third.
Practical example
from fractions import Fraction
ratio = Fraction(3, 8) + Fraction(1, 4)
from_text = Fraction("0.125")
print(ratio)
print(from_text)
assert ratio == Fraction(5, 8)
Construct from the right source
Prefer integers, another fraction, or a decimal string. Constructing from a float preserves the float's exact binary value, which may produce a surprising denominator.
Exactness has a cost
Fractions can grow during operations and cost more than floats. Use limit_denominator() only when approximation is acceptable. For money, decimal rounding rules usually make Decimal clearer.
Keep learning
Continue with Python Decimal for Accurate Money Calculations and Python statistics: Mean, Median, and Quantiles. A official Python documentation, accessed July 22, 2026, documents the API and behavior across versions.