443,818 Members | 1,282 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

# Looping using iterators with fractional values

 P: n/a Hello, Making the transition from Perl to Python, and have a question about constructing a loop that uses an iterator of type float. How does one do this in Python? In Perl this construct quite easy: for (my \$i=0.25; \$i<=2.25; \$i+=0.25) { printf "%9.2f\n", \$i; } Thanks in advance for your help. Daran Rife da**********@PAMyahoo.com Jul 18 '05 #1
6 Replies

 P: n/a drife wrote: Hello, Making the transition from Perl to Python, and have a question about constructing a loop that uses an iterator of type float. How does one do this in Python? Use a generator: def iterfloat(start, stop, inc): .... f = start .... while f <= stop: .... yield f .... f += inc .... for x in iterfloat(0.25, 2.25, 0.25): .... print '%9.2f' % x .... 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 2.25 Jul 18 '05 #2

 P: n/a drife wrote: Hello, Making the transition from Perl to Python, and have a question about constructing a loop that uses an iterator of type float. How does one do this in Python? In Perl this construct quite easy: for (my \$i=0.25; \$i<=2.25; \$i+=0.25) { printf "%9.2f\n", \$i; } <=Py2.3: for i in [x/4.0 for x in xrange(1, 10)]: print "%9.2f" % i Py2.4: for i in (x/4.0 for x in xrange(1, 20)): print "%9.2f" % i Reinhold Jul 18 '05 #3

 P: n/a Mark McEahern wrote: drife wrote: Hello, Making the transition from Perl to Python, and have a question about constructing a loop that uses an iterator of type float. How does one do this in Python? Use a generator: >>> def iterfloat(start, stop, inc): ... f = start ... while f <= stop: ... yield f ... f += inc ... >>> for x in iterfloat(0.25, 2.25, 0.25): ... print '%9.2f' % x ... 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 2.25 >>> Or use the numarray module: py> import numarray as na py> for f in na.arange(0.25, 2.25, 0.25): .... print '%9.2f' % f .... 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 Steve Jul 18 '05 #4

 P: n/a "drife" writes: Hello, Making the transition from Perl to Python, and have a question about constructing a loop that uses an iterator of type float. How does one do this in Python? In Perl this construct quite easy: for (my \$i=0.25; \$i<=2.25; \$i+=0.25) { printf "%9.2f\n", \$i; } Generally, you don't loop incrementing floating point values, as you're liable to be be surprised. This case will work as you expect because .25 can be represented exactly, but that's not always the case. Python loops are designed to iterate over things, not as syntactic sugar for while. So you can either do the while loop directly: i = 0.25 while i <= 2.25: print "%9.2f" % i i += 0.25 Or - and much safer when dealing with floating point numbers - iterate over integers and generate your float values: for j in range(1, 9): i = j * .25 print "%9.2f" % i http://www.mired.org/home/mwm/ Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. Jul 18 '05 #5

 P: n/a Mike Meyer wrote: Or - and much safer when dealing with floating point numbers - iterate over integers and generate your float values: for j in range(1, 9): i = j * .25 print "%9.2f" % i There's a glitch there, though - should be range(1, 10). Reinhold PS: I'm wondering whether my genexp approach or this one is preferable. Readability is equal, I would say, but how about speed? Brought up a few timeits: Python 2.3 ---------- for i in [x/4.0 for x in range(1, 10)]: 36,9 sec for j in range(1, 10): i = j * 0.25: 33,7 sec Python 2.4 ---------- for i in (x/4.0 for x in range(1, 10)): 32,5 sec for j in range(1, 10): i = j * 0.25: 28,4 sec So what does that tell us? (a) don't use genexps where there is a simpler approach (b) Py2.4 rocks! Reinhold Jul 18 '05 #6

 P: n/a Mike Meyer wrote: Or - and much safer when dealing with floating point numbers - iterateover integers and generate your float values: for j in range(1, 9): i = j * .25 print "%9.2f" % i I agree with this suggestion. As an historical aside, Fortran had loops with floating point variables for decades, but in 1995, the first standard in a while to REMOVE features, this was one of the few things deleted. The Fortran standards committee is very conservative about creating backwards incompatibilities, but they must have thought loops with floating point variables are so error-prone -- and alternatives with integer counters are so easy to write -- that they put their foot down. I know the OP is asking about Python, but the principle is the same. Jul 18 '05 #7

### This discussion thread is closed

Replies have been disabled for this discussion.