Learn PYTHON # 5: Loop ( Part 2)

kouba01(66)
Published in
OCD
Words
1051
Reading
5 min
Listen
Play
6y

Hi to all, this is Bilel for this second part of the fifth episode on python language learning, in the first part we have understood the stakes of the Loops For end For each .

Loop : Part 1

2af39a94c0d82a8de5ae794fa17434ca.jpg

Well. Now let's move on to the third and last loop type of this post. This is the famous loop 'while' which means "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.

2.JPG

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.

3.JPG

If now, I launch the script, it will spammer me in the console "your current salary is 1500.";

4.JPG

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.

5.JPG

So if I run the script look what it's going to do.

6.JPG

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 to1980. 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.

I am going to change the condition by putting here for example 10,000;

7.JPG

and if now I launch the script, look, it works just as much.

8.JPG

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 youtuber. 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.

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 .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.

9.JPG

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.

10.JPG

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)).

Anyway I hope that this video will have pleased you. 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 2) | Ecency