Python中的分数运算,在Python标准库fractions中的Fraction对象支持分数运算。具体操作如下:
在windows下,通过cmd进到dos shell,输入python3进入到python shell中。
C:\Users\Administrator>python3
Python 3.7.0b3 (v3.7.0b3:4e7efa9c6f, Mar 29 2018, 17:46:48) [MSC v.1913 32 bit (
Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from fractions import Fraction
>>> x=Fraction(3,5)
>>> y=Fraction(3,9)
>>> x
Fraction(3, 5)
>>> x.numerator
3
>>> x.denominator
5
>>> x+y
Fraction(14, 15)
>>> x*y
Fraction(1, 5)
>>> x/y
Fraction(9, 5)
>>> x-y
Fraction(4, 15)
>>> x*2
Fraction(6, 5)
>>>
原文地址:https://www.cnblogs.com/zhn620/p/9215254.html
时间: 2024-10-08 21:11:54