hello guys welcome ins again in this blog I'm going to show you how to use loops in Python and I'm going to show you how you can use a while loop and for loop in Python so these are two kinds of loop in Python you can use to iterate some values so first of all let's see what is a loop a loop is a piece of code which help you to iterate or to you know repeat a piece of code again and again without rewriting at the same piece of code again and again so loop as the name suggests is a piece of code which will help you to you know write or implement some piece of code again and again unless and until some condition is true so let's see how a while loop work in Python first so for example I have a variable called a which is equal to zero and so let's see how while loop declaration works so for declaring a while loop you just need to write while and then the condition comes which needs to be true if you want to go out of the loop so for example I want that if a is less than five then I want to implement this okay so once again I forgot the colon yeah my look has this colon okay so I want that if is less than five I want to know the value of a okay so I want to print a okay and because this condition is on you know never be true if I don't increment the value of a so I am going to increment the value of a like this so a plus equal to one this is same as writing for example a is equal to a plus one this same as this one but you can write this in short like this right and I will just execute this while loop and the result is 0 1 2 3 4 5 ok so you have you haven't printed this 0 to 4 integers you know you just have written this one line of code to print all these numbers so while loop or loop help you to you know implement a piece of food or again and again right so what happens here you have we have already declared a is equal to 0 and then your code goes here and it sees it's a while loop and it checks this condition a is less than 5 because a is 0 and 0 is less than 5 so it will print this 0 value here once again it goes to a while loop once again and because I was increasing it by 1 now value of a becomes 1 so a is 1 which is less than 5 so it will print 1 here with this code and it will increment a once again and now a is 2 and 2 is less than 5 so once again it will print 2 and same is for 3 but as soon as 5 comes here and 5 is not less than 5 then it will not print 5 because this condition is false now so it will go out of the loop so this in this way this while loop or box now let's see some real life example or some practical example of while loop so for example I have this example where I have a is equal to 0 and s is - or a is equal to one and as his s is equal to zero and I am telling the user to enter some value okay so enter number to add to the sum okay and enter 0 to n quit okay so I'm telling user to enter some values so that I can make the sum or otherwise if you want to quit just enter zero and then I am checking whether a is equal to zero if a is not equal to zero then I am going to take this value of s and then I'm asking the user to enter a number and then
I will add this number to my sum S stand for some and E stands for a number and then I will print the sum like this ok this is a little example which is more practical in real-life situations and I will run this and now it will run so I'm asking enter the number to add to the sum if you enter zero you will be quit or you will quit the if you will quit the program and current sum is zero so for example I enter 300 ok and now current sum becomes 300 okay and for example I enter - 2 or 3 40 then current sum becomes 40 okay so it will go up on keeping the sum or making the sum plus 80 so it will add to this 80 once again and then for exam 120 it will add 120 to the sum so this code will help us to make a sum of numbers which user will enter but as soon as I enter zero you see here then the while loop is no more valid because this condition becomes false and then a user will exit so when I press zero the sum is 160 which is true and while loop is ended and condition is false and I am out of the loop okay so in this way you can use while loops now there is one more kind of loop in Python which are called for loop and for example I have a list called B is equal to in the list I have 2 comma 4 5 some numbers okay I'm just taking some random numbers here and for example I want to print all these numbers in the list but one by one okay so what I can do I can use a for loop and for loop how it can be clear with just this 4 and then I will declare a variable which will hold the value of these elements one by one okay so for example and um this is defined by me okay and what this does is this will hold the value of elements in the list one by one and then write in this in operator I have shown you how it works in the last blog if you don't know and then just write B which is our list which contains these values okay and then what I am going to do once again I forgot this : I somehow I keep on forgetting this column and then I will print number because this num holds the value holds the list value one by one okay and I will just execute it and you see my print function prints the values in the list one by one by one right so in this way you can use for loop and while loop and both of the loop you can use interchangeably so it's you know the both loop works in the same way but the while loop the difference is in between while loop and for loop is by when you are using while loop it you know it waits for the condition to be true and it will keep on executing your code while your condition is true right and if condition is false it will go out of the loop and for loop there is no you know condition it will just list out the you know values you give here okay so I hope you have understood the concept of for loops in Python and I hope you enjoyed this blog
Post a Comment