You must have used numeric data types like integers and floating point numbers in python. But have you used fractions in their actual form? In this article we will study about fractions and will perform operations on fractions using the fractions module in python.
How to use the fractions module in Python?
We can use the Fraction method from fractions module in python to create rational numbers in the form of fractions.We can import the module as follows.
import fractions
We can convert an integer, a floating point number or a string to a fraction in python . To convert a ratio of two integers to fraction, we use Fraction() method of fractions module and pass the numerator as the first argument and the denominator as the second argument. The function returns a fraction object as follows.
import fractions
myInt1=1
myInt2=2
print("Integer 1(numerator) is:",myInt1)
print("Integer 2(denominator) is:",myInt2)
myFraction=fractions.Fraction(myInt1,myInt2)
print("Fraction value is:",myFraction)
Output:
Integer 1(numerator) is: 1
Integer 2(denominator) is: 2
Fraction value is: 1/2
We can obtain a fraction value from a floating point number using the Fraction method of fractions module. When we pass a floating point number to the Fraction() method as input, it returns the corresponding fraction value as follows.
import fractions
myFloat=0.5
print("Floating point number is:",myFloat)
myFraction=fractions.Fraction(myFloat)
print("Fraction value is:",myFraction)
Output:
Floating point number is: 0.5
Fraction value is: 1/2
We can also convert a string to fraction using the Fraction() method. We can pass a string representation of fraction or a floating point literal in the string as input to the Fraction method which returns the corresponding fraction values as follows.
import fractions
myStr1="0.5"
print("String literal is:",myStr1)
myFraction1=fractions.Fraction(myStr1)
print("Fraction value is:",myFraction1)
myStr2="1/2"
print("String literal is:",myStr2)
myFraction2=fractions.Fraction(myStr2)
print("Fraction value is:",myFraction2)
Output:
String literal is: 0.5
Fraction value is: 1/2
String literal is: 1/2
Fraction value is: 1/2
How to round off fractions?
We can round off fractions in python according to the number of digits required in the denominator of the fraction using round() method. The round() method takes the fraction to be rounded as the first argument and the number of digits to which the denominator is to be rounded as the second argument. The function returns a fraction with desired number of digits in the denominator.This can be understood as follows.
import fractions
myInt1=50
myInt2=3
myFraction=fractions.Fraction(myInt1,myInt2)
print("Fraction value is:",myFraction)
rounded=round(myFraction,2)
print("Rounded value is:",rounded)
Output:
Fraction value is: 50/3
Rounded value is: 1667/100
When we do not pass the number of digits to which the denominator is to be rounded as the second argument, the round() method converts the fraction to nearest integer. This can be seen as follows.
import fractions
myInt1=50
myInt2=3
myFraction=fractions.Fraction(myInt1,myInt2)
print("Fraction value is:",myFraction)
rounded=round(myFraction)
print("Rounded value is:",rounded)
Output:
Fraction value is: 50/3
Rounded value is: 17
Obtaining numerator and denominator from a fraction
We can also extract the numerator and denominator from a Fraction. To extract the numerator, we use the “numerator” field of the fraction object. Similarly, to extract the denominator, we use the “denominator” field of the fraction object. This can be understood from the following example.
import fractions
myInt1=50
myInt2=3
myFraction=fractions.Fraction(myInt1,myInt2)
print("Fraction value is:",myFraction)
print("Numerator is:",myFraction.numerator)
print("Denominator is:",myFraction.denominator)
Output:
Fraction value is: 50/3
Numerator is: 50
Denominator is: 3
Arithmetic operations on fractions
We can perform arithmetic operations like addition, subtraction, multiplication and division on fractions using the fractions module in python just as we perform these operations on other numeric data types like integers and floating point numbers.
We can perform the arithmetic operations on two given fractions in python as follows.
import fractions
myFraction1=fractions.Fraction(50,3)
myFraction2=fractions.Fraction(1,2)
print("First Fraction value is:",myFraction1)
print("Second Fraction value is:",myFraction2)
print("Fraction1 + Fraction2 is:",myFraction1 + myFraction2)
print("Fraction1 - Fraction2 is:",myFraction1 - myFraction2)
print("Fraction1 * Fraction2 is:",myFraction1 * myFraction2)
print("Fraction1 / Fraction2 is:",myFraction1 / myFraction2)
Output:
First Fraction value is: 50/3
Second Fraction value is: 1/2
Fraction1 + Fraction2 is: 103/6
Fraction1 - Fraction2 is: 97/6
Fraction1 * Fraction2 is: 25/3
Fraction1 / Fraction2 is: 100/3
Get approximate rational value from a floating point number using fractions module
We can obtain a rational number in the form of fraction from any floating point or decimal value. To obtain the fraction from the decimal number, we can pass the decimal number to the Fraction() method which will convert them to a rational number as follows.
import fractions
myFloat= 22/7
print("Floating point value is:",myFloat)
myFraction=fractions.Fraction(myFloat)
print("Fraction value is:",myFraction)
Output:
Floating point value is: 3.142857142857143
Fraction value is: 7077085128725065/2251799813685248
After obtaining the fraction in the above format, we can limit the highest value of the denominator using the limit_denominator() method. The limit_denominator() method, when invoked on a fraction, takes the highest allowed value of the denominator as input and returns the corresponding fraction. This can be understood from the following example.
import fractions
myFloat= 22/7
print("Floating point value is:",myFloat)
myFraction=fractions.Fraction(myFloat)
print("Fraction value is:",myFraction)
myFraction1=myFraction.limit_denominator(100)
print("Approximate Fraction value with denominator limited to 100 is:",myFraction1)
Output:
Floating point value is: 3.142857142857143
Fraction value is: 7077085128725065/2251799813685248
Approximate Fraction value with denominator limited to 100 is: 22/7
Conclusion
In this article, we have studied the fraction data type and have implemented it using the fractions module in python. We can also write the programs used in this article with exception handling using python try except to make the programs more robust and handle errors in a systematic way. Stay tuned for more informative articles.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.