Loops, and where your data lives

INPUT · Slides

Putting the tools together

01 / 06

Lay out the tools you have picked up

Let us lay out what this chapter has given you.

  • Repeating … for / range / while / break / continue
  • Lists … [ ] / taking items out by number / append / len / sorted / slices
  • Strings … by number / slices / in / split / join
  • Dictionaries and tuples … the container you look up by name, and the set of values you cannot change afterwards

Add the variables and branching from chapter one and that is the lot. There is no new grammar left. From here the practice is in choosing which one to reach for.

02 / 06

for, or while

Both of them repeat, but there is a rule of thumb for picking one.

  • The number of goes is settled in advance — for. "Three times", "once for every item in the list"
  • The end is decided by a condition — while. "For as long as there is enough stock"

When you are unsure, ask yourself whether you could say how many goes there will be before writing it. If you can, it is a for.

stock = 10shipments = 0while stock >= 3:    stock -= 3    shipments += 1print(shipments)print(stock)

Result

3
1

03 / 06

Go round, pick out, pile up

A problem that looks big stops being frightening once you split it into three jobs.

  • Go round … take one item at a time with for
  • Pick out … let through only what matches, with if
  • Pile up … add the total or the count into a variable that lives outside the loop

Make the variable you are piling into inside the loop and it drops back to 0 on every go. Keeping it outside is the trick.

scores = [55, 78, 92, 61]passed = 0for score in scores:    if score >= 70:        passed += 1print(passed)

Result

2

04 / 06

Telling break from continue

Both of them change the flow of a loop, but their jobs could not be more different.

  • break … no need to go round any more. You found what you were after, or the end has come
  • continue … skip this one item only. You still want to see what follows

"Found it, so break" and "do not want this one, so continue" is an easy way to keep them apart.

prices = [120, 300, 80, 450]for price in prices:    if price < 100:        continue    print(price)

Result

120
300
450

05 / 06

range and len only when you need the number

To go through a list plainly, for fruit in fruits: is enough. Reach for for i in range(len(fruits)): only when you need the number.

You need it for things like "number them 1. 2. 3." or "line two lists up position by position". Anywhere else it only sprinkles fruits[i] through the code and makes it harder to read.

fruits = ["apple", "orange"]for i in range(len(fruits)):    print(f"{i + 1}. {fruits[i]}")

Result

1. apple
2. orange

06 / 06

When it will not work, look at the middle

When something does not behave the way you expected, showing the values partway through is the fastest way in.

Drop a print(i) or a print(total) into the loop for a moment and watch what is in there on each go. You will always find the place where the picture in your head and the actual value part company.

Right — ten questions. They get bigger as you go.