[Tutor] eval and floating point

Vern Ceder vceder at canterburyschool.org
Thu Jan 15 03:31:13 CET 2009

Mr Gerard Kelly wrote:
> Thanks very much
> I've noticed that the eval() function gives an integer, so eval("3/2")
> gives back 1. float(eval("3/2")) doesn't seem to work, any way to get a
> floating point number back with eval()?
> I know you can just do ("3./2."), but is there any way to do it with
> just ("3/2")?

If you are using a current version of Python, the line:

from __future__ import division

will make division with the "/" return a float; you then use "//" for 
integer division....

 >>> from __future__ import division
 >>> eval("3/2")
 >>> eval("3//2")


This time for sure!
    -Bullwinkle J. Moose
Vern Ceder, Director of Technology
Canterbury School, 3210 Smith Road, Ft Wayne, IN 46804
vceder at canterburyschool.org; 260-436-0746; FAX: 260-436-5137

More information about the Tutor mailing list