[Tutor] floating point range() ?
Dave S
pythontut at pusspaws.net
Sat Jun 19 07:16:15 EDT 2004
>
>
>
>Hi Dave,
>
>
>I don't think there is a built-in equivalent for range() with floats.
>However, it isn't too bad to write something ourselves:
>
>###
>def frange(start, stop, skip=1.0):
> """A range-like generator that returns numbers in the interval
> [start, stop).
> """
> i = start
> while i < stop:
> yield i
> i += skip
>###
>
>
>
>Here's an example of how to use frange():
>
>###
>
>
>>>>for x in frange(0, 10, 0.5):
>>>>
>>>>
>... print x
>...
>0
>0.5
>1.0
>1.5
>2.0
>2.5
>3.0
>3.5
>4.0
>4.5
>5.0
>5.5
>6.0
>6.5
>7.0
>7.5
>8.0
>8.5
>9.0
>9.5
>###
>
>Does this seem reasonable?
>
>
>
>Hope this helps!
>
>
Sure does, It also reminds me of the yield builtin.
Things are getting clearer - though I still catch myself thinking in
terms of BASIC & forgetting to open up to the flexability of Python -
Guess it will just take time & practice.
Dave
More information about the Tutor
mailing list