Alternative to standard C "for"
BJörn Lindqvist
bjourne at gmail.com
Sat Feb 5 13:49:48 EST 2005
> I am quite new to Python, and have a straight & simple question.
> In C, there is for (init; cond; advance). We all know that.
> In Python there are two ways to loop over i=A..B (numerical.):
> 1) i = A
> while i<B:
> ...do something...
> i+=STEP
This is indeed quite ugly. You rarely need such loops in Python and
with some thinking you can often translate the C-equivalent to
something more pythonic. As you guessed, your second problem is best
solved with a generator function - xrange(). It is completely equal to
range() except that it returns a generator instead of a list.
--
mvh Björn
More information about the Python-list
mailing list