Play with loops

Now is time to learn what a “loop” is. If you had to imagine a loop, you would probably even make the gesture of a “circle” with your finger. That’s exactly what happens in Python. You would choose to perform a “loop” when you want to repeat a block of code a specific amount of times (over, and over again).

There are two types of loops:

FOR loop

A for loop is useful for iterating over (or going through) each of the items of a sequence, such as lists, dictionaries, strings, sets and tuples. In order words, we want to perform the same thing again, and again, in succession.

Why would we like to do that? Imagine you ask someone “From 0 to 10, how much do you love me?” And this person replies, “Which are my options?” Therefore, you choose to show the options as follows:

This person, who is a #CodeForAChallenge veteran, replies that you should be more efficient, and recommends you to perform a “for loop” as follows:

Can you see? It’s like reading, try to say out loud “for each option I have in this container of 0 to 10 levels of love, show my friend each option up”. 

In the first line, we selected a “list” as a container, and we called it “love_levels”. In the second line, each element of the list I chose to call it “option”. “For ___ in ____” corresponds to the loop structure. 

It is important you understand that even if you change anything selected in bold, the code will continue showing the same result. This means you can choose the name better fits for you, so it’s easier to read (preferably not very long).

Importantly, we would like to add that you can surprise your “special friend” with something called “range function”. This is exactly what you have done so far, but it avoids typing each of the values of the list love_levels. Why is this relevant? Because imagine your love levels range from 0 to 1000, would you type each one in a list? Let’s have a look!

Note

The range function retrieves the values from 0, to 10, but requires the values range(0,11). This happens because the 0 is inclusive, but the 11 is exclusive.

We hope your friend chose, at least, a 10/10 of love for you!

WHILE loop

Now that you’ve met the “for loop”, it’s the right moment to introduce you to the “while” loop. Whereas the “for” performed something a number of times, a “while” does the same, but under a given Boolean condition (True, False).

Following the “example of love” of the for loop section, let’s explore the love levels, again:

Let’s translate it:

#First iteration 

  • Line 1: we declare the variable love_level, and we select the starting value “0”.
  • Line 2: while the value inside love_level, is smaller than 11, do something. In our first case, the starting value is 0, so 0 < 11. This is True, so it goes to Line 3.
  • Line 3: It prints the value inside love_level, which is 0. It goes to Line 4.
  • Line 4: declare a new variable called love_level too, and whatever was inside in the previous variable called love_level, add +1. This is possible because we are “rewriting” the variable, as we are using the same name. In other words, love_level was 0, now is 0+1 = 1. 

#Second iteration

  • Line1: Now that love_level = 1, the code is automatically performing the declaration of the variable in line 1. So it will start in the “while” part. 
  • Line2: Given that love_level is still smaller than 11, it will continue to line 3, and then to 4.

#In the 10th iteration

  • The variable love_level will be equal to 10, so when it reaches line 4 (and it becomes 11) the loop will finish. There won’t be a 11th iteration, as the condition of line 2 will be False.

Did you see what we’ve done here? We strongly recommend you to perform the loop line by line in a paper, or in your head. It’s important that you understand what you are expecting, so programming doesn’t become a black box without sense. Don’t give up!