Python Tutorial 13: Understanding Python While Loops

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter from toptechboy.com and we're here today with lesson number 13. in our incredible new tutorial series where you're going to learn python or you're going to die trying what i'm going to need you to do is pour yourself a nice strong cup of black coffee that is straight up black coffee no sugar no sweeteners none needed what i'm going to also need you to do is fire up your most excellent visual studio code and as you're bringing up visual studio code as always i want to give a shout out to you guys who are helping me out over at patreon it is your encouragement and your support that keeps this great content coming you guys that are not helping out yet look down in the description there is a link over to my patreon account think about hopping on over there and hooking a brother up but enough of this shameless self-promotion let's move over and let's get ready to learn all about while loops okay you guys that took my arduino classes you already know how to do while loops in arduino and what i'll be showing you today is how to do while loops in python now you guys that didn't take the arduino classes don't worry i'll show you enough today that you will be able to write your own most excellent while loops and so what i need to do here is i need to get out of your way and then we are going to start a fresh new program by coming over here we are working in the python files folder i will point there and then click on new file and we are going to call this file while loops remember don't put spaces in file names or folder names it's a poor programming habit and then i need you to do dot p y the dot p y is kind of important and boom we have a fresh new python file here just waiting to be written okay now i'm going to show you how to do while loops with a really simple program which is just a program that counts to 10. all right now you already know how to do for loops you would know how to do it in four loops i'm going to show you how to do it in while loops the big difference between a while loop and a for loop is the for loop sort of manages your counting index for you and with the while loop you've got to kind of manage your counting index on your own some people like the while loop better some people like the for loop better i kind of like the while loop better because it allows me to sort of directly have control over my accounting index and it's easier for me to make it do exactly what i want it to do so enough of this talking let's jump in and start coding i'll start with a happy little message of saying it is to print it is uh it is time to count exclamation point and then we are getting ready to create our while loop but remember we have to manage the counter so before the while loop starts i've got to start my counter so i'm going to call i'm going to use j as my counter you could use whatever you wanted to i'm going to say j is equal to 1 because i want to count from 1 to ten now i will start my while loop i will say while notice when i type in while it turns the happy little color because python recognizes it if i put a w-h-i-l-e uppercase you see no happy little color doesn't recognize it i would get an error and so while that's the way it needs to be now i got to put put the condition i want to keep looping while what while j is less than or equal to 10 and then i end up my while line of code with our old friend mr colon so you can see this format is a lot like an if statement it is a lot like a for loop it is just a while loop and then when i click enter i come over indented and i will be inside that while clause that while loop as long as i stay indented just a reminder you always want to do your indentation with your tab key you do not want to try to do that with the space bar if you do it with the space bar you are going to get messed up so always you do your indentation with the tab excuse me okay so i'll say while j is less than or equal to 10 now i'm going to say print i'm going to print j and now i want you to tell me what this program is going to do what is this program going to do it's going to do exactly what i told it to okay not what i wanted it to do it's going to do what i told it to do and you can see i have created an infinite loop where it is going to sit in loop forever and print j which is 1 because you see i set j equal to 1 and then i said loop as long as j is less than or equal to 10 and print j i never change j so it's just going to sit and loop forever so what do i need to do while i am still inside of the while loop i need to change j so i'm going to say j is equal to j plus 1 so i'm incrementing j inside of the while loop now this time i think also i will come down here and just show you i've taken the indentation out so this will be the first line of code after the while loop it should do this line of code one time so i will just say print and then that's all folks like that and we'll come over and run it and look at that boom it does exactly what i wanted it says it is time to count 1 2 3 4 5 6 7 8 9 10 and it says that's all folks notice that it's doing more precisely what i want it to i want it to go to 10 and it loops all the way to 10. now if you remember with the for loop if you say go to 10 it stops at 9. so if you want it to go to 10 you got to tell it to go to 11 and then it stops at 10. i like this with the while loop because it does exactly what i want it to in a little bit easier fashion now if you wanted it to behave like the for loop and stop one number before your indicated number you can do that with the while loop instead of saying while j is less than or equal to 10 just say loop as long as j is less than 10 and it would go 1 2 3 4 5 6 7 8 9 9 is less than 10 so it would do that and then it would go to 10 but it wouldn't do that because 10 is not less than 10. so this then my friend should behave a lot like the for loop and in fact it does boom do you see it went to 9 and then stopped so i just kind of like having this extra control i'm kind of a while loop man myself okay let me show you another thing that you can do with a while loop that's not so easy to do with a for loop i can make my counter or my index a floating point number and so instead of a round number like one i could make it 1.0 which makes j a float and then down here instead of counting by 1 i could count by 0.1 so i could say j is equal to j plus 0.1 so this should be 1 1.1 1.2 1.3 but this is going to point out one of the peculiar things about floating point numbers and that is they are not perfectly precise you have a little bit of uncertainty way way out in the last digit and as you keep adding that uncertainty adds up and so you don't end up with exactly 1.1 and exactly 1.2 and what that will do is that can kind of mess up this conditional up here so let me show you that we want to count from 1 to let's go back to 10 less than or equal to 10 and we want to count by 0.1 now let's see what happens here all right you see we start getting some of these kind of goofy numbers here we kind of are getting some goofy behavior it didn't count to 10 it counted 9.99999 now why is that the case well because of that imprecision so i start at 1 i go to 1.1 but by the time i get to 1.2 i start getting this little round off error way way way out in that insignificant figure and then that ends up impacting this conditional up here and we get kind of an unexpected behavior that we went to 9.9999 instead of going to 10. i'm going to show you how to fix that and you need to fix this if you are using a floating point number as a counter and that is you just need to round that floating point number off and so after i increment j by 0.1 i need to say j is equal to i need to round it okay i need to round what i need to round j and i need to round it to one decimal point and so what that will do is it will round you know the it will round this 1.1 to exactly 1.1 and it will round this 1.2 all these zeros too it will round it to 1.2 and then everything will behave just exactly like you want it to so let's try that and we run and let's come down here and see let me kill this and try it again okay let me run it and boom it goes all the way to 10.0 exactly what i wanted it to and it started at 1 and then it went 1.1 then if i wanted it to stop at 9.9 instead of 10 you'll stop at the number before the 10. i could just say j is less than 10 and then we run it and we come down here and boom it goes to 9.9 exactly what i wanted it to do okay so that should give you enough to be able to work problems with a while loop now what i'm going to do is i'm going to give you a homework assignment i need you to work this homework assignment without looking at my solution and the homework assignment i've given you enough information today and in earlier lessons to do this what i need you to do in the homework assignment is to write a program that will use a while loop to input a list of grades from the user and then a second while loop to print those grades out you don't have to average in high low and uh sort and all that stuff that we did with the for loop but i want you to show me that you can input a list of grades and print a list of grades using the while loop all right and so what i'm going to have you do is i'm going to have you pause the video go off and do this and then you'll come back and look at my solution but guys you really got to do these on your own because if you just follow along with me it looks so easy when i do it and you understand why i'm doing it and you think you understand it but you really don't understand it till you can do it on your own and so what i'll need you to do to do is pause the video and then come back in a minute and watch come back maybe after an hour after you've done it and then watch how i do it okay so one two three pause okay guys were you able to do it leave me a comment down below if you were able to do it type a comment down below i was legend or i am legend if you weren't able to do it just say that you folded up like a cheap walmart lawn chair okay so i will show you my solution so i will stay i'll keep doing this this same file this while loop file and i need to what we're going to do is what are we going to do we are going to get a list of grades from a person so i need to find out how many grades he has so i'm going to say num grades is equal to input now i've got to make this if i just do input it's going to input it as a string and we're going to get very unanticipated are very unusual results and so i got to tell it this is a number now since number grades is going to be my index i am going to make num grades an int and that way i won't end up with any of that strange round off stuff that i was showing you so then i'm going to input and then i'm going to say how many grades do you have question mark and a space okay and then that closes that first parentheses closes my input and then i need a second parentheses to close my uh to close my int all right so numb grades i've got that and then i come down here so now i know how many grades there are and so now i need to create my counter and i am going to create my counter j and then i'm going to set j equal to 0 all right because why because i'm going to put be putting these numbers in an array and the first position in an array is what the zeroth position so you've got to kind of remember how the arrays works and you might have gotten kind of goofed up on this point but the first position an array is the zero position therefore i need to count start counting at zero then i need an array to put my grades into so i will create that grades is equal to an empty array so now i have the empty grades array ready for the grades to go in i have my counter index set to zero and i need to know how far i'm going to go so i am ready now to write my while loop while and then j all right now this we gotta think about if i have let's say four grades and i'm starting if i have four grades and i'm starting counting at zero it is zero one two three okay so the last number that i want to go to is 3 not 4 because if i go all the way to 4 then i've actually input 5 numbers and so i want to say while j is less than or equal to if num grades no i don't want to say i don't want to say j is less than or equal to num grades because then if it was 5 it would go 0 1 2 3 4 5 and it would input 6 numbers and so how do i want to do this well i wanted to do it like the for loop does it i want to go as long as j is less than or equal to num grades and that way if i have four numbers it would go 0 1 2 3 right and by counting to 3 it would input 4 numbers and so i got to stop one before numb grades if you understand that stop and think about it because you've really got to understand where you are stopping all right now what do i do i am ready to input those numbers and so i will say here i will say that grd just a variable just a simple variable i'm going to read into is a float because you could have a number 92.5 so this needs to be a float and then i'm going to input and now i'm going to put my prompt and my prompt is going to be please want to be polite please input your grade like that okay and then this first parentheses closes my input the second parentheses closes my float and so at this point grd has that number as a number that you just input now what do i have to do i have to go put it in the grades array well how do i do that well i have grades and i dot what i dot append and what do i want to append the number grd all right so now what you don't want to forget is you've got to do what you've got to manage your index your counter j so j is going to be equal to j plus 1. so that will step through all the grades and input them okay now what do i want to do i come out of the for loop i mean out of the while loop now i want to create another while loop where i go in and i print out those grades so now i'm going to say while okay now you got to think about something right i'm going to say while j just like before is less than or just less than num grades for the same reason as before and then i want to print want to print grades of j and i'll try to get that out okay so after this what i have after this what i have is i have an array called grades that have all the grades in them now i want a second while loop that will step through and we'll print those out okay and then i am going to print that's all folks and i want you to see that that print statement this print statement is not in the while loops which you just printed out one time okay you guys tell me what it's going to do tell me what it's going to do all right so it comes in good news is it asked me how many grades i have i'm going to say 4 and then i'm going to say 90. oops you notice i didn't put a space in there uh 91 92 93 and that's going to be four grades and then it says that's all folks what did it do exactly what i told it to so you guys somebody tell me what my error is here well first of all i'm going to put a space in up here after grade so that it'll be formatted nicely okay but why did it input the grades and then not print them out i said well j is less than num grades and then it didn't i mean right here while j is less than num grades print grades of j didn't print anything out can somebody tell me what happened okay in a while loop who has to manage your index you do okay what two things did i not do well j is still set to four from up here because it went j is one j is two j is three j is four so it comes out of this first for loop with j still 4. what do i need to do i've got to reset j back to 0 okay and what else did i not do i didn't increment j inside my loop so i've got to say j equal j plus 1. and the reason i make these mistakes because they're really really hard to figure out you have to remember you have to before the while loop right before the while loop you have to you have to initialize your counter you have to set your counter and then you have to increment your counter inside the while loop so let's try this all right how many grades do you have i have four grades and i'm going to say 90 ooh you know what i didn't do i didn't point down here and click like i needed to okay i have four grades and i have 90 and i have 91 and i have 92 and i have 93 and boom it prints out the grades 90 91 92 93 okay so guys you have some pretty useful tools here as far as python goes you know your basic operators add subtract multiply and divide you learn the most excellent mod function how to read something to a power you then learned how to do if statements with simple conditionals and compound conditionals you learned how to do for loops you could step through numbers or you could step through fruits arrays or whatever and then today you learned how to do while loops and so you have got a pretty good tool kit that is building uh with your python skills okay so you've built some pretty nice python skills what i think i'm going to do next week is i think i'm going to show you a lesson on how now to store data to your disk and read data from a disk so you can read and store files data files okay so we'll do that next week and that will further increase your skills guys i hope you are having as much fun taking these pro taking these lessons as i am having making them if you like the video give us a thumbs up also if you haven't already subscribe to the channel when you subscribe make sure you ring that bell so you'll get notifications when my future lessons come out and then think about sharing this with your friends on social media okay guys this is paul mcquarter from toptechboy.com i will talk to you guys later
Info
Channel: Paul McWhorter
Views: 6,858
Rating: undefined out of 5
Keywords:
Id: KewbepRxrD0
Channel Id: undefined
Length: 21min 55sec (1315 seconds)
Published: Wed May 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.