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?
divisionpythonpython-2.7python-3.x
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?
Best Answer
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:
See it here:
Integer division is achieved by using
//
, and modulo by using%
:As commented by
user2357112
, this import has to be done before any other normal import.