for with decimal values?

Mark Dickinson dickinsm at
Sat May 2 18:45:47 CEST 2009

On May 2, 4:12 pm, Esmail <ebo... at> wrote:
> Is there a Python construct to allow me to do something like
> this:
>     for i in range(-10.5, 10.5, 0.1):
>       ...

I'd recommend using integer arithmetic:

for ten_times_i in range(-105, 106):
    i = ten_times_i / 10.0

This has the advantage that it's clear that
you want to include the endpoint 10.5. (*Did*
you want to include the endpoint?)

Presumably you'd want the float version
to be interpreted as:

i = -10.5
while i < 10.5:
    i += 0.1

(which isn't *quite* right, because the
value of i that you end up with *after* the
while loop has run to completion is 0.1
more than the last value used within the

The big problem with this is that 0.1
is not exactly representable as a float, and
so the successive additions in the
assignment i += 0.1 are subject to
(small) rounding errors.  As a result, it's
not clear whether the value of i for the last
loop is going to be 10.4+small_error or


More information about the Python-list mailing list