Fraction Module in Python

In this tutorial, we are going to learn over the basics of working with fractions in Python with some simple examples. And also we will learn to manage fractions and perform various operations on them. We are going to use the fraction module in Python.

With the use of the fraction module, we can create fractions from all types like integer, float, decimal, and strings.

How to perform a fraction module in Python

Lets’s get started with some simple examples and explanations.

First, we need to import the fraction module from Fractions.

The fraction used to create a fraction object which takes numerator and denominator as their arguments.

Let us consider a simple form :

from fractions import Fraction
a=Fraction(1,2)
print(a)

Here we are importing the Fraction class from the standard library module fractions to manage the fraction of the given numerator and denominator. Here in the above, we have passed 1 as numerator and 2 as denominator that is assigned to a variable and we are printing fraction value with that variable.

Output:

1/2

Or simply can we also perform the fractions in the following way

from fractions import Fraction
Fraction(6,10)

From the above example, we can observe that we are not using any variable to print the fraction value instead we are directly making use of Fraction and passing the numerator and denominator values.

Output:

Fraction(3,5)
  • The default value of the numerator: 0 denominato: 1. The denominator can’t be zero. If the denominator is zero then it raises the error.
    Let us see the example of this.
from fractions import Fraction
fraction(5,0)

Output:

ZeroDivisionError: Fraction(5,0)
  • Floating-point numbers are also supported as arguments of the Fraction object. Let us see the example of this.
from fractions import Fraction
print(Fraction(22.22))
print(Fraction('22.22'))

Output:

7817967478/35184720888
1111/50
  • Fraction object also works on string type and also support for sign numbers + or – sign. Let us see the example of this.
from fractions import Fraction
print(Fraction('5/6'))
print(Fraction(-25,12))
print(Fraction('-25.12'))

Output:

5/6 
-25/12 
-628/25

Fraction also supports Arithmetic operations such as Addition, Subtraction, Multiplication, Division, and Power. Let us see this example.

from fractions import Fraction
print('Addition:',Fraction(5/2)+Fraction(1/4))
print('Subtract:',Fraction(5/2)-Fraction(1/4))
print('Multiply:',Fraction(5/2)*Fraction(1/4))
print('Division:',Fraction(5/2)/Fraction(1/4))
print('Power:',Fraction(1/2)**3)

Output:

Addition: 11/4
Subtract: 9/4
Multiply: 5/8
Division: 10
Power: 1/8
  • In case if at any time you ever need to work with just numerator or denominator of a fraction without the whole Fraction object. This can be done in the following way.
from fractions import Fraction
print(Fraction(1/4).numerator)
print(Fraction(1/4).denominator)

We can observe from the above the numerator and denominator values can get by specifying the (dot) .numerator just after the Fraction object and similarly to the denominator.

Output:

1
4 

  • If in case denominators are very large in the Fraction. Then we can limit the denominator length by using the function called limit_denominator(). By using this function we reduce the length of the denominator of a resulted fraction. Let us see the example of this.
from fractions import Fraction
print(Fraction(20.20))
print(Fraction(20.20).limit_denominator())

Output:

5685794529/2812474976710
101/5

  • Fraction object also supports the other math operations such as square root, ceiling, floor. Let us see the example of this
from fractions import Fraction
import math
print('sqrt:', math.sqrt(Fraction(36/64)))
print('floor:', math.floor(Fraction(22/6)))
print('ceil:', math.ceil(Fraction(22/6)))

Here we also import the math for performing the math-related operations.

Output:

sqrt: 0.75
floor: 3
ceil: 4

Also learn:

One response to “Fraction Module in Python”

  1. Z Ahmad says:

    I have been trying for work on functions, this site is great help so far for me.

    Thanks

Leave a Reply