Question about typing: ints/floats

Steven D'Aprano steven at
Thu Mar 4 03:00:02 CET 2010

On Wed, 03 Mar 2010 15:45:51 -0800, Wells wrote:

> But:
>>>> a = 1
>>>> b = 3
>>>> a / b
> 0
> This does not implicitly do the casting, it treats 'a' and 'b' as
> integers, and the result as well. Changing 'b' to 3.0 will yield a float
> as a result (0.33333333333333331)

This is  design flaw in older versions of Python, that the / operator for 
ints performs integer division rather than floating point division.

In Python 2.5 and beyond, you can fix this by calling 

from __future__ import division 

at the top of your module, and then / will perform float division with 
int arguments, and you can use // for integer division.

Or upgrade to Python 3.1, where this is the default behaviour and there 
is no need for the from __future__ call.


More information about the Python-list mailing list