for Loops#
Iteration over … Something#
Iteration: a central concept everywhere
Programs build and manipulate data
… and occasionally (most often?) iterate over data
⟶ Specialized looping construct:
for
for name in ['Caro', 'Johanna', 'Eva', 'Jörg']:
print(name)
name: loop variableBound to the current element in the list, four times in a row
A list is iterable - many other types participate in this game
break, continue, else#
Just as with while: usual looping features
breakends the loop -elseclause not executedcontinueexecutes block with next element (if any)
haystack = ['straw', 'mouse', 'straw', 'needle', 'straw']
for item in haystack:
if item == 'needle':
break
else:
print("couldn't find needle")