On this page
fractions
Prev: fnmatch | Up: stdlib_module | Next: ftplib
Rel: numbers, math, cmath, decimal, random
Ref: docs.python.org — fractions
Tags: #public #python3 #stdlib
Import
import fractions
Overview
Exact rational-number arithmetic (Fraction(numerator, denominator)) automatically reduced to lowest terms. Reach for Fraction when exact ratios matter (music intervals, aspect ratios, probability/combinatorics, or rational linear algebra) without binary floating-point drift.
Quick Start
from fractions import Fraction
a = Fraction(1, 3)
b = Fraction(1, 6)
assert a + b == Fraction(1, 2)
assert Fraction("0.75") == Fraction(3, 4)
assert (Fraction(2, 3) * 3) == 2
Implementation Walkthrough
1. Approximating Floats as Clean Ratios (limit_denominator)
Binary floats like 0.3 have huge exact denominators; limit_denominator() snaps them to the closest clean rational approximation:
from fractions import Fraction
import math
approx_pi = Fraction(math.pi).limit_denominator(1000)
assert approx_pi == Fraction(355, 113)
clean = Fraction(0.3).limit_denominator(1_000_000)
assert clean == Fraction(3, 10)
2. Mixing with int & Extracting Components
from fractions import Fraction
ratio = Fraction(1920, 1080)
assert (ratio.numerator, ratio.denominator) == (16, 9)
Gotchas & Boundaries
- Pass strings or integer pairs, not raw floats:
Fraction(0.1)producesFraction(3602879701896397, 36028797018963968); writeFraction("0.1")orFraction(1, 10). - Denominators can grow rapidly in long iterative calculations: Call
.limit_denominator()or switch tofloat/Decimalif arbitrary-precision numerators/denominators grow unbounded. - Formatting: Python 3.12+ supports standard format specs on
Fraction(f"{Fraction(1, 3):.4f}" == "0.3333").
Classes & Objects
- fractions.Fraction — This class implements rational numbers. (Methods:
.as_integer_ratio(),.conjugate(),.denominator(),.from_decimal(),.from_float(),.from_number())
Linked from
- tuts/python3
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/cmath
- tuts/python3/stdlib_module/decimal
- tuts/python3/stdlib_module/fnmatch
- tuts/python3/stdlib_module/fractions/Fraction
- tuts/python3/stdlib_module/ftplib
- tuts/python3/stdlib_module/math
- tuts/python3/stdlib_module/numbers
- tuts/python3/stdlib_module/random
- tuts/python3/stdlib_module/statistics
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/tutorials/core_fundamentals/primitive_data_types
Related notes
- tuts/python3/stdlib_module/cmath Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/decimal Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/fnmatch Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/fractions/Fraction Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ftplib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/math Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/numbers Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/random Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib