Python Tutorial for Beginners While Loop in Python

Python Tutorial for Beginners While Loop in Python

Welcome back aliens, my name is Avind Reddy and let’s continue with the series on Python. And in this video, we’ll talk about loops. Now, what exactly loop means? Now, in programming, what you do is you sometimes you need to repeat the statements.

Example, let’s say you have written five statements and maybe you want to repeat them. How will you repeat them? Of course, there’s one way. You can copy paste the same code multiple times.

Example, let’s say I want to print, let me print telescope. So, I want to print telescope once. And if I run this code, of course, it will print only once.

What if I want to print telescope two times? So, what we can do is we can just copy paste to it, right? So, we are good with this. So, I will copy paste it and we got two telescopes. And if I run this code, you can see we got two telescope.

That’s working. What if I want to print telescope ten times? What you will do now? Of course, you can copy paste it ten times. But don’t you think you are doing manual work? What if I want to print thousand times? So, it will be difficult to do that, right? Now, the best way to do it is applying a loop.

What you can do here is you can say, hey, Python, I want to, I want to repeat the same statement multiple times. To do that, we can use loops. And we can use two types of loops.

One is called as while loop and second is called as for loop. Let’s focus on while here. So, we can use a while.

So, while is a keyword. And then you have to specify the condition. See, while will say, OK, my job is to repeat this statement multiple times.

But then tell me for how much time you want to do that. I will say, OK, do it for five times. But how would I do that? So, when you say you want to do it for five times, we count, right? We need a counter here.

Example, if I say, hey, you say hello five times. I’m talking to you here. OK, so say hello five times.

What you will do is in your mind, you will be saying hello once. OK, hello. Second time.

Hello. Hello. And hello.

So, you have a counter there, right? In the same way, we need a counter here. For that, I will be using a variable called as I and I will say I start with one and then every time. OK, first of all, you have to check, right? How will I check if the value of I is less than or equal to five? OK, we have to apply a condition, right? So, step one would be take a counter, a variable, which is I in this case, and check if the value of I is less than five.

And every time you print the score, just increment the value of I, right? So, because you’re doing it right. So, you’re saying hello. Hello.

So, you’re incrementing the value. So, you will say I is equal to I plus one. That’s it.

It’s so simple. And remember the syntax. So, we have to use a while, then you have to specify the condition and a colon.

Now, this colon specifies whatever is coming ahead is a part of this block. And this two statement belongs to the same suit is because we are using a proper indentation here. And now, let me just run this code.

And you can see we got Telisco five times. So, what is happening behind the scene? And how do I know that? It’s very simple. So, set the breakpoint.

And now let’s debug it. I love this thing, debugging. And then say F8.

And you can see the value of I is one. Okay, that’s done. And then if I again say F8, it is printing Telisco once.

And you can see that in the console. Okay, this is that we got hello once. And if I go back to debug it again, the value of I now, so the moment I say F8, the value of I becomes two.

You can see the value of I is two now. And it will check if two is less than five. Yes, it will go ahead and it will print Telisco.

You can see if I go back to console, Telisco will print second time. And then if I say F8 again, you can see we got value of I is three. Is three less than five? Yes, it will go ahead, it will print.

It will then try to print the statement which is Telisco. And you can see in the console, we got Telisco three times. And now the value of I is four, right? So, you can see the value of I is four.

And then if I say F8, it will print Telisco fourth time. You can see that in console. If I switch back to there, you can see we got Telisco four times.

And then the value of I now is five, right? Is five less than equal to five? Yes, you can see we have equal to there. The evaluation is also true. You can see that the pop-up.

If I say F8, Telisco, it will print Telisco fifth time. You can see we got fifth time. And if I go to debug it again, say F8, and you can see the value of I is six.

Now, the value of I is six, right? Is six less than equal to five? No, that’s a false, right? You can see the pop-up which is false. And if I say F8 now, it will come out. And that’s why you’ve got five times Telisco, right? So, this is how you can use while loop.

