Home Computers Computer Languages Division in Python
Division in Python PDF Print E-mail
Written by Gordon Tillman   
Tuesday, 09 December 2008 14:55

The normal behavior in Python when dividing two Integers is to give an Integer result. This is true at least up through Python version 2.5. In the future this will change so that division will yield a floating point number. If you want that future behavior now, just include this line in you Python program file:

>>> from __future__ import division

It's interesting to see what this division that you imported is:

>>> division
_Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)

The (2, 2, 0, 'alpha', 2) indicates that this division feature was first available in Python 2.2. The (3, 0, 0, 'alpha', 0) indicates that thsi division feature will probably become standard in Python 3.0. The 8192 is the bit-field flag that should be passed into the compile() function to enable the division feature in dynamically compiled code.

Here is an example:

>>> 6/7
0
>>> from __future__ import division
>>> 6/7
0.8571428571428571
>>> 
Last Updated on Tuesday, 09 December 2008 14:59