Reduce or simplify fractions in Python

Python fractions module supports fractions and Fraction is the type of fraction. A fraction instance has numerator and denominator attributes. Python Fraction automatically reduce a fraction.

from fractions import Fraction

a = Fraction(6, 8)

print(a)  # 3/4
print(a.numerator)  # 3
print(a.denominator)  # 4

Fraction(6, 8) is $\frac{6}{8}$ but the numerator is 3.


Powered by Markdown