Now, three things are important here. First is the initialization. Second is the condition.

And third one is increment. Oh, can we do decrement here? Yes, we can do decrement as well. So, you can do reverse, right? Sometimes we’re going this way.

Hello, hello, hello, hello, hello. Or we can say, so let’s say we will do reverse. Hello, hello, hello, hello, right? So, we can do that as well.

So, we’ll start with five and then we’ll reverse the condition. We’ll say I is greater than equal to one. And instead of saying I is equal to I plus one, we’ll say I is equal to I minus one.

We are going reverse order. Let’s run this code. It works.

You can see we’ve got five times hello. So, we can go ascending or we can do descending, your choice. So, there are three things important here.

One, assign the value, then condition, and then increment or decrement. Now, let’s say I don’t want to print Telisco here. I want to print the value itself.

Or maybe I want to print Telisco with the value. We can do that. So, we can say space comma I. I want to print the value of I as well.

Let’s run this code and you can see it is printing Telisco five, Telisco four, Telisco three, Telisco two and Telisco one. So, you can do that. Okay, this is how you work with while loop.

Now, can I use multiple while? I mean, while inside of while, you can do that. So, we have done that before as well, right? Example, I will print Telisco once. And then while I’m printing Telisco once, I want to print Telisco rocks multiple times.

I want to print Telisco rocks. I want to print rocks five times. So, what I’ll do, I will say while.

Oh, now I want to say five times, I have to use a different variable, right? And I will be using a variable here, which is j is equal to one again. We’ll say j less than equal to four. Let’s say I want to print rocks four times.

We’ll give a colon and we’ll say print rocks. That’s good. So, it will print Telisco and then rocks four times.

I don’t want to print the value of i now. So, it will print Telisco and rocks four times. So, before executing this, you try to guess the output, okay? It’s you, it’s depend upon you now.

You tell me what’s the output. Okay, when I print Telisco rocks, Telisco itself will get, will be getting printed five times, right? And then on every line, you will get rocks four times. This is what you want, right? But if I run this code, oh, that’s weird.

It’s not stopping anywhere. You know why it’s not stopping? Because we forgot to increment the value of j. And now you learn from mistakes. Let’s run this code once again.

And you can see we got Telisco rocks, rocks, rocks four times. And then again, Telisco four times. That’s, that’s, that is not something which we want.

What is going wrong here? Let’s try to debug this, you know. So, I will just say, hey, debug the application. Now, you can see the value of i is five.

In fact, you know, just to make it more simple, let’s go with one. Let’s go with increment so that it will be easier for understanding. Because once you understand the basic stuff, you can try it on your own.

And remember this, we are using while inside of while, okay? So, this is nested while loop. Let me just debug this by applying a breakpoint. Let me say f8.

And you can see the value of i is one. Remember these values. The value of j is one.

Is one less than five? Yes. Let’s go inside. It will print Telisco, of course.

And you can see in the output console, we will be getting Telisco now. We got Telisco. Now, what’s the value of j? It’s one.

One less than four? Yes. It will go ahead. It will print logs.

It will execute this j equal to j plus one. So, now the value for j is two. If I say f8, the value of j is two.

But look at the jump. It is not jumping outside. It’s not jumping back to this loop.

It is jumping back to the inner loop. Because first, it will complete the inner loop. Then it will go back to the outer loop, right? It is something like, you know, we have seven days a week and each day has 24 hours, right? So, you can imagine the number of days is the outer loop and the number of hours is the inner loop, right? So, what it means? So, let’s say, if you start with Sunday, Sunday has 24 hours.

It will start with zeroth hour, then one hour, second hour, third hour, fourth hour. Now, when you complete the entire 24 hours, it will go for the new day, which is Monday in this case. On Monday as well, we have the same hours, right? Zeroth hour, one hour.

