In Python 2, dividing two integers will yield a rounded integer result:

$ ipython2
Python 2.7.11 (default, Jan 22 2016, 08:29:18)
Type "copyright", "credits" or "license" for more information.

IPython 4.1.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: 1/2
Out[1]: 0

To get a real-number result:

In [2]: 1./2
Out[2]: 0.5

In [3]: float(1)/2
Out[3]: 0.5

In Python 3, the default is the real-number result:

$ ipython
Python 3.5.1 (default, Dec  7 2015, 21:59:10)
Type "copyright", "credits" or "license" for more information.

IPython 4.1.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [2]: 1/2
Out[2]: 0.5

In [3]: exit
>>> elapsed time 15s

In a Python script, to use Python 3 division behavior for Python 2:

import from __future__ division;
print 1/2

blog comments powered by Disqus