Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to access denominator of a fraction in Python 2

Is there any way to access the denominator in a fraction in Python 2?

For example, I have a variable:

x = fractions.Fraction(4,1)

And I want to access its denominator. How do I do that?

like image 320
nsane Avatar asked Sep 07 '25 14:09

nsane


1 Answers

>>> from fractions import Fraction
>>> a = Fraction(1,2)
>>> a.denominator
2

Additionally Python's help() method can be very useful to determine exactly what methods and properties exist for an object. In the example above you could get a help description for the Fraction object by help(Fraction) or help(a) in an interpreter session.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!