Alternative to standard C "for"
Georg Brandl
g.brandl-2005-01 at gmx.net
Sat Feb 5 14:03:32 EST 2005
BJörn Lindqvist wrote:
>> 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.
Slight terminology glitch -- it does return an iterator, not a
generator. Generators are functions that return iterators.
regards,
Georg
More information about the Python-list
mailing list