[Tutor] range() or xrange() for non-integers?

Terry Carroll carroll at tjc.com
Sat Sep 13 13:33:41 EDT 2003


I found myself needing something like this:

  for x in xrange(-4.0, 4.0, 0.5):
      print x

(The print line will, of course, in practice, be something more useful.)


I found that xrange (and range) do not work except with integers.

What's the pythonic way to do this, under 2.2?

I ended up doing this, which seems more fortranic than pythonic:

  start = -4.0
  end = 4.0
  step = 0.5

  x = start
  while x <= end:
      print x
      x += step

[ yeah, I know, using < rather than <= would be the actual equivalent, but 
<= is actually what I need. ]

-- 
Terry Carroll        |   "I say to you that the VCR is to the American
Santa Clara, CA      |   film producer and the American public as the 
carroll at tjc.com      |   Boston strangler is to the woman home alone."  
                     |       Jack Valenti, MPAA President
Modell delendus est  |       Testimony before Congress, 1982




More information about the Tutor mailing list