Learn PYTHON # 5: Loop ( Part 1)

kouba01(66)
Published in
OCD
Words
1421
Reading
7 min
Listen
Play
6y

Hi everyone this is Bilel for this fifth episode on language learning python. In the fourth part we saw how to create lists that allowed us to temporarily store several values within a single variable.

1.jpg

Today, I propose to attack a new concept in which we will treat loops.

In programming as in everyday life, one is regularly made to do tasks that will be repetitive, iterative in order to obtain a result or to achieve an objective.

To give you a very simple example, when you play a video game where you have a mechanism to the inside of which is called the game loop. It is simply a task that will be repeated as long as a condition is fulfilled; the condition of getting
a certain number of points or else not to die and so on ...

I suggest you to see the different types of loops that exist and put all this into practice. To try to understand, we will start with a first example.

At first I propose here to display the following message: "You are the customer # 1." So until then no difficulty;

2.JPG

if I run the script, we have in the console the message in question.

3.JPG

Now, if you ask to do the same message for customer number 2, 3, 4, and 5 for example;what will you be tempted to do? To copy this instruction to duplicate it as many times as necessary and simply replace what we are interested in, that is the customer number (customer number 3 number 4 and finally number 5.)

Same thing, once again if I run the script, I have all the messages that are displayed each turn.

4.JPG

Only it's not totally right. Indeed in programming when you want to repeat several times the same instructions in our program, we quickly tend to want to duplicate the code in question as many times as necessary (just as I do at this level) by changing only what interests us as appropriate.

So what we could do is use a loop that starts from the value 1 up to the value 5 and which, for each of the values, displays you are the customer number ... So, for that, we will use the first type of loop. This is the for loop, which means: for a starting value (in our case it's 1) up to a value arrival. (in our case it is 5). So let's go for the creation of the loop. We'll start by skipping a line, here we will put the little keyword "for" to make this famous for. You will give a name to each of the values that will to pass successively in this loop, which will be processed. So in our case they are client numbers (1 2 3 4 and 5); so we can call it "num_client small space. And this is where everything will be played, we will say from which list of elements we want to make the loop So for that we go use the keyword "in" which means "from", "range" to make an interval between values 1 to 5. Two points to start the loop. We jump a line and there we will put our famous print, by putting as a message "you are the customer no ..." And here we are going to come and inject the customer number.

5.JPG

So I will delete the previous code; and if now I run the script, you will see that it puts me well "you are the customer # 1." Then you are the customer number 2, number 3 and number 4.

6.JPG

And there you will tell me ok Bilel, it's all well and good, but how come there is not the fifth client? "in programming and in our case precisely, one defines here the starting value which is 1 and the value of arrival which is 5.

7.JPG

However the arrival value is excluded, so if you want to make the values between 1 and 5 we will have to take a value above, in our case 6.

So now I restart the program, and look we have the expected result.

8.JPG

So if you have understood the stakes of the loop, you will see that the following will seem very simple. What to simply remember, it is a loop, so it's a series of instructions (so in our case it was to display a message) that will be repeated as long as a condition is fulfilled.

So in our case the condition was that we have a value called "customer number" that is less than 6.

Well. In the same spirit, we also have a second type of loop, called the "for each" loop, value of a given list. Suppose you want to send a console message by marking "email sent to:" and the email in question, for example [email protected] for the first message. Then a second message "e-mail sent to [email protected] "and finally a last message" e-mail sent to [email protected].

9.JPG

You agree with me that once again this code is redundant since we are doing the same code base that is to display "e-mail sent to:" and to inject the e-mail in question. So technically what we could do to generate it differently, it would be here to list e-mails; for that we will do as in the previous post,

we will create a variable emails that will store the different emails ([email protected] first, then [email protected] and finally [email protected].

10.JPG

From this list, I could now do that for each of the values I post this famous "e-mail sent to ..." and here I insert the email in question. So here we go. We will say that for every e-mail from my e-mail list, I'm going to make a print by putting "e-mail sent to: and here I will inject the email in question.

11.JPG

Now I run the script, I see that I have the expected result.

12.JPG

What happened was that my loop she came across my entire list, so she started with the first item, she picked it up and posted "email sent to ..." by injecting it. Then it went to the second element "e-mail sent to [email protected] "and finally we finished with the last element, so we can go through a list in recovering this or that value. It's very useful.

To show you that it works well, I will add here two new e-mails, which I will inject at this level; so the first [email protected] and for example [email protected].

13.JPG

I restart the script, and once again I have here the expected values.

14.JPG

Perfect. Well, now that you know these different elements, I suggest you to see two very practical tools that will allow you to control your loop internally (ie to move to the next round, therefore to skip a loop turn, or to be able to stop a loop at a given moment). Before anything else I go here create a little utility, which is going to be just a blacklist.

So I'm going to do here a variable blacklist that will be a list of emails that will be prohibited. So I'll start by putting eg [email protected], and why not [email protected].

15.JPG

Now, when I browse my list of items, I'll check here if the email I'm processing is in this blacklist, in these cases I will display a print by putting "E-mail ..." (Then I will inject the e-mail in question) "forbidden! Sending impossible ..." and I will inject instead of accolades, e-mail in question.

16.JPG

So if I send it script have the message that is displayed when I enter this condition.

Only it still sends "e-mail sent to ...", so we should not enter this instruction, so we will use the keyword "continue" which will go on to the next loop turn.

17.JPG

So if now I launch the script look "Email [email protected] not allowed ! Sending impossible ... "

So it works perfectly.

18.JPG

You also have at your disposal a second tool called the break that will break the loop. Is to say that it will not even continue to run.

19.JPG

So if I run the script; look, the first time my loop came back in that condition, it went straight destroyed. So you can see that the rest did not happen.

20.JPG

So here is a little what I wanted to show you today, in any case I hope you will like this part, if this is the case don't hesitate to like and comment if you have any questions or suggestions,

Follow me in the next posts!!Thanks

Learn PYTHON # 5: Loop ( Part 1) | Ecency