[Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)?

Amit Saha amitsaha.in at gmail.com
Mon Aug 5 15:15:34 CEST 2013


On Mon, Aug 5, 2013 at 10:51 PM, Richard D. Moores <rdmoores at gmail.com> wrote:
>>>> import fractions
>>>> fractions.Fraction(6, 21)
> Fraction(2, 7)
>
> How do I turn that Fraction(2, 7) into "1/7"? (and not 0.2857142857142857...)

I think you meant, 2/7? Here is how you can extract the numerator and
the denominator (as your subject states):

>>> fractions.Fraction(6,21)
Fraction(2, 7)
>>> f=fractions.Fraction(6,21)
>>> f
Fraction(2, 7)

>>> f.numerator
2
>>> f.denominator
7


Also, of course, str(f) would give you 2/7.




-- 
http://echorand.me


More information about the Tutor mailing list