Python Fraction Module

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

Python supports all basic data types like integer, float, decimals and strings. Apart from these basic data types, python also supports the use of fractions in it's standard form with the help of the python fraction module. The python fraction module allows a user to denote a fraction using their numerators and denominators and also supports multiple operations on these fractions.

Python Fraction Module

The Python Fraction module is a python module that helps represent fractions and also allows the user to perform operations on them. The python fraction module allows the user to create fractions using integers, floating point numbers, decimal numbers as well as strings.

The python fraction module allows users to create fraction instances that can be created from a pair of integers(numerator and denominator), from a rational number, or even from a string. Fraction instances are hashable and are treated as immutable.

The python fraction module can be used in a python module by importing the fraction module:

The fraction module can then be used to represent fractions in python.

Important Examples to Understand Fraction Python Module

Instantiate Using Numerator and Denominator

Python fractions can be instantiated by using integer numerator and denominator pairs by using the following class:

For example:

Output:

As we can observe, if no value is passed to the function, 0 is returned as the default numerator and denominator values are 0 and 1 respectively. If the denominator is passed as 0, a zerodivision error is raised.

Instantiate Using Other Fraction

Python fractions can also be instantiated using other fraction instances. This requires that the argument is an instance of numbers.Rational where numbers.Rational is an abstract class of data that consists of numerator and denominator properties:

For example:

Output:

Instantiate Using Float Value

Python fractions can also be instantiated using a floating-point number. When a floating point number is passed into Fraction it returns an equivalent fraction:

For example:

Output:

Instantiate Using Decimal

Python fractions can also be instantiated using decimal numbers and an equivalent fraction is returned

For example:

Output:

Instantiate Using a String

A fraction can also be passed as a string to the Fractions function. The equivalent fraction is instantiated:

For example:

Output:

In this case as well, if the denominator in the string passed is zero, a zerodivision error is thrown.

Fraction Modifiers

Python Functions also allows us to modify how the fraction is represented and has two functions, namely: Limit Denominator : Syntax:

It returns the smallest equivalent fraction with denominator less than the specified max value. For example:

Output:

Represent as integer ratio Syntax:

Allows us to represent a fraction as an integer ratio tuple. For example:

Output:

Performing Mathematical Operations on Fractions

Basic mathematical operations can be performed on fraction instances in the same way as they are performed on other datatypes. For example:

Output:

Conclusion

  • Python Fraction module allows users to represent fractions in their standard form
  • Python Fraction module helps perform multiple operations on fractions
  • Python Fractions can be instantiated using integers, decimals, floating point numbers as well as strings