This program demonstrates the use of the Fraction class from the fractions module in Python. The Fraction class provides support for rational numbers, which are numbers that can be expressed as a fraction of two integers.
In this program, a list of decimal numbers l is created. The program then iterates over each number in the list using a for loop. For each number, the Fraction() constructor of the Fraction class is used to convert the decimal number to a fraction.
The Fraction() constructor takes a string or a number as an argument and returns a Fraction object representing the corresponding fraction. The Fraction object has a numerator and a denominator, which can be accessed using the numerator and denominator attributes, respectively.
The print() function is used to print the original decimal number and the corresponding fraction in a formatted string. The "&>;4" format specifier is used to right-align the decimal number in a field of width 4.
Overall, this program demonstrates the use of the Fraction class and its constructor to convert decimal numbers to fractions. It shows how Python provides built-in support for rational numbers, which can be useful in many applications involving precise mathematical calculations.
import fractions l = ["2.3", "7.5", "6.32", "5.1"] for s in l: f = fractions.Fraction(s) print("{0:>4} = {1}".format(s, f))
2.3 = 23/10 7.5 = 15/2 6.32 = 158/25 5.1 = 51/10
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions