始める無料で始める

整数と浮動小数点の除算

Python には 2 種類の除算演算子 /// があります。Python 3 では、/ は常に浮動小数点の結果を返し、// は切り捨て除算で、常に整数の結果を返します。切り捨て除算は math.floor(numerator/divisor) と同じで、除算結果以下で最大の整数を返します。math.floor については Python Docs で詳しく学べます。

この演習はコースの一部です

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(____)
コードを編集して実行