[Tutor] Loop over floating point values

Dominik George nik at naturalnet.de
Sun Dec 1 10:14:54 CET 2013


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

-- 
* concerning Mozilla code leaking assertion failures to tty without D-BUS *
<mirabilos> That means, D-BUS is a tool that makes software look better
            than it actually is.

PGP-Fingerprint: 3C9D 54A4 7575 C026 FB17  FD26 B79A 3C16 A0C4 F296
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 905 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/tutor/attachments/20131201/10122954/attachment.sig>


More information about the Tutor mailing list