開始使用免費開始

整數與浮點數的除法

Python 提供兩種不同的除法運算子:///。在 Python 3 中,/ 會一律回傳浮點數結果,而 // 則是無條件捨去除法(floor division),會一律回傳整數結果。無條件捨去除法等同於執行 math.floor(numerator/divisor),也就是回傳小於或等於除法結果的最大整數。你可以在 Python Docs 進一步了解 math.floor

本練習屬於課程

Python 的資料型別

檢視課程

練習說明

  • 列印 2/11/2 的結果。
  • 列印 2//11//2 的無條件捨去除法結果。
  • 列印 2/12//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(____)
編輯並執行程式碼