Integer Division

If you're using Python 2.x integer division can be a real gotcha for most scientists. The problem is that in Python 2 if you divide two integers by one another you always get another integer.

In [1]:
9 / 2
Out[1]:
4

Of course, we all know that 9 / 2 is equal to 4.5, but the folks who write programming languages often want operations involving two objects of the same type to return an object of that type. So, since 9 and 2 are both integers Python 2 returns 4 (i.e., it gets rid of the remainder of the division).

Fortunately, the good folks who wrote Python have seen the light, and the newest version of Python, Python 3, divides integers as we expect. If you're still using Python 2 (and most scientists still do because a lot of important scientific modules don't work in Python 3 yet) you can make it function in the way you'd expect by using a special command.

In [2]:
from __future__ import division
9 / 2
Out[2]:
4.5

The idea is that you're asking Python to use the form of division from it's future self. You're importing the function division from the future. The lines on either side of the word future are double underscores, which is Python's way of indicating something special or non-standard.

If you're worried about getting caught by this strange behaviour (a common issue) you can always add the import line to the beginning of every program just to be safe.