Python中的簡單計算

Python中的簡單計算

(1)基本的加減乘除
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5  1.6

2)除法總是會返回一個浮點數,想要返回整數,需要用“//”來表示(floor division),另外,可以用“%”進行取餘操作

>>> 17 / 3  # classic division returns a float
5.666666666666667
>>>
>>> 17 // 3  # floor division discards the fractional part
5
>>> 17 % 3  # the % operator returns the remainder of the division
2

3)冪運算可以使用“**”來進行

>>> 5 ** 2  # 5 squared
25
>>> 2 ** 7  # 2 to the power of 7
128

需要注意的是,

>>> -3**2 #it will be interpretedas -(3**2)

-9

如果需要得到正確的結果,需要下面的寫法

>>> (-3)**2

9

 

除了floatint類型的數據之外,還支持其他類型的數字,例如DecimalFraction,當然python也支持複雜的數據格式,一般都是以j或者J作爲後綴,例如3+5j


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章