1.10 Loops in Python | Python Full course 2024 #learnpythonin2024 #mlwithakansha

Akansha Yadav covers for and while loops, showing how for loops iterate over sequences like lists and strings, while while loops run based on conditions. She explains break to exit early, continue to skip iterations, and else clauses that run after loops complete.
Chapters
Ladies and gentlemen, so in this video we will see what loops are in programming. So one by one, we are going to cover each and everything related to the loops in programming. So basically, what loops are? Loops are basically the fundamental structures in programming that helps us to iterate something or to repeat something. There are two types of loops in Python. One is the for loop, another one is the while loop. Both are used in the different situations. So one by one, we are going to see in which situation which loop you have to use. So the very first loop that we have in our list is the for loop. For loop is used to iterate over a sequence. A sequence here is a list, can be a tuple, it can be a string, or it can be a range. Also, you can execute a block of a code for each element in the sequence. For example, As I said, for loop. So I will show you how to write it down, what is the syntax to do it. So you will write for, which is a keyword, then you will write any variable, whatever you wanted to write. Suppose in my case I'm writing a in sequence, and then you will follow up with the code to be executed. For f should be in the lowercase. Yeah, so for our variable in sequence. So now we will write one code quickly. So let's suppose I have a list called fruits, and in this list I have few items like apple, orange, and I do have grapes. Next I'm going to say for f— f for a in fruits, print a. Why I am expecting it to print a is because a is my variable in which my loop is storing items one by one. It means the entire list is not getting stored in a, it is storing the items one by one. So let's see what is going to be the output. Now I printed it and I got each and everything line by line. This is how our for loop iterates over a sequence. If I would have printed directly the fruits variable, I, I would have get like in the output I'm getting the entire list with all the items. But in case you want the items line by line, then you have to use the variable. With it, with the for loop. So this for loop can be used in the multiple ways by using different sequences like tuples, strings, range, or any other scenario that you wanted to use in. Another type of a loop that we have is the while loop. So the while loop is one of the loop, it works as per the condition. In for loop, we were not having any condition, we just wanted to iterate over a sequence. Whatever sequence we are providing, I wanted to iterate over that, or if I am providing it a range, then it will work according to the range. But in case of a while loop, we do have a condition. So while loop continues to execute as long as the specified condition is true. Since starting, I told you that there is basic question that why do we have two types of loop? Why do we have for and while loop? Why can't we have one? So the answer for this question is that we do have two types of loop. Both are used in the different scenarios. So suppose in the situation where you have the condition and you want your loop to iterate something up to condition only, then in that case you will use while loop. Let's take an example of another situation in which you don't know about the condition, you are just sure about the number of iteration required. I wanted to iterate 10 times, I wanted to iterate 20 times, then in that case you can use for loop. But in case you have a condition, you will go with the while loop. So Let's— a simple example would be a game in which I have a condition. If the, if the player passed that level, then this— you have to move up to this. So there is a condition. So that is how your while loop will be used. So now I will show you how to use while. So in the case of for loop, we were using variable within the while loop— for loop, it means within the statement of the for loop. But in this case, what you have to do is you have to Initialize a variable before starting a loop. You have to initialize a variable before the loop. After that, you have to write the keyword called while, following up with the condition that you have. Then you will write what needs to be done. Code to be executed. So that is going to be this systematic way to work with the while loop. So let's write down a short program on the while loop. Suppose I wanted to print a number from 0 to 5. So what is going to be my code? I have the condition I wanted to go up to 5 only. You can take it as a range also, but right now we are— I'm telling you about the while loop. So there is a condition that I want my number to be printed up to 5. Up to 5, it means 5 should also be there. So I'm going to take a variable count is equal to 0. This is my initial variable. I don't have anything right now to store in it, so I am storing 0. Which is going to be still considered as a variable. So now I'm going to say while my condition is count is less than 6, because I want numbers to be printed up to 5. So whenever you are writing the condition in programming, you will write one greater than what you want. If you want 6, you will write 7. If you want 10, you will write 11. So now what I want is, I want if the count is 6, then what to do? I want it to print count. But what— how that count variable will work for us? How the numbers will go up to 5? So for that, what I will do is I will use incrementer over here. I will say count is equal to count plus 1. So what I am doing over here is I am telling my while loop that whenever you iterate, whenever you are checking for the condition, you have to go and you have to add up the 1 into the num— into the current status of the count variable. Once you have added 1, go back again and check for the status of variable count and then print the numbers. So this is how it will work. Currently it is 0, which means it is less than 6. So yes, it will print the number. Then it will increase 1, i, 1, like it will add up 1 in the count variable. So then my count variable is having 1 in the output. Then it will repeat the same thing. Then count is 1, which is also, yes, less than 6. It will print that number. Then it will add up 1. Then it will continue with the same. It will add up 1. It means we, we currently have 2 in the variable. Then we have 3, 4, 5. When it will go up to, uh, up till 6, it will check, okay, it's adding 1, and it will check, okay, 6, 7. 7 is less than 6? No. The condition goes false. Our loop will stop working. So another thing to notice in the loops concept is there are few statements that controls your loop. There are few statements that controls your loop. One is the break statement, another one is the continue statement, then we do have else statement. So let me show you how to use the break statement. So let us suppose I'm writing a for loop for in list, I am writing 1, 2, 3, 4, 5. Okay, let me change the cell to the code cell. Yeah, now we have for i in this numbers, and I'm expecting it that if i is equal to equal to— it means the if condition— if i is equal to equal to 3 then break. Else, else print the number i. So what I am doing over here is I am writing a for loop within which I have a variable i and in the list. Then if this condition is true, it will break it up. Otherwise it will print the number. So once I have run the program, I got 1 and 2 as an output, but 3, 4, 5 isn't— is not, because as per this condition, the loop has to break up whenever this condition goes true. Once this condition goes false, it will stop. Sorry, it— once it condition goes true, it will, it will stop. Once it goes false, it will print the numbers for us. Now I'm going to use the same program in the next cell and I will show you something. Let's suppose I'm going to remove this code and I'm going to add it after print. Will that make any change? Yes, it will. This time it is printing 3 for us because I After printing, it got this, uh, condition got to know, okay, this is false. After printing, it happened. But here, what we were doing, we were firstly checking and then printing. Here we are printing, then checking. So that is how break statement works. Another statement that we have is the continue statement. Now I will show you the same code and some magic with the continue statement. So when I have the code And now I'm— let's, let's take this one, and I'm going to replace it. Now I'm going to change the break statement with continue, and let's run it. So what continue statement says is it will skip that part, that current iteration part, and it will move to the next iteration. It means it is skipping that particular thing. And continuing with the rest of the thing. It's not breaking up, it's just saying that skip it. That is how continue statement works. Another statement that we have is the else clause. That is not a statement, that is else clause, because within the for loop you can write else as well. So just now we did one example which was related to the fruits over here. I'm going to take the same example and I will show you at the end that how you have to use the else clause. Else clause can be used with the both— it means with the for loop as well and with the while loop as well. So in this case, I'm writing else print no more items. So I have a fruits list, I am using the for loop, but after printing everything, if there is nothing in the list left, it will say print no more items. That is how you have to use the else. Same way, what you can do is you can use this same code of while loop and keep this else one and say that condition is false. So after printing all the numbers, we are getting output saying that condition is false. So that is how your else clause within the for loop will work. So this loop is very crucial for effectively writing or handling some repetitive task in programming, and which is like— so understanding loops is very crucial. It means you have to use the repetitive task in programming day by day. So that is the one of the major, uh, importance of loops. That is why you should learn loop. So choosing the appropriate loop type is also the powerful step to take up.










