Looping using iterators with fractional values

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Sat Jan 1 15:26:24 EST 2005


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



More information about the Python-list mailing list