(PPT) Iteration in Python
(PPT) Iteration in Python
• Another approach:
• Iterate over all members in a collection
• Called bounded iteration
while for
• Loop counter should be • Loop counter initialized
initialized outside loop automatically
• Includes continuation test • Continues while more elements in
before body the collection
• Should update loop variables • Loop variable updated automatically
in body of loop – do not update in loop
• Body contains steps to repeat • Body contains steps to repeat
What does this function do?
def smaller(L, x): • How many iterations would
p=0 smaller([10, 8, 6], 3) involve?
while p < len(L):
if L[p] < x: • smaller([7, 10, 2], 8)?
return p
else: • smaller(L, x) for any L and x?
p = p+1
return False
What does this function do?
def mult_table(n):
table = []
for r in range(n):
row = []
for c in range(n):
row.append(r*c)
table.append(row)
return table