整數與浮點數的除法
Python 提供兩種不同的除法運算子:/ 與 //。在 Python 3 中,/ 會一律回傳浮點數結果,而 // 則是無條件捨去除法(floor division),會一律回傳整數結果。無條件捨去除法等同於執行 math.floor(numerator/divisor),也就是回傳小於或等於除法結果的最大整數。你可以在 Python Docs 進一步了解 math.floor。
本練習屬於課程
Python 的資料型別
練習說明
- 列印
2/1與1/2的結果。 - 列印
2//1與1//2的無條件捨去除法結果。 - 列印
2/1與2//1的型別。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Print the result of 2/1 and 1/2
print(____)
print(____)
# Print the floored division result of 2//1 and 1//2
print(____)
print(____)
# Print the type of 2/1 and 2//1
print(____)
print(____)