LoginSignup
0
1

More than 5 years have passed since last update.

pythonの除算|実数になる場合、整数として出力する場合

Last updated at Posted at 2019-02-10

pythonでは以下の場合に出力が実数になります。

割り算の場合

出力は常に実数になります。

math.py

>>> 10 + 20 * 3 / 4
25.0

式に実数が混じっている場合

math2.py

>>> 10.0 + 25
35.0  

整数を出力したい場合

整数を出力したい場合は // を使用します。
ただし//を使用した場合、式に実数が混じっていれば出力は実数となります。

math2.py

>>> 10 + 20 * 3 // 4
25
>>> 10 / 2
5.0
>>> 10 // 2
5
>>> 10.0 // 2
5.0
>>> 17 / 3
5.666666666666667
>>>
>>> 17 // 3
5
>>> 17 % 3
2
>>> 5 * 3 + 2
17

0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1