1. 导入Fraction
from fractions import Fraction
fractions 位于python/python36/fractions.py下面,学会查看源码,并且通熟一遍。
2. Fractions 定义
class Fraction(numbers.Rational):
"""This class implements rational numbers. 实现有理数
In the two-argument form of the constructor, Fraction(8, 6) will
produce a rational number equivalent to 4/3(自动约分). Both arguments must
be Rational. The numerator defaults to 0 and the denominator
defaults to 1 so that Fraction(3) == 3 and Fraction() == 0. (分子默认是0,而分母默认是1)
Fractions can also be constructed from:
- numeric strings similar to those accepted by the
float constructor (for example, '-2.3' or '1e10')
- strings of the form '123/456'
- float and Decimal instances
- other Rational instances (including integers)
&#