Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python fractions Module

Python fractions module enables the user to manage fractions related computations in an efficient manner. This module enables us to create fractions from integers, floats, decimal, and strings.

This fraction module supports rational number arithmetic operations.


Basics of fractions module

1. Importing a fraction module:

from fractions import Fraction

2. Instantiate the fraction class:

There are a number of ways to instantiate the fraction class. The following are some of the ways to instantiate the fraction class:

  • Pass the numerator and denominator
  • Instantiate with float
  • Instantiate with decimal
  • Instantiate with string

Example: Instantiate by passing numerator and denominator

from fractions import Fraction
Fraction(1, 2)

Example: Instantiate with float

from fractions import Fraction
Fraction(2.5)

Example: Instantiate with decimal

from fractions import Fraction
from decimal import Decimal
Fraction(Decimal('1.1'))

Example: Instantiate the string

from fractions import Fraction
Fraction('9/16')

Creating fractions

import fractions

for num, decimal in [(1, 2), (3, 6), (30, 7)]:
    result = fractions.Fraction(num, decimal)
    print('{}/{} = {}'.format(num, decimal, result))

Output:

1/2 = 1/2
3/6 = 1/2
30/7 = 30/7

Performing Arithmetic operations on fractions

The following example explains the different mathematical operations on fractions.

from fractions import Fraction 

print(Fraction(100, 10) + Fraction(25, 5)) 

print(Fraction(18, 5) / Fraction(18, 10)) 

print(Fraction(18, 15) * Fraction(16, 25)) 

print(Fraction(17, 25) * Fraction(15, 25)) 

print(Fraction(12, 5) ** Fraction(12, 11)) 

Output:

Output Arithmetic Operations On Fractions
Output-Arithmetic Operations On Fractions

Fraction Instances

A Fraction instance is a fraction built from a pair of integers, rational numbers or string. Fractions are hashable and immutable, so their values can’t be changed once created.

class fractions.Fraction(numerator=0, denominator=1) : It requires the numerator and denominator to be numbers. A fraction value = (numerator/denominator) is returned as the output, the default values is 0. An error of zero division is raised if denominator = 0.

Example:

from fractions import Fraction 

print(Fraction(10, 35)) 
 
print(Fraction(10, 13)) 
 
print(Fraction()) # default value

Output:

2/7
10/13
0

Combining Math with fractions

The functions of the math module can be mixed with the fractional representation. The below example depicts one such representation.

import math
from fractions import Fraction
  
print("Square root: {}".format(math.sqrt(Fraction(21, 4))))
  
print("Floored to: {}".format(math.floor(Fraction(4100, 1100))))
  
print("Sined Fraction: {}".format(Fraction(math.sin(math.pi/2))))

Output:

Square root: 2.29128784747792
Floored to: 3
Sined Fraction: 1

Rounding off fractions

There is a possibility to round off fractions by the number of digits the user wants in the denominator.

Example:

import fractions

result = fractions.Fraction('21/8')
print('Rounded without limit : {}'.format(round(result)))

print('Rounded to 2 digits : {}'.format(round(result, 2)))

Output:

Rounded without limit : 3
Rounded to 2 digits : 131/50

Conclusion

We have understood and implemented the functionalities of the fractions module in Python.


References