Convert int to float

Lie Lie.1296 at gmail.com
Sun Mar 16 04:53:40 EDT 2008


On Mar 16, 4:43 am, Guido van Brakel <guidovb1 at invalid> wrote:
> Hello
>
> I have this now:
>
> > def gem(a):
> >     g = sum(a) / len(a)
> >     return g
>
> > print gem([1,2,3,4])
> > print gem([1,10,100,1000])
> > print gem([1,-2,3,-4,5])
>
> It now gives a int, but i would like to see floats. How can integrate
> that into the function?
>
> Regards,
>
> --
> Guido van Brakel
> Life is like a box of chocolates, you never know what you're gonna get
> --

Python 2's division operator's default behavior is to do integer
division whenever all of its operands are integers/long and do float
division if any of them are float/decimal, in Python 3, this is going
to be changed so that division would always be float division and
while integer division would have its own operator "//".

You can change the default behavior of Python 2 by importing division
behavior from __future__ module (from __future__ import division), or
you could convert one of the operands to float ("float(a) / b" or "a /
float(b)").



More information about the Python-list mailing list