Loops

For loops

for cur_element in some_sequence:
suite
else:
suite
  • Iterates over the elements of a sequence--string, tuple, list, or other iterable object--in the order that they appear in the sequence.
  • The else suite is executed after the for loop exits, unless the loop exited with a break.
  • Use the built-in range function to iterate a number of times.
for element in some_list:
print element
else:
print 'End of list'

While loop

while expr:
suite
while expr:
suite
else:
suite
  • The else suite is executed after the while loop exits, unless the loop exited with a break.

Jump statements

  • break  : Immediately exits the for or while loop, without executing the else suite, if one is present.
  • continue  : Immediately starts the next iteration of the for or while loop.