Python's Not (Just) For Unicorns

An interactive introduction to programming in Python, for human beings and whoever else

Chapter 19

Counting with loops

For loops are really really really useful, but it can take a little work to start thinking about them in the “right” way. So I figure we can spend some time learning programmatic thinking while working with them - the process of converting a human idea into something a computer can understand. Just because you know what you want the computer to do, it doesn’t always mean you know how to tell the computer to do it!

For example: let’s say I have a bunch of ducks. How many ducks? I don’t know, we should count them! When I ask you how to do that, it seems like a pretty dumb question. You just, I don’t know, count them, right? But how do you get a computer to do it?

Honestly we would just use len(ducks), but let’s pretend that len doesn’t exist.

If we combine loops with the whole incrementing variables thing we learned last chapter, we can try to see how many times we’ve gone through the loop.

# We have a handful of numbers
numbers = [10, 20, 30, 40, 50, 60]

# Start from zero
count = 0
print("Count starts off at", count)

for number in numbers:
  print("----")
  count = count + 1

# Display the final count
print("Count finishes at", count)

We see that it seems to work, but it might not be clear how it works.

Inside the loop Python does two things: she prints out ---- and adds 1 to count. Each time she goes through the loop, she prints out --- and count gets one bigger.

Six numbers means six times through the loop. This means six times incrementing count, so count ends up as 6.

But to help us see it happen, add two new lines inside the loop - first, one to print out the current number you’re looking at, and then another to print the current value of count.

  • Hint: Remember to indent to make your new code be “inside” the loop
  • Hint: Print count out after you add one to it
  • Hint: Remember, this should be inside the loop - you want this to happen each time you’re looking at a different number. You’ll need to indent!
  • Hint: You can use print(number) to print out the current number
  • Hint: You can use print(count) to print out the current value of count
  • Hint: Both of those print statements need to be indented to the same level as count = count + 1
  • Hint: Remember to print count after you add one to it, otherwise you won’t get confetti!

Indenting is very very important when dealing with loops. Why? Well, run the code below and see that it counts up to 6 correctly.

numbers = [10, 20, 30, 40, 50, 60]
count = 0
for number in numbers:
  print("Looking at", number)
  count = count + 1

print("Count finishes at", count)

Now, remove the indent before count = count + 1. How does the output change?

When you remove the indenting, you’re no longer incrementing every time you’re inside of the loop. Instead, it doesn’t increment until the loop is totally done. Yes, even though it’s right below the loop, only the indented parts are run as part of the loop. When we remove the indenting, we take count = count + 1 out of the loop, so it only gets run once (after the loop is done). That’s why count is only 1 when you run the edited code!

Python loves punctuation, and Python loves indenting. Using the wrong punctuation usually makes the program yell at you and not run, but using the wrong indenting usually just makes you get the wrong result. It’s tricky!

But back to counting in loops: if we want to talk about what we’re doing as programmatic thinking, we can break it down into a few steps.

  1. Stage One: Initial value. Before we start, how many numbers have we counted? Zero! So count = 0.
  2. Stage Two: Increment. This is a fancy word for saying “make something one bigger.” In this case, we increment every time we go through the loop by using count = count + 1
  3. Stage Three: Answer. That’s just count! We print it out and we’re done.

Okay, let’s get back to those ducks. Remember how we wanted to count them without using len? Give it a try. You’re only responsible for stage two here.

ducks = ["Leonard", "Starburst", "Leopold", "Lackluster"]

# Initial state: ZERO ducks
count = 0

for duck in ducks:
  print("Looking at", duck)

# Answer: How many ducks?
print("We found", count, "ducks")
  • Hint: Pay attention to indenting!
  • Hint: You’ll use count = count + 1 to make count one bigger each time you look at a duck
  • Hint: Add count = count + 1 right after you print the duck’s name.
  • Hint: Make sure you indent your incrementing line so it happens inside of the loop!

Chapter summary

We started to learn about programming thinking, where we break down ideas into steps we can tell Python about. We also learned the word increment, which means to make someting one bigger.