[Tutor] Loop over floating point values

Mark Lawrence breamoreboy at yahoo.co.uk
Sun Dec 1 12:32:27 CET 2013


On 01/12/2013 09:14, Dominik George wrote:
> Hi,
>
>> - Do not create a list of the floating point values as i=[0.01, 0.02,
>> 0.03..] - either like that or by using a suitable mathematical formula
>> combined with a list comprehension
>
> You could simply write your own version of xrange that does it, as a
> generator:
>
>    def xrange_f(start, stop, step):
>        x = start
>        while x < stop:
>            yield x
>            x += step
>
> Then, in your code, you can do:
>
>    for f in xrange_f(0, 10, 0.01):
>        pass
>
> -nik
>

To encourage Python 3 take up this should be range rather than xrange :)

-- 
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence



More information about the Tutor mailing list