for
loop
The
for
loop is another control-flow statement in Python; it allows a program to repeatedly execute a block of code. The process of doing the same thing repeatedly is called
iteration
. Want to traverse a list and do some computation with each element? Use a
for
-loop. Want your program to do something a set number of times? Use a
for
loop.
for
loop
for
loops on lists (
stub1.py
,
accum1.py
)
for
loop to perform
definite iteration
--repeating some action a set number of times
Optionally, Read: 4.2 and 10.3
Slides: individual slides for viewing , 6-up layout for printing
Examples:
forLoops.py
Materials from for-loop catch-up session, Mon Mar 21, 2022
To download the above .py files, right-click (Windows) or command-click (Macs) on the filename and then select "Save link as".
This way
you
choose where the files will be saved, instead of having your computer save to the default folder (from which you will later have to move your files for organization).
Questions and Answers: check the answers — after you try them on your own!