angio
een er ue ued ead
Definite Loops
* Quite often we have a list of items of the lines in a file - effectively a
finite set of things
+ We can write a loop to run the loop once for each of the items in a
set using the Python for construct
+ These loops are called “definite loops” because they execute an
exact number of times
¢ We say that “definite loops iterate through the members of a set”angio
een er ue ued |
A Simple Definite Loop
be)
for i in [5, 4, 3, 2, 1] : 4
sss b eto em) 3
print ('Blastoff!') 2
i
is}
EK oiiangio
een er ue ued ead
A Definite Loop with Strings
Happy New Year: Joseph
Pratt eg eo) Cet Happy New Year: Glenn
for friend in friends : Happy New Year: Sally
print (‘Happy New Year:', friend)
print ('Done!')
Done!angio
een er ue ued ead
A Simple Definite Loop
Yes
i)
oe Cs )
print (i) 3
print ('Blastoff!') A
a
i=)
lastoff!
Definite loops (for loops) have explicit iteration variables
that change each time through a loop. These iteration
variables move through the sequence or set.eee eur renin
Looking at In...
* The iteration variable
“iterates” through the
sequence (ordered set)
Tr lileyaR-Uit-lol sequence
The block (body) of code is
executed once for each
value in the sequence
fecal
for i in [5, 4, 3, 2, 1]
print (i)
Bie oa rele me)
through all of the values in
fl t-te (01 pfes-)angio
een er ue ued ead
No
Ves Sete Mile-1 ela) 9) 1
through the sequence (ordered set)
print()
+ The block (body) of code is
executed once for each value in the
sequence
Ciba Mi ice leleola a lir- ome) 1)
for iin [5, 4, 3, 2, 1]: through all of the values in the
print (i) sequenceeen er ue ued
tere th fy 1, 2, ch, 2,
print (i)
Paogiess
Baranangio
een er ue ued ead
Definite Loops
* Quite often we have a list of items of the lines in a file - effectively a
finite set of things
+ We can write a loop to run the loop once for each of the items in a set
using the Python for construct
+ These loops are called “definite loops” because they execute an exact
number of times
¢ We say that “definite loops iterate through the members of a set”I Cc
Loop IdiomsPere ec ane) Any
Pel Ueto Mme lol)
Se ee eter ae aC)
r ere ner weeny
Information and ‘and made available under a
Cee er
Pee ee en een
attribution requirements ofthe license. If you make a change,
fee fee to add your name and organization tothe ist of
Cee eo a
oe a ee eos
SSR ual
eee ere Reed