[Tutor] eval and floating point

Jervis Whitley jervisau at gmail.com
Thu Jan 15 03:26:29 CET 2009


On Thu, Jan 15, 2009 at 1:19 PM, Mr Gerard Kelly <s4027340 at student.uq.edu.au
> 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")?
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>

Hi,

at the top of your script you can write:

from __future__ import division

now

>>> eval("4/3")
1.33333

You can read up on it here if you are interested..

http://www.python.org/dev/peps/pep-0238/

Cheers
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090115/b01d1032/attachment.htm>


More information about the Tutor mailing list