Fractions (Rational Numbers) in Python
In Python, you can handle fractions (rational numbers) with the fractions module.
See the following article on how to find the greatest common divisor and least common multiple.
All sample code in this article assumes that the Fraction class has been imported as follows:
from fractions import Fraction
Create a Fraction object
This section describes how to create a Fraction object with the constructor, Fraction(). In all cases, the result is automatically reduced (simplified).
Specify numerator and denominator as integers
You can specify the numerator and denominator as integers (int). The denominator is considered 1 if omitted.
print(Fraction(1, 3))
# 1/3
print(Fraction(2, 6))
# 1/3
print(Fraction(3))
# 3
Convert float to Fraction
If a floating-point number (float) is passed, it is converted to a Fraction.
print(Fraction(0.25))
# 1/4
print(Fraction(0.3333333333333333))
# 6004799503160661/18014398509481984
print(Fraction(0.3333333333333333).limit_denominator())
# 1/3
The limit_denominator() method can be used to limit the maximum value of the denominator. Details are provided later in this article.
Convert str to Fraction
If a string (str) in fractional notation is passed, it is converted to Fraction.
print(Fraction('2/5'))
# 2/5
print(Fraction('16/48'))
# 1/3
Get numerator and denominator
You can get the numerator and denominator with the numerator and denominator attributes of Fraction. These attributes are read-only and cannot be modified.
a = Fraction(1, 3)
print(a)
# 1/3
print(a.numerator)
print(type(a.numerator))
# 1
# <class 'int'>
print(a.denominator)
print(type(a.denominator))
# 3
# <class 'int'>
# a.numerator = 7
# AttributeError: property 'numerator' of 'Fraction' object has no setter
Calculate and compare fractions
You can perform calculations on Fraction objects using arithmetic operators.
print(Fraction(1, 2) + Fraction(1, 3))
# 5/6
print(Fraction(1, 2) / Fraction(1, 3))
# 3/2
print(Fraction(1, 6) ** 2 + Fraction(1, 2) * Fraction(1, 3))
# 7/36
You can also use comparison operators with Fraction objects.
print(Fraction(7, 13) > Fraction(8, 15))
# True
Convert Fraction to float
You can convert Fraction to a floating-point number (float) with float().
print(float(Fraction(1, 5)))
# 0.2
The result of an operation between Fraction and float is automatically converted to float.
print(Fraction(1, 5) * 0.5)
# 0.1
Convert Fraction to str
You can convert Fraction to a string (str) with str().
print(str(Fraction(1, 3)))
# 1/3
print(type(str(Fraction(1, 3))))
# <class 'str'>
Get rational approximations: limit_denominator()
You can get a rational approximation with the limit_denominator() method.
The limit_denominator() method returns Fraction whose denominator is less than or equal to the max_denominator argument. The default is max_denominator=1000000.
Approximate Pi and Euler's number e
pi = Fraction(3.14159265359)
print(pi)
# 3537118876014453/1125899906842624
print(pi.limit_denominator(10))
print(pi.limit_denominator(100))
print(pi.limit_denominator(1000))
# 22/7
# 311/99
# 355/113
e = Fraction(2.71828182846)
print(e)
# 6121026514870223/2251799813685248
print(e.limit_denominator(10))
print(e.limit_denominator(100))
print(e.limit_denominator(1000))
# 19/7
# 193/71
# 1457/536
Convert repeating decimals to fractions
a = Fraction(0.3333333333333333)
print(a)
# 6004799503160661/18014398509481984
print(a.limit_denominator())
# 1/3
a = Fraction(0.14285714285714285)
print(a)
# 2573485501354569/18014398509481984
print(a.limit_denominator())
# 1/7