[Tutor] range query

Jose Amoreira amoreira@mercury.ubi.pt
Fri, 19 Jan 2001 15:10:29 +0000


Hello

kevin parks wrote:

> how come range only works with integers? Is there a way to do the same thing with floats-value steps? It seems very unPython like to me.
>
> why can't you do this:
> range(3, 0.5)
>
> [0.0 ,0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
>

Yes, it seems that the arguments to range are converted to integers before any action. But if you are running python 2.0 you can do it
using list comprehensions:

>>> [float(i)/2 for i in range(7)]
[0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]

Hope it helps,
Ze