Re: python loops
- From: "Kay Schluehr" <kay.schluehr@xxxxxxx>
- Date: 31 Aug 2006 12:14:22 -0700
Putty wrote:
In C and C++ and Java, the 'for' statement is a shortcut to make very
concise loops. In python, 'for' iterates over elements in a sequence.
Is there a way to do this in python that's more concise than 'while'?
C:
for(i=0; i<length; i++)
python:
while i < length:
i += 1
As AlbaClause had demonstrated you can iterate over indices as well
using the for-statement and the range() function. But you can also
combine iterating over elements and indices at the same time using the
builtin enumerate() type:
for i, item in enumerate(("s1","s2")):
print i,item
0 s1
1 s2
.
- References:
- python loops
- From: Putty
- python loops
- Prev by Date: Re: SQLObject or SQLAlchemy?
- Next by Date: Re: python loops
- Previous by thread: Re: python loops
- Next by thread: RE: python loops
- Index(es):
Relevant Pages
|