Facebook Twitter Instagram
    Facebook Twitter Instagram Pinterest Vimeo
    Hand On CodeHand On Code
    Hand On CodeHand On Code
    Home»Feature»Fraction Module in Python
    Feature

    Fraction Module in Python

    March 15, 2023No Comments3 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Python Module for Fraction

    The Python Fraction module is used for rational number arithmetic. This module enables the creation of Fractional instances from integers, floats, numbers, decimal, and strings.

    What is an instance fractional?

    It is possible to build fractional instances using a pair of integers, a string, or another rational number. They should be handled as changeable hashables.

    How may a fractional instance be created?

    The Fraction module has the following built-in functions for creating fractions. Let’s examine the following fraction instance creation methods.

    • class fractions.Function(numerator = 0, denominator = 1): – This methods assigns numerator as 0 and denominator as 1 by default. If the denominator is equal to 0, it will raise the Zerodivision error. Let’s understand the following example.

    Example –

    1. from fractions import Fraction  
    2. print (Fraction(10, 37))  
    3. # returns Fraction(11, 35)  
    4.   
    5. print (Fraction(11, 18))  
    6. # returns Fraction(5, 9)  
    7.   
    8. print (Fraction())  
    9. # returns Fraction(0, 1)  

    Output:

    10/37
    11/18
    0
    
    • class fractions.Fraction(other_fraction) : This method takes the other_fraction instance of a number. It returns rational and a fraction instance with same value.
    • class fractions.Fraction(float) – This method takes the float value and returns the same value fraction instance.

    Example –

    1. from fractions import Fraction  
    2. print(Fraction(1/12))  

    Output:

    6004799503160661/72057594037927936
    
    • class fractions.Fraction(decimal) :- This method takes the decimal instance and returns the instance with the same value. Let’s understand the following example.

    Example –

    1. from fractions import Fraction  
    2. print (Fraction(‘2.26’))  

    Output:

    113/50
    
    • class fractions.Fraction(string) : – This method takes the string or Unicode as an argument and returns the instance with the same value. Let’s understand the following example.

    Example –

    1. from fractions import Fraction  
    2. print (Fraction(‘7/25’))  
    3. print (Fraction(‘1.23’))  
    4. print (Fraction(‘3/5’))  
    5. print (Fraction(‘1.414213 \t\n’))  

    Output:

    7/25
    123/100
    3/5
    1414213/1000000
    
    • limit_denominator(max_denominator=1000000) – It is used to find the approximation to given floating-point number. It returns the closet Fraction to self that has denominator at max_denominator. Let’s understand the following example.

    Example –

    1. from fractions import Fraction  
    2. print (Fraction(‘3.14159265358979323846’))  
    3. print (Fraction(‘3.14159265358979323846’).limit_denominator(10000))  
    4. print (Fraction(‘3.14159265358979323846’).limit_denominator(100))  
    5.   
    6. print (Fraction(‘3.14159265358979323846’).limit_denominator(10))  
    7. print (Fraction(125, 50).numerator)  
    8. print (Fraction(125, 50).denominator)  

    Output:

    157079632679489661923/50000000000000000000
    355/113
    311/99
    22/7
    5
    2
    

    Example 2: Apply a mathematical operation on fractional values

    1. from fractions import Fraction  
    2. print (Fraction(56, 12) + Fraction(18, 25))  
    3. print (Fraction(5, 2) / Fraction(12, 10))  
    4. print (Fraction(16, 15) * Fraction(26, 39))  
    5. print (Fraction(18, 5) * Fraction(15, 36))  
    6. print (Fraction(22, 5) ** Fraction(6, 10))  

    Output:

    404/75
    25/12
    32/45
    3/2
    2.4326050606703427
    
    Fraction Module in Python Learn Python free Python Code Python Course Free download python coursefree Courses Download Python Language
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleTesting in Django Part 2 Model Mommy vs Django Testing Fixtures
    Next Article Development and Deployment of Django on Fedora

    Related Posts

    python

    Plot With Pandas Python Data Visualization Basics

    March 27, 2023
    python

    Defining and Calling Python Functions

    March 27, 2023
    python

    BreadthFirst Search in Python

    March 27, 2023
    Add A Comment

    Leave A Reply Cancel Reply

    Facebook Twitter Instagram Pinterest
    © 2023 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.