Python's Not (Just) For Unicorns

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

Chapter 16

Being lazy with lists

But yes, yes, back to lists! If we wanted to say hello to each of our president friends, we might do this:

presidents = ["Washington", "Adams", "Jefferson", "Madison", "Monroe"]
print("Hello", presidents[0])
print("Hello", presidents[1])
print("Hello", presidents[2])
print("Hello", presidents[3])
print("Hello", presidents[4])

No, wait, sorry, that’s terrible. We keep seeing the prompt again and again and again, it just looks really messy! Let’s switch back to the editor style that we were using when we learned if statements.

I’ve written some code in the box below. Click the Run Python code button to, yes, run the Python code. You’ll see the results in the other box.

presidents = ["Washington", "Adams", "Jefferson", "Madison", "Monroe"]

print("Hello", presidents[0])
print("Hello", presidents[1])
print("Hello", presidents[2])
print("Hello", presidents[3])
print("Hello", presidents[4])

Whew, much nicer. The editor is 100% the right thing to use when working with a lot of data!

The editor is definitely nice, but there’s still one problem: this code is too much work. Just look at it! We use print again and again, and "Hello" again and again, and president + square brackets again and again.

Remember the first rule of programming: programmers are lazy! We don’t want to type the same thing again and again and again. If you were explaining this to Python in real life, you’d say something to her like, “say hello to each of the presidents.”

You can do that in Python, too. Click Run Python code to see what happens!

presidents = ["Washington", "Adams", "Jefferson", "Madison", "Monroe"]

for president in presidents:
  print("Hello", president)

This is called a for loop. It’s a way of doing the exact same thing to every single thing in a list. The little indented part is what gets done to every single president.

We can try it with numbers, too:

numbers = [1, 2, 3, 4]

for number in numbers:
  print("Looking at", number)

Works great! As long as our code is indented, it gets done to each number individually. You can indent with either two spaces or four spaces, but I like two (four is just SO MANY).

Before I talk any more, let’s talk about the most common error you’re going to run into when writing a for loop:

THE WRONG WAY

for number in numbers
  print(number)

THE RIGHT WAY

for number in numbers:
  print(number)

Can you spot the difference?

Pay attention to the colon : at the end of the for... line: you need that colon! If you don’t have it, Python will cry. She loves punctuation.

Try to write a for loop down below that prints each number, but don’t include the colon! Yes, write it the wrong way. It’s nice for us to see these errors on purpose before we do it accidentally.

numbers = [1, 2, 3, 4]

After you do it wrong, try to do it correctly (with the colon :). Make sure you’re indented for your print(number) line! If you’re lucky, it will do it automatically.

You’ll also have to hit enter one more time than you think, just to tell Python “yes please run this code.”

Fun fact: you can run multiple lines of code in the indented part! The code below will run the two print lines for each number.

numbers = [1, 2, 3, 4]

for number in numbers:
  print("Looking at", number)
  print("A bigger version is", number * 100)

A question everyone always asks is “how does Python know to call it president or number???” You think Python is being super smart and knows about singular nouns or whatever, but no, it’s all you. When we write for blahblah in numbers, the blahblah part is what our number is called when inside the loop.

For example, let’s try something dumb:

numbers = [1, 2, 3, 4]

for potato in numbers:
  print(potato * 10)

As long as we are inside of the indented part, we’re going to call each number potato. Why? Because we wrote for potato in numbers! Why? Well, I dunno, programming magic. That’s how Python works.

It might also help if we break it down into the long version, without the loop. I’ve added some spacing to help things visually, and included a step-by-step description below. This is the exact same thing the loop is doing, but with a billion more lines of code to write.

numbers = [1, 2, 3, 4]

potato = numbers[0]
print(potato * 10)

potato = numbers[1]
print(potato * 10)

potato = numbers[2]
print(potato * 10)

potato = numbers[3]
print(potato * 10)

That’s the long, step-by-step version:

  1. We start by building a list of all of the numbers.
  2. Then we take the first number, put it in a variable called potato, and print it times ten.
  3. Then we take the second number, put it in a variable called potato, and print it times ten.
  4. Then we take the third number, put it in a variable called potato, and print it times ten.
  5. Then we take the fourth number, put it in a variable called potato, and print it times ten.

The alternative is the for loop, which has a somewhat shorter description.

  1. We start by building a list of all of the numbers.
  2. Then we take each number, put it in a variable called potato, and print it times ten.
numbers = [1, 2, 3, 4]

for potato in numbers:
  print(potato * 10)

So much nicer!

For loops can be tough to wrap your head around when you first start programming, but it’ll get better as time goes on. Practice, practice, practice.

Oh, and are you from some other programming world where for loops look something like for(i=0; i < 10; i++)? Yeah, Python’s are definitely more friendly.

Chapter summary

We learned about for loops, which are ways of doing the exactly same thing to every item in a list. It’s important to use the correct punctuation and indentation with for loops or else everything breaks!