APPRENDRE LE PYTHON #5 ? LES BOUCLES

480.08k views2807 WordsCopy TextShare
Graven - Développement
Aujourd'hui , on se retrouve pour le 5ème épisode sur l’apprentissage du langage python avec la noti...
Video Transcript:
Hey everyone this is Graven 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. Today, I propose to attack a brand 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 and so on .
. . Today 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. First of all I will delete the code of the last video to start on new bases. At first I propose here to display the following message: "You are the customer # 1.
" So until then no difficulty; if I run the script, we have in the console the message in question. 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. 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 a) 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. 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.
And there you will tell me "ok Graven, 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. 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. 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, which means in French "for each" 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 gravenilvec@gmail. com for the first message. Then a second message "e-mail sent to graven@graven.
yt "and finally a last message" e-mail sent to contact@graven. yt. 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 to generate it differently, it would be here to list e-mails; for that we will do as in the previous video, we will create a variable emails that will store the different emails (gravenilvec@gmail. com first, then graven@graven. yt and finally contact@graven.
yt. 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. So I will delete the previous code, and if now I run the script, I see that I have the expected result. 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 graven@graven. yt "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 gravendev@yahoo. fr and for example steelaxiss@free. fr.
I restart the script, and once again I have here the expected values. 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 gravendev@yahoo.
fr, and why not steelaxiss@free. fr. 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. 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.
So if now I launch the script look "Email gravendev@yahoo. fr not allowed ! Sending impossible .
. . "So it works perfectly.
You also have at your disposal a second tool called the break that will break the loop. Is to say that she will not even continue to run. So if I run the script; look, the first time my loop came back in that condition, she went straight destroyed.
So you can see that the rest did not happen. Well. Now let's move on to the third and last loop type of this video.
This is the famous loop 'while' which means in French "as long as a condition is true. "To give you a very simple example, we will start from the following case: we imagine an employee who perceives 1500 euros each month. So to represent that, I'm going to create a variable that will be called salary, which will store its salary, which is 1500 euros.
Until then no difficulty, that's what we saw earlier. Now what you need to know is that as long as this employee receives a salary of less than 2000 euros, then he will receive an increase of 120 euros on his salary (which will be cumulative of course). So how are we going to represent that?
Well, the loop will be repeated as long as the salary is less than 2000. So that's where the condition principle comes into play. So we will start by putting the keyword "while", and here we will put the condition that is that the salary must be less than 2000.
So what I propose to you at first is to make a print by putting "your current salary is of" and we will put the salary here. If now, I launch the script, it will spammer me in the console "your current salary is 1500. "; since it repeats itself as long as the condition is true (since we did not touch the variable salary it will always remain 1500) so the condition will always be true since 1500 is always less than 2000.
What we should do is simply add 120 euros to salaries and then I post the result. So to add the 120 euros I will get the variable salary do + 120, and reassign everything to the variable, otherwise I would just have the result of the calculation but I really want to reallocate it. Know that it is possible to write it differently by simply doing + =, it is our way of writing it that we also saw previously.
So if I run the script look what it's going to do. At first I had 1500 euros, so it's back in my loop, since it's less than 2000. We added 120 euros, so we went to 1620.
Then, the message is displayed in the console. So we have "your current salary and 1620 euros, so it works perfectly. And then we will re-enter the loop.
1620 is always less than 2000, so we will be able to add 120 euros to the salary here. So it will be 1740, then we display "your current salary is 1740 euros". So this is the second message.
we re-enter the loop 1740 is still less than 2000, so we will add the 120 euros and we will go here to 1860. Then you display "your current salary is 1860", it is this third line. Then we re-enter the loop 1860 is always less than 2000 so we add again 120 euros, so we go to 1980.
One displays "your current salary is 1980". Once again 1980 is less than 2000 so we will add 120 euros to the salary. So we go to 2100 euros.
We post "your current salary is 2100 euros", so this is the last message. we re-enter the loop again, and this time the salary is no longer less than 2000 since we are at 2100. Therefore the condition here is no longer true, the loop will stop and it will execute following the code.
So we can put here for example (print) "End of the program. " So I'm going to put it back to 1500, and if now I launch the script, we have here the instructions which are done little by little, and at the end "End of the program. " I am going to change the condition by putting here for example 10,000; and if now I launch the script, look, it works just as much.
Perfect. Well, I propose now to take a last example always with this loop "while" to fully immerse yourself in this notion. We will delete the code in question, and this time we will take the example of a youtubeur.
His name is "Gravinou" and he has a community of 2500 subscribers. Until then no difficulty, I just need to create a variable subscribers_count for example, which is equal to 2500. We imagine that gravinou starts to rise on youtube, and he gains 10% of audience extra (so subscribers of course) per month.
And we want to estimate how much will there be subscribers after two years (of course 24 months. ). So first we will have to integrate this story of months that are moving forward, so we will start by creating a variable months, which by default is equal to zero months.
We're going back here in the loop "while" by putting the condition (which is that the number of months is strictly inferior or equal to 24), and here we will start by increasing the audience. So for that I will recover the number of subscribers, and I will multiply it by 1. 10.
So that's the coefficient that will increase with a certain percentage a value, that is to say, if for example I want to increase the number of subscribers by 30% we will make a + (30/100), which will give me 1. 3. If I want to increase the 2,500 subscribers by 30%, I'll just multiply by 1.
3. It works of course in the other direction, if I want to reduce the community by 20% I just have to do 1- (20/100), what will give me 0. 8.
So if I multiply 2500 by 0. 8 it's like we lost 20% audience for this youtuber. So I'm going here reassign to the number of subscribers (of course we can once again simplified this mechanism).
Then we will display the number subscribers. So I'm going to print here by putting "You currently have" and then we'll inject the value that corresponds to the number of subscribers, "subscribers", and to inject this value we will make a coup de . format (subscribers_count) which will replace this value by the current number of subscribers.
And very important we do not forget to spend the month following. So for that I will get the number of months and come add 1. Otherwise the condition will always be true therefore we will arrive at values endless.
It's gone I run the script. We had 2,500 subscribers at the start, the person increases by 10% of audience 2750 after one month, then 3025, 3327 et cetera and so on. And after two years she will have 27086 subscribers.
So it works perfectly. So from now on you have a global overview of the different types of loops to your provision in python (of course each has its usefulness and you have to know how to judge dose in your case (do not worry it is done by doing projects, doing practical work and so on)) and precisely as we are at the end of the video I propose as usual to do a little TP, and today it will be a little game the right price. So for those who do not know this game, it's very simple: we will choose a number between 1 and 1000 for example, and the player will have to announce a value between this interval, for example it says 100, and we will tell it is more, it's less, or it's found!
So there, as long as the game is not finished, we will ask the user to enter a price. If he finds the right price then we put it: "it's won! ", otherwise we print "it's less" or "it's more".
You will of course have the answer of this TP in description, as well as a small quiz to train you on this new concept. So here is a little bit what I wanted to show you today, in Anyway I hope that this video will have pleased you. If that's the case do not hesitate to put a like, to subscribe to the channel if it is not already done, and to share this video.
In any case it was Graven, hello to all and see you next time!
Copyright © 2025. Made with ♥ in London by YTScribe.com