1. 程式人生 > >Python中的分數運算

Python中的分數運算

img 分享 windows In 分數 format user 操作 nbsp

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

技術分享圖片

Python中的分數運算