Python中的分数运算

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)
>>>

 

posted @ 2018-06-22 19:37  枧桥分享哥  阅读(1267)  评论(0编辑  收藏  举报