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

Chris “Kwpolska” Warrick kwpolska at gmail.com
Mon Aug 5 16:40:04 CEST 2013


On Mon, Aug 5, 2013 at 4:34 PM, Richard D. Moores <rdmoores at gmail.com> wrote:
> On Mon, Aug 5, 2013 at 6:15 AM, Amit Saha <amitsaha.in at gmail.com> wrote:
>> 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?
>
> Yes, sorry for screwing that up.
>
>  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
>
> Ah, better yet! Thanks! But where could I have found that in
> <http://docs.python.org/3/library/fractions.html#module-fractions>?
>
> Dick
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

> The Fraction class inherits from the abstract base class numbers.Rational, and implements all of the methods and operations from that class. […] In addition, Fraction has the following methods:

This basically translates to “go look numbers.Rational up if you want
more methods/properties”.  numbers.Rational is there:
http://docs.python.org/3/library/numbers.html#numbers.Rational

-- 
Chris “Kwpolska” Warrick <http://kwpolska.tk>
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense


More information about the Tutor mailing list