Python Fractions

Python is a readable and high-level programming language that can handle various data types. It can also work with fractions, which are rational numbers using the “fractions” module. This module provides the class “fractions.Fraction()” that creates the fraction object.

This tutorial delivered a comprehensive guide on Python fractions using several examples. Follow the below contents to get started:

What is the Python Fraction Module?

In Python, the “fractions” module is a part of the standard library that is used for rational number asthmatic. This module enables you to work with fractions, which are numbers represented as a ratio of two integers (numerator and denominator).

How to Create Python Fractions Using “fractions.Fraction()”?

The class “fractions.Fraction()” accepts numerator, denominator and constructs a fraction object in Python. The default value of the denominator and numerator is “1” and “0”, which introduces “ZeroDivisionError” when the denominator is set to “0”.

Let’s overview this class by following multiple examples!

Example 1: Create a Fraction Using a Numerator and Denominator

In the below code, we first imported the “fraction.Fraction” class. Next, the “Fraction()” class takes the numerator and denominator as an argument and retrieves the fraction.

from fractions import Fraction
print(Fraction(20, 15))
print(Fraction(13, 8))


The below snippet shows the fraction object:

Example 2: Create a Fraction Using a Float Value

Here, the “Fraction()” class is used to create a fraction by taking the float value as an argument.

from fractions import Fraction


The below fraction object is created from the float:

Example 3: Create a Fraction Using a String Value

We can also pass the string, float, fraction, or Unicode instances to the class “fraction.Fraction()” to create a fraction object.

from fractions import Fraction
print(Fraction('1.414213 \t\n'))


The following snippet shows the fraction object generated using string:

Example 4: Perform Mathematical Operation on Fraction Object

In the given code below, mathematical operations, such as addition, division, multiplication, and others are performed on the two-fraction object. The class “fractions.Fraction()” creates the fraction object by taking the numerator and denominator values:

from fractions import Fraction
print(Fraction(5, 10) + Fraction(25, 10))
print(Fraction(8, 5) / Fraction(8, 10))
print(Fraction(8, 5) * Fraction(6, 9))
print(Fraction(2, 5) ** Fraction(2, 10))


After performing the mathematical operation, the following output is retrieved:

Example 5: Perform Mathematical Operation on Fraction Object Using Math Module Functions

In the below code, the “math” module is imported along with the “fractions” module. Next, the “math.sqrt()” and “math.floor()” functions take the fraction object and retrieve the square root as well as the floor value of the fraction. After that, the “Fraction()” class retrieves the fraction object of the result of the “math.sin()” and “math.cos()” methods. The “limit_denominator()” employed here retrieves the fraction whose denominator is smaller than or equivalent to the “max_denominator” argument (default max_denominator=1000000):

from fractions import Fraction
import math
print(math.sqrt(Fraction(5, 10)))
print(math.floor(Fraction(8553, 3121)))


The below snippet shows the fraction of the specified value:


In Python, the “fractions.Fraction()” class is used to create the fraction object by taking the numerator and denominator values. We can also create the fraction object by accepting the float, string, and Unicode values as an argument. The simple mathematical operation and math module function can be performed on multiple fractions to retrieve the outcome. This tutorial provided a precise guide on Python fractions utilizing numerous examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.