How can I divide two numbers in Python 2.7 and get the result with decimals?
I don't get it why there is difference:
in Python 3:
>>> 20/15 1.3333333333333333 in Python 2:
>>> 20/15 1 Isn't this a modulo actually?
In Python 2.7, the / operator is integer division if inputs are integers.
If you want float division (which is something I always prefer), just use this special import:
from __future__ import division See it here:
>>> 7 / 2 3 >>> from __future__ import division >>> 7 / 2 3.5 >>> Integer division is achieved by using //, and modulo by using %:
>>> 7 % 2 1 >>> 7 // 2 3 >>> As commented by user2357112, this import has to be done before any other normal import.
import __future__ doesn't work.__future__ is almost always the recommended way/ is integer division and the result has decimals I would be taken by suprise, and the from __future__ import division may be more than a screenful away.In Python 3, / is float division
In Python 2, / is integer division (assuming int inputs)
In both 2 and 3, // is integer division
(To get float division in Python 2 requires either of the operands be a float, either as 20. or float(20))
20 mod 15 == 5float(20)/float(15)