So, that means every time you change your day, it will restart the number of counting for your hours. So, it will first complete the hours, right? So, same thing is happening here. So, if I say f8 again, it prints rocks.

It will increment the value of j to 3. And that’s why you can see, if you see the output, we got one telescope but two time rocks, okay? So, that works. In fact, we wanted this, right? We wanted one telescope and four rocks. Okay, we’ll do that.

We’ll think about that. If I run this, you can see we got rocks. And once it is getting completed, once the value of j becomes 5, you can see that the value of j now is 5. If I run this again, it is false.

And now, it is focusing on i. So, i value is 2 now. It went back there. And then again, if I say f8, it will print telescope, of course.

But look at the value of it for j. j is 5. That’s where things are going wrong. Now, since j is 5, it is not continuing ahead. So, that was a mistake.

So, to solve this problem, what we can do is, first, we have to do two things. I want to print rocks on the same line, right? I don’t want to print rocks on new line. So, what we can do is, after telescope, I can give a comma and say, end is equal to double quotes.

Now, what it means? By default, when you say print, it will print the statement and comes on new line. I don’t want new line. I want to print on same line.

Example here, if you see, it is printing telescope and then it is coming on new line. I don’t want that. I want it to be, I want it to print telescope and stay on the same line.

Same thing can be done for rocks. I would say, hey, don’t go on new line, stay on the same line. Now, I hope it will come on same line.

If I say run, okay, so that we got everything on same line. Okay, we don’t want that. We don’t want all telescope on same line.

We want telescope on new line. So, after incrementing, I will say print. Because see, after completing one telescope, I want to print on new line.

So, I will say run. You can see we got telescope on new lines, but then rocks is coming only for four times here. What is going wrong? The value of j reached five here, right? So, what I will do is, every time I start with the outer loop, I want to restart the value for j, which is one.

It’s important. Every time, in fact, we should not be declaring the value here. Let’s start here.

Okay, so it will initialize here, check for the condition here and then increment, decrement here itself. Let’s run this. This we want, right? So, we got telescope rocks, rocks, rocks.

Now, how it is working? Let’s again go for debug. We’ll say debug and say f8. You can see the value of i is one, i less than five.

Yes, it’s one. It will print telescope and it will not go on new line. Okay, it will stay on the same line.

Let me jump to console and you can see, if I say f8, telescope on same line. Okay, there’s not jumping to next line. The value for j is one.

Is one less than four? Yes, it will go ahead and it will print rocks. And you can see rocks on the same line. And then the value of j is getting incremented to two now.

So, you can see the value for j is two here. And then, again, it will come back. It will print rocks and it will go for j value, which is three.

And then it will again, three is less than four. It will print rocks and then j value is four. And then it will check.

Is four equal to four? Yes, it will print rocks. And the value for j is now five. Now, five is not less than equal to four.

It will come out. But it will come out to this next statement, which is i is equal to i plus one. Now, in this case, i equal to i plus one, it will increment the value for i. So, it will make it two.

And then, since we are printing, since we are doing print, it will come on new line. You can see the cursor now. Cursor is on new line.

I will say, I mean, it will continue. Okay, so it will do the same thing which we have done till now. So, if I go fast, fast, fast, see that.

See the sequence. So, this is how your loops are working. Now, see, since I’m doing this, I have worked on this for a long time.

So, I know how it works. Now, if you’re a fresher, you might find this thing difficult. But trust me, the only thing is this is not difficult.

This is just unfamiliar. You just need to practice it. The more you practice this concept, it will make your concept very clear.

Okay, so we’ll be doing that. We’ll be doing some more examples with this, you know, printing the patterns like star, star, star, real star. So, we’ll do that in the further sessions.

I hope you got some idea about while loop. In the next video, we’ll talk about for loops. So, if you’re enjoying this series, let me know in the comment section and do click the like button if you’re enjoying it.

Thank you so much for watching everyone.

Love To Share Knowledge.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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