python for loop

Hello, everyone, this is Peter woods from MIT and in today’s Article, I’ll be focusing on various loops that are available in Python.

So without any further due let us move forward and have a look at the agenda for today. So first we’ll see why to use loops and what is the reason of introducing loops in the programming world, let me see what exactly are loops and then we’ll focus on various types of loops that are there in python.

Namely WHILE, FOR, NESTED and I’ll be explaining you all these three loops with examples after that I’ll give you a brief summary of all the things that we have discussed in today’s session so I hope we all are clear with the agenda.

So in Short below is our Agenda:

  • Why to use loops
  • What are loops
  • Types of loops in python ( While, for, nested)
  • Summary.

So let us move forward and see why we need loops, let us understand this with an example that is there in below screenshot

Suppose you are a software developer and you are required to create a software module for payroll processing of all the employees in your office, now one of the things you need the salary of those employees, the bonus and the total amount that they’ll get at the end of the month. So all these things for each employee you need to print.

Now there are two ways of executing this task, so let us see one of those two ways.

so the one way is you can actually write a logic to calculate the total salary of each of the employees that includes all the fields like salary, bonus, total and you are going to write that logic for all the employees that are there in your office. There might be hundreds of employees that are there in your office so you need to write the logic or you need to write the code for all of those employees so this process is actually pretty hectic you need to repeat the same logic for total employees are there in the office. I am just giving you an example of three employees but what if you’re have like hundreds of employees so at that time this particular process will not work this will not only increase the size of the code it will make it more complex and less efficient.

Now another way of approaching this task would be you write this logic to calculate the Salary you keep on iterating that logic for the total number of employees that are there.

So when this loop runs once it’ll print the salary of your first employee when the loop runs for the second time it will print the salary of the second employee similarly it will keep on repeating so if you have a hundred employees it will repeat four hundred times.

Now what advantage we get here is we don’t need to write the logic for each employee in order to calculate the salary we can write one logic and keep on repeating it for all the employees so this way the size of the code gets reduced the length of the code is reduced at the same time it reduces the complexity makes it more efficient and even increases the speed of execution.

So this is just one example in order to show you why we need loops so there are hundreds of thousands of examples that you can think of why we need loops right so this is just one simple example that I’ve shown you in order to make you understand why we need loops.

 

What exactly our loops let me explain you this with the help of the flow chart that is there in below screen shot.

Loops basically allow us to execute a statement or a group of statement multiple times.

Let me explain you this with the help of an example that is shown in above screenshot now over here what happens the control comes condition and checks the condition so if this condition is true it will go on and execute the conditional code that is there this is nothing but the statements that are present inside the loop so it will execute that and again it will go back and check the condition so if the condition is still true then again it will execute the conditional code of the statements present inside the loop and it will keep on repeating until this condition becomes false and the moment this condition becomes false the control will move out the loop and it’ll execute the statements that are present after that loop.

now one thing to notice here guys is that there can be two kinds of loops one is finite this is actually the flowchart for the finite loops and another can be infinite now in infinite loops what happened the condition never be false so at that time the control will never come out of the loop so it will keep on repeating and it will never stop that is what infinite loops are so basically the condition will never be false and through that the loop will be executed infinite number of times.

Now there is one more way in order to categorize loops that is called post-test and pretest

In post-test loops what happened the control will first enter the loop and then, in the end, it will actually check the condition,but in pretest loops the control will enter the loop only when the condition is true, so the condition is checked at the beginning of the loop in pretest loops but in post-test loop the condition is checked at the end of the loop.

In python, there are no post-test loops present there are only pretest loops, so we’ll focus on pretest loops in today’s session so I hope you all are clear what exactly loops are.

we’ll see various type of loops that are present in Python so Python basically supports three kinds of loops one is while, for and nested.

we’ll look at all these loops one by one and I’ll explain you with examples as well so first we’ll have a look at while loop so while loop is basically used when you don’t know how many iterations are required so let me explain you this with the help of the flowchart that is there in below screenshot.

