Looping problems usually involve lists, minimums, maximums and counting. A few examples might be

  • Given a list of people and salaries, who makes the most money?
  • Given a list of restaurant reviews, what percentage are above 4 stars?
  • Given a list of movie ratings, which got the worst score?

NOTE: To be perfectly honest, there are a lot of other ways of doing these problems besides for loops, and most all of them are actually better! This is a good way to think through it for beginning Python students, though.

Overview

You have three steps to any sort of problem that involves loops or lists.

  1. The Initial Condition: You need to start somewhere. For example: If I’m counting dogs, how many dogs do I have before I even start counting? Zero, probably.
  2. The Conditional: Every time you go through the loop, you probably test something to see if you should make a change. If I’m counting pit bulls, it might be something like is this a pit bull? if it is, let’s do something.
  3. The Change: If the conditional is true, I need to do something. Maybe it’s since this was a pit bull, let’s incrementing the pit_bull counter.

It doesn’t count as a step, but there’s also The Summary, where you display your answer.

Type 1: Counting

Definition: Given a list of numbers/objects/etc, how many fulfill some sort of condition?

  1. The Initial Condition: You need a counter. The counter starts at zero, because you haven’t counted anything yet.
  2. The Conditional: Depends on your question!
  3. The Change: Add one to your counter.

In words

Data set: We have twenty dogs, and are keeping track of how many pit bulls there are.

Initial condition: We haven’t counted any pit bulls yet, so we’ve only counted 0.

The loop: We walk down the line of dogs, stopping at each one. Is that dog a pit bull? (conditional) - if it is, we make a little counting mark on our page (the change).

The summary: When we’re done, we look at how many marks we’ve made on our page.

In code

Let’s find out how many of our numbers are greater than 100.

# This is our data set
numbers = [5, 20, 377, 123, 1, 384, 0]

# THE INITIAL CONDITION: Before we've counted, we've found ZERO numbers greater than 100
large_numbers_count = 0

# THE LOOP...
for number in numbers:
  # THE CONDITIONAL: check to see if the number is larger than 100
  if number > 100:
    # THE CHANGE: since the number is greater than 100, let's increment our counter
    large_numbers_count = large_numbers_count + 1

# THE SUMMARY: print it out
print("We found", large_numbers_count, "numbers larger than 100")

Type 2: Minimum/maximum

Definition: Given a list of numbers/objects/etc, which one is the largest/smallest? For the text below, we’ll always talk about the largest.

  1. The Initial Condition: You need an largest. You have to pretend something is the biggest before you start, so just pick the first element. If it isn’t the largest it will be replaced later. Our cat is the richest person we know.
  2. The Conditional: Is the one we’re looking at in the loop bigger than the one we’ve saved as our largest? e.g. Is Bill Gates richer than our cat? Yes.
  3. The Change: Change the largest to be a new value Bill Gates is now the richest person we know.

In words

We have four people with certain amounts of money:

  • our cat ($0)
  • Kanye West ($300)
  • Bill Gates ($900), and
  • the mailman ($200)

The question is, who is the richest?

Initial Condition: Before we start, we’re write down “CAT, $0” as the richest person we know. Now we’re going to visit all four people (the loop).

  • First, we visit our cat. Is our cat ($0) richer than our cat ($0)? (conditional) Well… no, I guess. So we move on.
  • Next, we visit Kanye. Is Kanye ($300) richer than our cat ($0)? Yes! The change: So now we scratch off our cat’s info and write “KANYE, $300” as the richest.
  • Next, we visit Bill Gates. Is Bill Gates ($900) richer than Kanye ($200)? Yes! So now we scratch off Kanye’s info and write “BILL GATES, $900” as the richest.
  • Next, we visit the mailman. Is the mailman ($200) richer than Bill Gates ($900)? No, so we move on.

Summary: Finally, we look at the name we’ve written down as the richest: BILL GATES, $900.

NOTE: Be sure to keep track of the right amount of information! If we had just kept track of the richest person’s name in the example above, we wouldn’t have been able to compare how rich they were in the conditional. We needed to keep track of their name to display at the end, along with their wealth to do the comparison.

In code

Let’s find the largest number in our data set.

# This is our data set
numbers = [5, 20, 377, 123, 1, 384, 0]

# THE INITIAL CONDITION: Let's just say the first element is the largest. It might be! If it isn't, it will be replaced with something larger later.
largest = numbers[0]

for number in numbers:
  # THE CONDITIONAL: is the number we're looking at bigger than what we already have as the largest?
  if number > largest:
    # THE CHANGE: since we've found out that "number" is bigger than "largest", let's replace "largest" with whatever "number" is
    largest = number

# THE SUMMARY: print it out
print("Our biggest number is", largest)