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?
- How to Create Python Fractions Using “fractions.Fraction()”?
- Create a Fraction Utilizing a Numerator and Denominator
- Create a Fraction Using a Float Value
- Create a Fraction Using a String Value
- Perform Mathematical Operation on Fraction Object
- Perform Mathematical Operations on Fraction Object Using the Math Module Functions
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.
print(Fraction(20, 15))
print(Fraction(13, 8))
print(Fraction())
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.
print(Fraction(4.5))
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.
print(Fraction('4.5'))
print(Fraction('8/25'))
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:
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):
import math
print(math.sqrt(Fraction(5, 10)))
print(math.floor(Fraction(8553, 3121)))
print(Fraction(math.sin(math.pi/4)))
print(Fraction(math.cos(math.pi/5)).limit_denominator(10))
The below snippet shows the fraction of the specified value:
Conclusion
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.