The answer for dividing integers is different between python2 and 3.
python2
>>> 1/2
0
python3
>>> 1/2
0.5
If you're designing code that works on both python2 and python3
If you expect 1/2 = 0.5
:
python2or3
>>> 1./2.
0.5
If you expect 1/2 = 0
:
python2or3
>>> int(1/2)
0
You should do it like this.
By using the __future__
module, it seems that division can be performed in python2 series as well as in python3 series. The __future__
module has a function that complements the compatibility of python2 series and python3 series, and imports division
in it.
python2or3
from __future__ import division
print(1/2) # => 0.5
print(1//2) # => 0
--Comment by @ LouisS0616 -future module --- Salinger's programming diary