Python fractions Module — Core Concepts
What the fractions module provides
The fractions module gives you a Fraction class that represents rational numbers as exact numerator/denominator pairs. Arithmetic on fractions produces exact results — no floating-point rounding.
from fractions import Fraction
a = Fraction(1, 3)
b = Fraction(1, 6)
print(a + b) # 1/2 (exact)
print(a * 3) # 1 (exact)
Creating fractions
| Method | Example | Result |
|---|---|---|
| Two integers | Fraction(3, 4) | 3/4 |
| Single integer | Fraction(5) | 5 |
| String | Fraction("3.14") | 157/50 |
| Float | Fraction(0.1) | 3602879701896397/36028797018963968 |
| Decimal | Fraction(Decimal("0.1")) | 1/10 |
Notice the float vs string difference. Fraction(0.1) captures the actual float value (which isn’t exactly 0.1 in binary). Fraction("0.1") gives you exactly 1/10. Always use strings or integers for precise input.
Automatic simplification
Fractions are always stored in lowest terms:
Fraction(4, 8) # → Fraction(1, 2)
Fraction(6, -9) # → Fraction(-2, 3)
The sign is always on the numerator. The GCD is computed automatically using math.gcd.
Arithmetic
All standard operations return exact Fraction results:
from fractions import Fraction
a = Fraction(2, 3)
b = Fraction(3, 4)
a + b # 17/12
a - b # -1/12
a * b # 1/2
a / b # 8/9
a ** 2 # 4/9
abs(Fraction(-5, 3)) # 5/3
Mixing Fraction with int preserves exactness. Mixing with float converts to float (losing precision).
Fraction(1, 3) + 1 # Fraction(4, 3) — exact
Fraction(1, 3) + 0.5 # 0.8333333333333333 — float, not exact
limit_denominator — practical approximation
Sometimes you want a simpler fraction that’s close enough:
Fraction(355, 113) # ≈ π
pi_approx = Fraction(355, 113)
pi_approx.limit_denominator(10) # Fraction(22, 7)
pi_approx.limit_denominator(100) # Fraction(311, 99)
pi_approx.limit_denominator(1000) # Fraction(355, 113) — already simple enough
limit_denominator(max) returns the closest fraction with denominator at most max. This is useful for converting messy floating-point numbers back to clean fractions:
Fraction(0.1).limit_denominator(1000) # Fraction(1, 10)
Float vs Decimal vs Fraction
| Type | Precision | Speed | Use case |
|---|---|---|---|
float | ~15 decimal digits | Fastest | General-purpose math, science |
Decimal | Configurable (28+ digits) | Medium | Finance, currency, fixed-point |
Fraction | Exact (arbitrary precision) | Slowest | Exact ratios, symbolic math, algorithms |
Choose float unless you need more. Choose Decimal for money. Choose Fraction when rounding errors are unacceptable.
Common misconception
People assume Fraction is always better than float because it’s exact. But Fraction arithmetic creates progressively larger numerators and denominators. After many operations, a Fraction might have numerators with hundreds of digits. Arithmetic on such large integers is slower than float arithmetic. Fractions trade speed for exactness — make sure you need that exactness.
Practical use cases
Probability calculations:
from fractions import Fraction
# Probability of drawing 2 aces from a 52-card deck
p = Fraction(4, 52) * Fraction(3, 51)
print(p) # 1/221 (exact)
Recipe scaling:
# Scale a recipe from 4 servings to 6
scale = Fraction(6, 4) # 3/2
flour = Fraction(2, 3) * scale # 1 cup (exact)
Continued fraction approximations:
# Find rational approximation of √2
import math
approx = Fraction(math.sqrt(2)).limit_denominator(100)
print(approx) # 99/70
The one thing to remember: Use Fraction when you need exact rational arithmetic — create from strings or integers (not floats) to keep the input exact, and use limit_denominator when you need a cleaner approximation.
See Also
- Python Atexit How Python's atexit module lets your program clean up after itself right before it shuts down.
- Python Bisect Sorted Lists How Python's bisect module finds things in sorted lists the way you'd find a word in a dictionary — by jumping to the middle.
- Python Contextlib How Python's contextlib module makes the 'with' statement work for anything, not just files.
- Python Copy Module Why copying data in Python isn't as simple as it sounds, and how the copy module prevents sneaky bugs.
- Python Dataclass Field Metadata How Python dataclass fields can carry hidden notes — like sticky notes on a filing cabinet that tools read automatically.