python loops

Paddy paddy3118 at
Fri Sep 1 20:33:41 CEST 2006

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

Someone else gave an apt reply to a similar question before, but I
don't know when so I'll give the gist:
In languages like C and Java, the for loop index variable is often used
to iterate over members of a sequence or other data type.
In Python many of the data types can be directly iterated over without
having to increment a separate indexing value.

C type language:

length = length_finder(some_datatype);
for(i=0; i<length; i++){
  indexed_data = some_datatype[i]

Becomes the more elegant Python:

for indexed_data in some_datatype:

- Paddy.

More information about the Python-list mailing list