Loops

Imagine you peel potatoes. You take the first potato from the bag, peel it, put it in the pot. Then take another potato, peel it, put it away - until all the potatoes are peeled. Now, imagine that you have to program the robot that does this. How do you get down to it?

The simplest yet least effective idea is to write as many operations of this sort: take the potato - peel - put in a pot, as there are potatoes in the bag. It's a bad idea, because:

The easiest and most effective way is to write the program the following way:

  1. if there are still potatoes in the bag:
    • Take a potato,
    • peel it,
    • put it in the pot,
  2. go back to point one.

This way will always work, no matter if we have three potatoes to peel, ten or one hundred and fifty.

Loop structure

In the Python language (and in all others too) there are special structures called loops. Loops are the repetitions of a set of actions. There are many ways to write loops, but we Python coders, most often use for-in constructions.

To explain how such a loop works, let's consider an example:

# a bag of potatoes
bag = ["potato 1", "potato 2", "potato 3", "potato 4", "potato 5"]
 
# we make a loop
for potato in bag:
    print(potato, "peeled!")

Creating for-in loops

The for - in loop is created as follows:

# For each potato in the bag, do the following
for potato in bag:

Pay attention to the colon at the end of the code line. It is mandatory!

Print instruction

What is to happen as the next step in the loop (peel the potato) is written in the code block (where there is the print instruction). This block of code is the so-called loop body, all the instructions are to be repeated as long as the potatoes are in the bag. Note that the print line is indented in relation to the for instructions. This indentation is mandatory. It can be 1 tab (it will work, but is reluctantly accepted by programmers), or several spaces (good solution - preferably 4 spaces). All the instructions inside the loop should be indented!

The loop will take another element from the list and this element will be assigned to the potato variable. Then the body of the loop (i.e. the code in the indentation) will be executed. After the indented code is finished, another list element will be taken and assigned to the potato variable. This will be done as long as there are some other elements in the bag list.

This solution is universal! If the bag list contains 100 potatoes, it will be done 100 times. If only one, it will be done only once. If the list is empty - it won't be done even once, because there won't be any potatoes in the bag! :)

Task 1

We are not going to peel potatoes in the tasks. We will work with cooler things, namely The Beatles band. Take a look at the code. You will find there a list of songs from the album "Please Please Me". In a for - in loop, iterate through the song list and display each song separately.

Do the task See the solution

Task 2

Look at the code. You will find there a list named please_please_me_time, which contains the duration of each song from the album "Please Please Me" in seconds. In a for - in loop, iterate through this list and sum up all the times. Display the result on the screen.

How to do this?

First declare a variable, name it somehow, e.g. result, and assign the initial value of 0 to it. Then, in the body of the loop, add the value of the next loop element to the variable result.

Do the task See the solution

Task 3

It's time for something more difficult: repeat the code from the first task, but instead of displaying subsequent strings from the please_please_me list, do the following:

Display the title of the song, the text "\t" (the character \t means tab) and the duration of the song on a single line.

Do the task See the solution

Next chapter: Conditions »