Now over here what happens the control will move inside the loop only when this While condition is true and when it is true it will execute the body of the loop and then again it will go back and see whether the condition is still true or not if it is still true then again it’ll execute the body of the loop and it will keep on doing that until the condition becomes false and the moment the condition becomes false it will come out of the loop and execute the statements that are present after the loop.

This is the syntax for while loop:

Now why we use while loop?

Basically, I told you earlier as while loop is used when you don’t know how many iterations are required so you don’t know how many times you need to execute the statements that are present inside your loop so that is the reason why we use while loops.

Let me show you that with the help of a PI charm and I’ll give you a very basic example of using a while loop and after that, I’m going to increase the complexity of the code.

So first we’ll see a very small introductory example of a loop in which I’m going to print the integer values between 0 to 9 that won’t include 9 so it will print 0 1 2 3 till 9 but it won’t include.

Have told you earlier as well I’m going to print the integer values between 0 to 9 but that won’t include 9 so for that what I need to do is I need to first define a variable let me name it as count and I’m going to initialize value to it that is 0 now after that I’m going to use a while loop and I’ll write while and then I’m going to give a condition which says that when count becomes greater than 9 it should come out of the loop alright.

so now what I’m going to do is I’m going to just type in print let it be number colon comma print count right now I’m going to increment the value of count, count equal to count plus 1. Now when I come out of the loop I want to friend say goodbye.

Count = 0

While count < 9:

Print(“Number:”,Count)

Count = Count+1

Print ( “Good Bye”)

So let us see whether it works or not I’m going to run it and as

The output is 0 1 2 3 4 5 6 7 & 8 and finally, when the control went out of the loop it has printed goodbye

This is just a very small example. We just saw was a very simple example so I’ll increase the complexity of it.

I’ll give you one more example so let us move forward with that so over here what I’m trying to do is I’m trying to code a little guessing game, all right so what happens in this guessing game so there will be a random number that should be generated between 0 to 20 and you need to guess that number so the number that you have guessed is less than that random number then it will print number is too small. If you have guessed the number that is greater than the random number then it will print that number is too large.

The moment you get the right answer you’ll exit the while loop and you have won the game basically so it’s just a pretty simple example but this is a perfect example in order to show you why we use while loop because if you notice here I don’t know how many iterations are required in order to guess the correct answer so at that time while loop is a perfect solution.

 

I will show you how to execute this so this is the code here

Let me explain you each and every line that is present here now what happens first I need to import a module that is called random in order to generate a random number now over here I have defined a variable N and I’ve given a value to it that is 20 because I won the random number that is to be generated should be between the range 0 to 20.

Now I have taken one more variable and I’ve named this to be guessed and over here it should be an integer type of written and this logic will generate the random number that I am talking about and this will be generated between the range 0 to 20.

I have defined one more variable guess and I have assigned a value to it or initialize the value to it 0.

Now comes the while loop now in while loop i have given a condition which says that the number that i have guessed if it is not equal to the random number then enter the loop now once you enter the loop you need to input the new number the number that you want to get again now if that guessed number or the number that you are guessing is greater than 0 then again there will be one more condition which says that the number that you have guessed is greater than the number that has been generated or the random number that has been generated then print number is too large.

If the number that you have guessed is actually smaller than the random number generated then the print number is too small

Now there is one more condition that is else which means that when the number that you have guessed is less than or equal to 0 then that means you are giving up so it will break it will come out of the loop and it will print sorry that you are giving up that’s when your game gets over and you have lost it.

Finally, if you have guessed the correct number then it will print congratulations you have made it.

so I hope you have understood the code that I was explaining here

Let us go ahead and execute this and see what happens all right so it asks me for a new number so it has to be between 0 to 20.

I’ll type 10 and let’s see what happens so it says number is too small that means it is between 10 to 20 how about 13 all right so I’ve guessed the correct number and it says congratulations you have made it now over here as you can see that it took only two iterations for me in order to get the correct number

But what if it would have taken more iterations I mean I’m pretty unclear how many iterations it will take for me so that is the reason why we use while loop because we don’t know how many iterations are required.

Leave a Reply

Your email address will not be published. Required fields are marked *