Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

loops

Loops are probably one of the more conceptually difficult concepts for new programmers, but once they are used a bit in practice, it becomes easier to grasp.

Loops are ways of working with collections of data (like lists) by isolating each item from the list, one at a time. A loop will go through each item in a list, like a list of breakfast items, to do a specific action to each item, like to display the item.

Here, we've also introduced a new function, print(). This function will "print," or display, whatever data you pass into the parentheses.

Some historical context for the name of the print() function: it comes from a time before screens, when computers literally printed the output of their computations.

Loops work with strings as well. We might print each character (or letter) from a string like "hello":

The syntax for writing a loop contains two lines. The first line identifies one item from a collection of items, such as a list. You can think of the first line saying something like "for (each) item (inside this) breakfast (list)." Then, the second line specifies the action done to that item, in this case, the instruction to print that item.

You can think of the loop as doing the following on each line:

for each item inside breakfast:
  display each item

a note on variable names

The variable which indicates each individual item (item) can be assigned to any name. That's because the variable is assigned on the fly, as the loop is being executed. This goes against usual practices with variable assignments, which always need to be assigned beforehand, or there will be a NameError because the variable hasn't been defined.

h
e
l
l
o

For example, we could name the individual item x, or any other letter, and the loop would run just fine. However, it's helpful to give as meaningful names as possible, to avoid code that gives no indication about the data contained in the variables. This will be really useful for other people reading your code as well. The idea is to be semantically meaningful while keeping your code as concise as possible.

When using variables in loops, the convention is to use the letter i as shorthand for indicating an item from a list.

string methods

Loops are great ways to do things en masse to data, such as to individual words within a text, in other words, to string type data that is contained within a list.

For example, you might want to lowercase all of the letters from our dataset to prepare it for analysis (explained in further detail in the “Text Cleaning” workshop). To do that, use the lower() method.

Now, let's combine this string method with what we know about loops. Let's try it on a list of items with varying levels of capitalization.

Here, the loop goes through each item in the list of "cities," and lowercases that item while also printing it (remember the print() function?). Here, the lower() method is attached to the "city" variable, which is assigned to each item, or city name, in the list.

Let's try to do the same with a longer list, like the text from the "Feminist Data Manifest-NO". First, though, we need to split our text, which is currently one long string, into individual strings within a list. For that, we use the string method, split(), which we saw briefly above when we talked about list methods.

We can see our text as a list of strings, saved to the variable "words." Now, we can do things programmatically to each string in the list, like lowercase the items.

This loop goes through each item in the list of words, lowercases it, then adds that word to a new list, called words_lower.

Notice here that I've introduced a few new things:

  1. Above the loop, I've created an empty list, called words_lower where we will eventually drop our lowercased words.

  2. The first line of the loop uses the i variable to stand in for each “item” in the list, which is a popular convention.

  3. The last line of the loop uses a list method to add data to our empty list. Notice that the list method append() is attached to a list object, while the string method lower() is attached to a string object.

group challenge

First, look up f-strings in python. Then, write a loop that prints out the square of the first 5 prime numbers (1, 3, 5, 7, 11), in individual strings like, “the square of 1 is 1”.

Solution (but don’t look unless you’ve tried to solve the challenge for 5 minutes!):

prime_numbers = [1, 3, 5, 7, 11]
for i in prime_numbers:
    print(f'the square of {i} is {i*i}')