Fraction Module in Python
The fractions module in Python allows you to perform arithmetic operations with rational numbers while maintaining precision.
Why use the fractions module?
- Unlike floating-point numbers (
floattype), which may cause precision errors, theFractionclass provides exact arithmetic with rational numbers. - It supports addition, subtraction, multiplication, and division without rounding errors.
1. Importing the fractions Module
To use Fraction, first import the module:
from fractions import Fraction
2. Creating Fractions
The Fraction class allows you to create fractions in multiple ways.
2.1 From Integers (Numerator/Denominator)
You can create a fraction by passing two integers, where the first number is the numerator and the second is the denominator.
from fractions import Fraction
f1 = Fraction(3, 4) # Represents 3/4
print(f1)
Output:
3/4
The fraction 3/4 remains unchanged.
2.2 From a String
You can also create a Fraction using a string representation:
f2 = Fraction("5/8")
print(f2)
Output:
5/8
This is useful when reading fractions from user input or text files.
2.3 From a Floating-Point Number
When you pass a floating-point number, Python converts it into an exact fraction.
f3 = Fraction(0.75)
print(f3)
Output:
3/4
Python automatically simplifies
0.75to3/4using the greatest common divisor (GCD).
2.4 From a Decimal Number
When using floating-point numbers, precision issues can occur. Instead, you can use decimal.Decimal for more accurate conversions.
from decimal import Decimal
f4 = Fraction(Decimal('0.1')) # Using Decimal instead of float
print(f4)
Output:
1/10
Decimal('0.1') is an exact representation, unlike Fraction(0.1), which can lead to floating-point inaccuracies.
3. Fraction Arithmetic Operations
Fractions in Python support all arithmetic operations.
3.1 Addition of Fractions
f1 = Fraction(1, 4)
f2 = Fraction(1, 2)
result = f1 + f2
print(result)
Output:
3/4
The sum of 1/4 and 1/2 is 3/4 because:
\frac{1}{4} + \frac{1}{2} = \frac{1}{4} + \frac{2}{4} = \frac{3}{4} ]
3.2 Subtraction of Fractions
result = f2 - f1
print(result)
Output:
1/4
The difference between 1/2 and 1/4 is 1/4 because:
\frac{1}{2} – \frac{1}{4} = \frac{2}{4} – \frac{1}{4} = \frac{1}{4} ]
3.3 Multiplication of Fractions
result = f1 * f2
print(result)
Output:
1/8
Multiplication formula:
\frac{1}{4} \times \frac{1}{2} = \frac{1 \times 1}{4 \times 2} = \frac{1}{8} ]
3.4 Division of Fractions
result = f1 / f2
print(result)
Output:
1/2
Division formula:
\frac{1}{4} \div \frac{1}{2} = \frac{1}{4} \times \frac{2}{1} = \frac{2}{4} = \frac{1}{2} ]
4. Properties of a Fraction
Each fraction has two properties:
numerator→ The top part of the fractiondenominator→ The bottom part of the fraction
f = Fraction(5, 10)
print(f.numerator)
print(f.denominator)
Output:
1
2
Python simplifies 5/10 to 1/2, so the numerator is 1 and the denominator is 2.
5. Limiting the Denominator
Sometimes, fractions derived from floats have very large denominators. Use limit_denominator() to find an approximate fraction.
f = Fraction(3.14159) # Creates a fraction close to π
print(f)
approx = f.limit_denominator(1000)
print(approx)
Output:
314159/100000
355/113
355/113 is a common approximation of π.
6. Mixed Operations with Integers and Floats
Fractions can interact with other data types.
print(Fraction(1, 3) + 1) # Adds an integer
print(Fraction(1, 3) + 0.5) # Adds a float
Output:
4/3
0.8333333333333333
If a fraction is added to a float, the result is converted to a float.
7. Comparing Fractions
Fractions can be compared using standard operators.
print(Fraction(1, 2) > Fraction(1, 3))
print(Fraction(3, 4) == Fraction(6, 8))
Output:
True
True
1/2is greater than1/33/4and6/8are equivalent.
8. Converting Fractions
8.1 Convert to Float
print(float(Fraction(3, 4)))
Output:
0.75
8.2 Convert to Integer
print(int(Fraction(5, 2)))
Output:
2
The integer part of
5/2is2(truncates the decimal).
9. Greatest Common Divisor (GCD) Calculation
Python automatically simplifies fractions using GCD.
import math
print(math.gcd(12, 16))
Output:
4
The GCD of 12 and 16 is 4.
Summary
| Feature | Example Usage |
|---|---|
| Create fraction from numbers | Fraction(3, 4) → 3/4 |
| Create from string | Fraction("5/8") → 5/8 |
| Arithmetic operations | Fraction(1, 2) + Fraction(1, 3) → 5/6 |
| Convert to float | float(Fraction(3, 4)) → 0.75 |
| Limit denominator | Fraction(3.14159).limit_denominator(1000) → 355/113 |
| Access numerator/denominator | f.numerator, f.denominator |