3.8.1

Iteration: the repetition of a specific block of code Example of iteration:

  1. I am hungry
  2. Make food
  3. Eat food
  4. If not hungry, break
  5. Repeat -> 1
def make_food():
    pass

while True:
    make_food()
    if input("still hungry? ") == "n":
        break

3.8.2

Iteration statement: a statement that causes a specific block to repeat until a condition is met (ex. for, while)

for i in range(10, 0, -1):
    print(i)
10
9
8
7
6
5
4
3
2
1
xs = [3, 16, 29, 42, 55, 68, 81]
idx = 0
while idx < len(xs):
    print(xs[idx])
    idx += 1
3
16
29
42
55
68
81

3.10

nums = [38, 45, 67, 83, 78]
m = nums[0]
for n in nums:
    if n < m:
        m = n
print(m)
38

Lists quiz