[Verba] Re: wait!

Stuart D. Gathman stuart at gathman.org
Wed Feb 1 19:26:56 EST 2006


On Wed, 1 Feb 2006, Rebecca Chenette wrote:

> Wait I have a guess about the reason python doesnt seem to do fractions: Is
> it because python rounds everything? like I put in 2001/2 and got 1000. So
> does python just round?

When dividing integers, python does integer division.  You get just the
whole number part of the result.  For the remainder, use the % operator.
You can get both quotient and remainder with divmod().

For example, 
>>> 2001%2
1
>>> divmod(2001,2)
(1000, 1)
>>> divmod(2,8)
(0, 2)

If one of the numbers is floating point, python will do floating point
division.  E.g.
>>> 2.0/8
0.25

-- 
	      Stuart D. Gathman <stuart at bmsi.com>
Business Management Systems Inc.  Phone: 703 591-0911 Fax: 703 591-6154
"Confutatis maledictis, flamis acribus addictis" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.





More information about the Verba mailing list