Your Legal Resource
This module provides support for rational number arithmetic. It allows creating a Fraction instance from integers, floats, numbers, decimals, and strings.
Fraction Instances: A Fraction instance can be constructed from a pair of integers, from another rational number, or from a string. Fraction instances are hashable and should be treated as immutable.
------------------------------------------------------------------------------------------------------
from fractions import Fraction
print (Fraction(11, 35))
# returns Fraction(11, 35)
print (Fraction(10, 18))
# returns Fraction(5, 9)
print (Fraction())
# returns Fraction(0, 1)
------------------------------------------------------------------------------------------------------
Output :
11/35
5/9
0
------------------------------------------------------------------------------------------------------
from fractions import Fraction
print (Fraction(1.13))
# returns Fraction(1272266894732165, 1125899906842624)
------------------------------------------------------------------------------------------------------
Output :
1272266894732165/1125899906842624
------------------------------------------------------------------------------------------------------
from fractions import Fraction
print (Fraction('1.13'))
# returns Fraction(113, 100)
------------------------------------------------------------------------------------------------------
Output :
113/100
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
from fractions import Fraction
print (Fraction('8/25'))
# returns Fraction(8, 25)
print (Fraction('1.13'))
# returns Fraction(113, 100)
print (Fraction('3/7'))
# returns Fraction(3, 7)
print (Fraction('1.414213 '))
# returns Fraction(1414213, 1000000)
------------------------------------------------------------------------------------------------------
Output :
8/25
113/100
3/7
1414213/1000000
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
from fractions import Fraction
print (Fraction('3.14159265358979323846'))
# returns Fraction(157079632679489661923, 50000000000000000000)
print (Fraction('3.14159265358979323846').limit_denominator(10000))
# returns Fraction(355, 113)
print (Fraction('3.14159265358979323846').limit_denominator(100))
# returns Fraction(311, 99)
print (Fraction('3.14159265358979323846').limit_denominator(10))
# returns Fraction(22, 7)
print (Fraction(125, 50).numerator)
# returns 5
print (Fraction(125, 50).denominator)
# returns 2
------------------------------------------------------------------------------------------------------
Output :
157079632679489661923/50000000000000000000
355/113
311/99
22/7
5
2
------------------------------------------------------------------------------------------------------
Performing Mathematical operations on fractions
from fractions import Fraction
print (Fraction(113, 100) + Fraction(25, 18))
# returns Fraction(2267, 900)
print (Fraction(18, 5) / Fraction(18, 10))
# returns Fraction(2, 1)
print (Fraction(18, 5) * Fraction(16, 19))
# returns Fraction(288, 95)
print (Fraction(18, 5) * Fraction(15, 36))
# returns Fraction(3, 2)
print (Fraction(12, 5) ** Fraction(12, 10))
# returns 2.8592589556010197
------------------------------------------------------------------------------------------------------
Output :
2267/900
2
288/95
3/2
2.8592589556
------------------------------------------------------------------------------------------------------
Fraction-based calculations using various functions of math module
import math
from fractions import Fraction
print (math.sqrt(Fraction(25, 4)))
# returns 2.5
print (math.sqrt(Fraction(28,3)))
# returns 3.0550504633038935
print (math.floor(Fraction(3558, 1213)))
# returns 2
print (Fraction(math.sin(math.pi/3)))
# returns Fraction(3900231685776981, 4503599627370496)
print (Fraction(math.sin(math.pi/3)).limit_denominator(10))
# returns Fraction(6, 7)
------------------------------------------------------------------------------------------------------
Output :
2.5
3.0550504633
2.0
3900231685776981/4503599627370496
6/7