#20 Python Tutorial for Beginners | While Loop in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my name is 720 and let's continue this series on Python and in this video we'll talk about loops now what exactly do pins now in programming what you do is you sometimes you need to repeat this statements example let's say you have written five statements and maybe you want to repeat them how many repeat them of course there is one way you can copy/paste the same code multiple times example let's say I want to print let you print that is code so I want to print that is ko wines and if I done this code of course it will print only once what if I want to print that is good two times so what we can do is we can just copy wedge to it right so we are good with they are good with this so I will copy paste it and record to that is first and if I done this code can see we got to the disco that's working what if I want to print that is put 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 you 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 state with multiple times to do that we can use loops and we can use to debauch loops one is Palace of wild lupine seconds for less for loop let's just focus on right here so we can use a wild so what is the keyboard and then you have to specify the condition see while we say okay my job is to repeat this video multiple times but then tell me for how much time you want to do that I would say okay do it for five times but how do you die how would I do that so we can say you want to do it for five times we count right with the counter here example if I say hey you say hello five times I'm not I'm talking to you here okay so say hello five times what you will do is in your mind you will be saying hello once okay hello second time hello hello and hello so you have a bound to that 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 okay first of all you have to check right how will I check if the value of I is less than equal to five like if you have to apply a phoenician right so step one would be take a count or a variable which is I in this case and check if the value of I is less than and every time you print a disco just increment the value of I right so because you're doing it really so you're saying hello hello so you're incrementing the value so you will say I is equal to I plus 1 that's it it's so simple and remove the syntax so we have to use a while the negative specify the condition and a colon now this kunis specify whatever is coming our head is a part of this block and this to 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 that is go five times so what is happening behind is seen and how do I know that it's very simple so the set the break point and now let's debug it I love this time debugging and then say f8 and you can see the value of I is 1 ok that's done and then if I again say f8 it is printing telescope once and you can see that in the console ok this is it that we got hello once and if I go back to debug again the value of I now so the moment I say f8 the value of I becomes 2 you can see the value of I is 2 now and it will check if 2 is less than 5 yes it will go ahead and it will print 10 you score you can see that if I go back to console it telescope will print second time and then if I say afraid again you can see the court value of I is 3 is 3 less than 5 we yes it will go ahead it will print it will then try to print the statement which is which is the disco and you can see in the console recorder is called 3 times and now the value of I is full right so far you can see the idea of is 4 and then if I say f8 it will print that is call forth time you can see that in console if I switch back to there you can see the quarter is 4 4 times and then the value of I now is 5 right is a 5 less than equal to 5 yes you can see we have equal to their the evaluation is also true you can see that the papa if I say afraid that it will print a disco 5th time you can see we got 5th time and if I go to debugger again say f8 and you can see the value of I is 6 now the value of I is 6 right is 6 less than equal to 5 no that's a false right you can see the UK see the pop-up it is false and if I say f8 now it will come out and that's why you got five times the disco right so these are 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 DiClemente as well so you can do the worse right sometimes we go in this way hello hello hello hello hello oh you can say so let's say we will do the worse hello hello hello hello hello right so we can do that as well so we started five and then we'll reverse the conditions will say I is greater than equal to one and you should say I is equal to I plus one will say I is equal to I minus one we are going to reverse order let's run this call 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 that's I don't want to print a disco here I want to print the value itself or maybe I want to print a disco quit 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 they just go file there is go forth a disco 3 that is per to itemscope on so you can do that ok this is how you work with while loop now can I use multiple like multiple wires coming one inside a while you can do that so we have done that before as well right example I will print the disco vines and then while I'm printing telescope once I want to print that is cos øx multiple times I want to print the disco dogs I want to print rocks 5 times so what I will do I will say while oh now I want to say 5 times I have to use a different variable right and I will be using a variable here which is J is equal to 1 again we'll say J less than equal to 4 let's say I want to I want to print works 4 times will give a colon and will say print lakhs that's that so it will train tedisco and then drops 4 times I don't want to print the value of aina so it will print telesco and works 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 with a trend that is Cox for time series that is going serve we'll 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 done this code oh that's weird it's not stopping anywhere you know why is not stopping because we forgot to increment the value of J you know you learn from mistakes let's run this code once again and you can see we got the HISP Oh rocks rocks it's for time and then again that is for 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 I can see 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 you know this we are using via inside a wide okay so this is nested while loop let me just debug this by playing a breakpoint let me say f8 and you can see the value of I is wonde remember this values the value of j is 1 is 1 less than 5 yes let's go inside it will print a disco of course and you can see in the output console we will be reading telesco now we got to disco now what's the value of J it's 1 1 dozen for yes it will go ahead it will print clocks it will execute this J equal to J plus 1 so now the value for J is 2 or if I say F 8 the value of J is 2 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 we 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 ours 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 0 to R then 1 our second or third or fourth R 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 as y 0 to R 1 so that means every time you change your day it will restart the number of counting for your num your ass so it will first complete the ass right so it's same thing is happening here so if I say afraid again it prints rocks it will it will increment the value of J 2 3 and that's why you can see if you see the output we got one telescope by two time Rob's okay so that works in fact we wanted this right we wanted one that is kind for walks okay we'll do that we'll think about that if aren't these you can see record talks and once it is getting completed once the value of J it becomes five you can see that the value of Jana is five if I done this again it is false and now he's focusing on I so I buy this too now it went back there and then again if I say a fateful printer disco of course but look at the value of it 4j j is five that's what things are going wrong now since J is five it is not continuing ahead oh 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 play two blocks on the same line right I don't want to print rocks on new line so what we can do is after the disco 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 this 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 telesco and then it is coming on new line i don't want that i want it to be i want it to print that is co and stay on the same line same thing can be done for rocks i would say hey don't go on new lines 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 there is come on same line we want that is corn new line so after incrementing I will say print because see after completing one that is who I want to print on ulong so I would say run you can see we got to this core new lines but then dogs is coming only four four times here what is going wrong the value of j reached five 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 don't 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 record the disco rocks rocks rocks now how it is working let's again go for debug we'll set debug and say afraid you can see the value of I is 1005 yes it's one it will print a disco and it will not go on newline okay it will stay on the same line let me jump to console and you can say if I say fate that is call on same line okay there is not jumping to next line the value for J is one it is one less than four yes it will go ahead and it will print rocks and you can see the rocks on the same line and then Jay's getting incremented to now so you can see the value for J is to here and then again it will come back it will print rocks and it will go for J value which is 3 and then it will again 3 is less than 4 it will print blocks and then D value is 4 and then it will check its fall equal to 4 yes it will print rocks and divide you for J is now 5 now since 5 is not less than equal to 4 it will come out but where it will come out to this next item which is I is equal to I plus 1 now in this case I equal to I plus 1 it will increment the value for I so it will make it 2 and then since we are printing if we since we are doing print it will come on new line you can see the cursor now resides on new line I will say I mean to continue ok 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 these are the loops are working now see it since I am doing this I have worked on this for a long time so I know how it works now if you know 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 ok so we'll be doing that we'll be doing some other examples with this you know printing the patterns like star star star over here stuff so we'll do that in the further sessions I hope you've got some idea about while loop or in the next video we'll talk about for loops so if you are enjoying this series let me know in the comments section and it looked like button if you're enjoying it thanks for watching everyone you
Info
Channel: Telusko
Views: 868,031
Rating: 4.9189205 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, loop, while, block, type, condition, syntax, why, need, initialisation, increment, decrement, nested, example
Id: HZARImviDxg
Channel Id: undefined
Length: 12min 42sec (762 seconds)
Published: Wed Jul 11 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.