Python Tutorial 9: Understanding For Loops in Python

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 nine in our incredible tutorial series where you're gonna learn python or you're gonna die trying what i'm going to need you to do is pour yourself a nice strong cup of black coffee that would be strong black coffee no sugar no sweeteners no creamers none needed you guys have got to learn that it is coffee it is caffeine that fuels the engineering world you guys on this channel out there you're like fighter jet aircraft but your fighter jet isn't gonna get off the ground if it doesn't have its jet fuel think about it okay and besides your coffee i also need you to 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 jump in and let's look at what we are going to learn today and as promised in the most excellent tutorial number eight we said that today what we're going to do is we're going to learn how to do for loops in python now those of you who have already taken my arduino series of tutorials and i certainly hope you have you already kind of have an idea of the concept of a for loop for you i will be doing is just showing you the syntax that you use in python to get a for loop those of you who have not taken my arduino lessons and i think you know who you are never fear i will go and explain how for loops work from scratch so again this series of tutorials is for absolute beginners and the goal is not to leave a man behind or a woman behind for that for that matter we don't leave anyone behind but what we're going to learn is we are going to learn how to work with for loops and at its most simple level a for loop is a clause that will execute a certain number of times so you'll have a lump of code that you will step through and execute that same line of code for some specific number of times and so it'll make a lot more sense watching me do it than having me talk about it now here is a difference between python and arduino and it's between python i would say probably most programming languages now if you're used to arduino you're not going to like this this python thing is i don't like that but once you start playing with it it actually makes a lot of sense and it really lets you do a lot of pretty amazing things now in the world of arduino the way you would do a for loop is you would kind of have a start value a stop value and an increment value a start stop and increment and your little counter will start at the first number and go up through the end number and it will increment each time by your increment value it's kind of it's kind of really great it's like old school programming for j equal 1 to 10 in increments of one go and then it'll go through that loop j is one two three four five six seven eight nine ten and then it'll drop out okay that's the way most programming languages work but actually python is kind of different and let me sort of show you so we're gonna go over to visual studio code and then we are going to come in here and we are operating in our python files and so i'll come there and then i will click a new uh file and i'll say for in python dot py the dot py actually is kind of important and boom we got ourselves a fresh new program just ready to be written and so let's kind of let's kind of maybe review a little bit it's it's a little bit different we got to go back and think a little bit about lists or arrays and i think we learned about that in lesson number one or lesson number two and so i could create an array and i will call my array fruit and fruit is equal to i start the array or i start the list with brackets and visual studio closes the brackets for me already so we could have the fruit apple and then i could put a comma and then i could have the fruit apple or or range and then i could have the fruit bun and then just for our tropical friends we will throw in mango and we will throw in kiwi so this is my list of fruits okay now if you remember how this worked is i could just say print and then i could print fruit and if i do that what is it going to do do you guys remember it is going to print the whole list of fruits do you see that there apple orange banana mango kiwi that's what it does all right or what i could do is i could print a specific one so like if i wanted to print apple i would say that's the first one so i would print fruit and in fact i think this is a list of fruits i should say fruits and i should say fruits and so i could print fruits of one and you would probably expect that you would get apple and when i do this you find out you would be wrong you don't get apple you get orange and why do you get orange instead of apple when you print fruits of one well that's because as programmers and as programming languages you start counting with zero so this position in the list is the zero position this is the one position so if you really wanted to print apple you would need to print the zero position in the listing i know we talked about this before but you really got to remember this because it's kind of important so then boom there is apple all right so let's get rid of that nonsense so you can make arrays or list and you will see better if i get rid of that look at that you see that a lot better you can do that uh using arrays now also you could say uh this is another kind of thing you got to keep in tr in mind with uh with uh arrays or lists so let's say you can go from one i want to print all the fruits in the fruits list from position one through let's say position four now what would you expect this to print well you would expect it to print this is zero one two three four you would expect it to go from orange through kiwi and if you expected that you would in fact be wrong why because it's just a quirky little thing about python and you've got to understand it or you're going to mess up or you're going to get confused and you're going to have trouble really getting it when you say one to 4 that means you go from 1 okay up to 4 but don't include 4 so it says go from 1 which would be 0 1 and then you would stop at 1 2 three because three is the last one before four so when you say you wanna go to four you gotta really go to uh you you know you've really gotta go one off from that so i guess if we wanted all of them will it do this i am not sure but let's try that okay yeah so when i say go from the first position to the fifth position this is zero one two three four five stop one before five which would be four kiwi and we've got that and so i don't want to go into all the reasons there's actually reasons they did it this way but just understand that when you say one to five it means stop before you get to five the last one before five all right so i didn't write it i'm just telling you what it does all right so that is that now we have really digressed we need to get back to for loops but just needed to kind of review arrays okay and just to also show you i could also say fruit is equal to i could say fruit is equal to fruits of 2 and then i could print and i want to print fruit singular what is that going to print tell me what it's going to print all right if you said banana you would be right because this is the 0 position the 1 position and this is the 2 position and so fruit became fruits of 2 which was banana and then i printed it all right and this is kind of like a standard that a lot of times if you're going to make a list you'll make it plural like fruits or grades and then if you want to go grab one out of the list you would call that fruit singular or grade singular well now this is where the for loop comes in the for loop in python does not think in terms of start number stop number and increment it thinks in terms of a list and then it steps through the list and so this is the way it will work i could say i can get rid of this nonsense and then i could say for fruit singular in fruits and then you start the for loop with a semicolon a lot like an if statement and then what am i going to print well if i said print fruits every time through it would print that whole list okay i don't want to print the whole list i want to print kind of like what is my index fruit so the first time through fruit is apple the next time through fruit is orange the next time through fruit is banana because fruit is step fruit is stepping through fruits and so let's see if that works like that and here we go and boom do you see that apple orange banana mango kiwi fruit stepped through fruits and then each time through it printed now we just i also want to show you that how do you end the for loop well you stop indenting and so you kind of start with the colon and then after that everything that is indented is going to be in the for loop now i got to warn you that you know you want to do your indentation with a tab you don't want to do it with like spaces you want to do it with a tab because you can get yourself messed up so that first one is like that okay and then i can come down and now i want to leave the for loop and so i can put as many lines of code here as i want and they will all be in the for loop until you stop indenting and when you stop indenting then this would be the first line of code that is not in the for loop of course that's not real code that is gibberish but let's just say i can do this that it's going to print the fruits and each time through it's going to print one fruit and then i could just say print and then i could print that's all folks like that and now let's try that i like to clear that every time and so let's come up here and boom i've got apple orange banana mango kiwi that was done in this loop and then the first line of code after the loop was this print statement that it's going to print one time because it's not in the for loop and it is going to print that it's all folks now of course it would be kind of silly if you're not paying attention and you put that in the for loop what's it going to do it's going to print it every time apple that's all folks orange that's all folks mango that's all folks kiwi that's all folks but that's a common mistake i see is putting something in the for loop that should not be in the for loop now you can also put four loops inside of four loops but if you wanted a for loop inside this other for loop it has to be indented so it is inside the for loop and here i could say for and i could say letter in fruit right letter in fruit and then i could print letter now what is this going to do well this is going to step through fruits and so the first fruit would be apple now when you get to this for loop the first time through the first for loop fruit is going to be apple and now it's going to letter now letter isn't a special word i just could could have called it l for l in fruit and then print l it is going to what what do you think this is going to do can you predict what this is going to do can you predict what this is going to do let's try it okay boom look at this so it prints apple because the first fruit is apple and it prints fruit and then it's going to step through the word fruit and it's going to step through each letter now again we don't have to call this letter we could just call it l whatever variable is going to step through fruit and then print l okay let me let me kill that make sure that we actually run it okay now you see the first time through fruit is apple it prints fruit and then it is going to step through the letter l is going to step through the word fruit one letter at a time which would be a p p l e and then it goes through the main loop a second time in which case fruit is going to be the next one which is orange and then it prints orange and then it's going to step l through the word orange and it's going to print each letter at a time o r a n g e and so this just shows you how you can step through you can have a loop inside of a loop and then finally this print that's all folks it is not in the the main for loop and it is not in the second for loop and so therefore it just prints it one time does that make sense okay so that is showing that in python the for loop the index which is our case froze fruit steps through the array fruits well what if you just wanted to count to 10 well in that case what we would do is we would have like my numbers and then we would make an array of numbers i want to count to 10 and so i am going to do 1 comma 2 comma 3 comma 4 comma 5 comma 6 comma 7 comma 8 comma 9 and we wanted to count to 10. and then now i'm going to save for my number singular in my numbers the array and then i want to print the my number my number because that's going to be stepping through and then i don't want this other nonsense here and then it will print that's all folks and so what should this do this should count to 10. so let's clear this and let's come up here and then we are going to count to ten so we go one two three four 5 6 7 8 9 10 and then say that's all folks so you see you're stepping through a list with a for loop in python now i know what you're thinking you're old school you're an old school programmer and you want to just count you want for j equal one to ten in steps of one you don't want this not nonsense because what if you wanted to go from one to a thousand you would not want to come up here and have to create this array well i have good news for you is python has a function that will create the array for you and that function is called range like home on the range for my number in range and this rain function range function will generate it will generate that list for you that it will step through and just understand because it's a function we are using the parentheses not the square brackets and we want to start at 1 and we want to count to 10 and we want to go in steps of one and so then mine is range is going to create 1 2 3 4 5 6 7 8 9 10 you would think and then it is going to print 1 2 3 4 5 6 7 8 9 10 you would think right and so let's come over here and let's clear all this and let's run this boom denied what happened it went to nine because again it is this crazy characteristic of python when you say go from 1 to 10 it stops 1 before 10. and so if you really wanted to go to 10 you've got to tell it 11. so remember it's from 1 to 11 and stop 1 before you get to 11. kind of crazy huh kind of crazy but let's try that we want to count to 10 so we say start at 1 and then stop 1 before 11 and increment in increments of one and we get boom one two three four five six seven eight nine ten okay what if we wanted all the even numbers between between 10 and 20 well we're going to count in increments of 2 and then where do we want to start we'll start is easy we're going to start at 10 and then we want to go through 20 and we want it to include 20. so we'll just say go to 21 and let's see what happens boom 10 12 14 16 18 20. who's your huckleberry i'm your huckleberry all right it is this increment of two that makes that happen okay and so that is starting to give us uh you know kind of control over this thing but always remember you got to kind of think of this one so what if i wanted to count backwards from 10 well i would start at 10 and i want to go to 0 but really since i want to go to 0 i'll go to -1 and then i want to take steps of -1 and i'm kind of doing this on the fly and so i hope this actually works we're going to clear this and then we're going to come in and do this and then let's see oh look at that 10 9 8 7 6 5 4 3 2 1 0. okay why because i went one past zero so go to the last number before minus one in your sequence and so you'll get used to this but remember it because if you don't do python for a while you can very easily forget that but we just did a countdown okay so let's kind of get back to more like what we would do traditional you know uh let's say that we would kind of do more like traditional programming i had to show you all this so that you see what how it works and what it can do and all the things that it can do but now let's get back to kind of what you would normally be doing and so this is going to be a homework assignment for you and then i want you to pause the video and then i will come back you pause the video and you write this code on your own and then come back and you can watch me do the coding but really guys you've got to do this on your own you can't just sit and watch me do it because the way you learn to code is making mistakes and having to sit through you know having to think through it and having to struggle and having to get frustrated and banging your head against the wall and then you figure it out and it's just like this epiphany and you get joy and then you really learn how to code if you just sit and top his eye type you are never going to be a coder okay here is your assignment prompt the user for the number of grades that he has okay prompt the user for the number of grades he has then read those grades in okay one at a time so if if he says he's got five grades then you've got to ask him for grade one grade two grade three grade four grade five okay you've got to ask him for five grades okay and then what i want you to do is print out the grates but i want you to do it with four loops so that would be two for loops the first for loop reads the numbers in and then the second for loop prints the numbers out okay and so that is what we are going to do and so i want you to go ahead and pause the video and then come back after you have figured it out and watch me do it so one two three pause okay now enjoy a little coffee while you were working on that how many of you were able to do it if you were able to do it leave a comment down below i am legend if you weren't able to do it you might just indicate that you folded up like a cheap walmart lawn chair okay so let's see if we can do this i am going to come here and what is the first thing that we need we need to know how many grades the user has so i'm going to say num grades okay and num grades is uh is is you get that you've got to ask the guy how many grades he has so you say num grades is equal to it's going to be a number so you want to float it and then you want to input it and uh you're going to say how many gray ooh i better put that in that's a prompt that's a string better put it inside the single quotes so that is going to be how many grades do you have and then a question mark and a space for nice formatting now what do i want to do well i want to step through a list so i'm going to say 4 and i'll use i in range okay so i'm just going to use a simple counter i which is going to step through the list that is created with range and i'm going to start with the number one and then i'm going to go to num grades and then i'm going to go in increments of one and then we do start a for loop with a colon and then here we are going to uh we are going to be indented so this will be inside the for loop and then i i'm going to say grades grade is going to be equal to i'm going to input and then i'm going to input please enter your please enter your grade okay and then since that's a number i sure better float that and then i got to close the float so i close the string i close the input and then i close the float so now i'm going to step through and i'm just going to be reading in a grade from him but the problem is with grade it is just going to read it and read it and read it i need to put it into an array and if i'm going to put it into an array if you remember from lesson one or two you've got to create the empty array up here i'm going to call that grades plural is equal to an empty array open bracket close bracket is a list and there's nothing in it so it is an empty list now each time through i want to read grade and each time through to through i want to put grade into grades and so now i'm going to say grades dot append right that's how we add something into grades what do we want to impend great okay so what do i do here i get the grade from the user and then i append grade to the empty array grades okay i append grade to the empty array grades all right and so let's see what happens here and now i want to go through and i want to print those so now i'm going to do it again for i in range and i'm going to start at 1 okay and then i am going to go through num grades times and then i'm going to increment in increments of one and then what is going to happen i am going to print and what am i going to print you got to think about it i am going to print grades of i okay so the first time through i is one the second time through i is two the third time through i is 3 okay and so it should print out those grades right and so then just for good measure after that i'm going to say print and i'm going to print that's all folks okay now you would think this would work wouldn't you so let's look at this and say how many grades do you have i'll click down here and i'm going to say three and then whoa float cannot be interpreted as integer ah okay i remember something now that this range command wants integers and num grades was a float okay because it doesn't want to step through something 3.2 times it steps through it in integers and so what we see here is again a variable type problem we need to make that an int because that's the round numbers that's the counting numbers and range is a counting function so we have to make num grades on int all right so let's try this and so now how many grades do you have i have three and then please enter your grade and right off the bat i see i didn't format it very well but i'm going to say 90 and then 95. oh index out of range what happened hmm what happened okay i want you to go back and think about how we count i want you to think about how we count in programming languages do we start with one no we don't we start with what zero okay and then when we print similarly we need to start at 0. so you see i was taking that number and i was putting it in the one spot where i should have put it in the zero spot and so what i will do now is i will go from zero all right to num grades in steps of one now if i have three grades it's going to be grade 0 grade 1 and then grade two okay which is three numbers but then num grades is three so it stops one early does that make sense it's zero one two that's how many grades it's three but then remember if num grades is three it's going to stop one early anyway and so this is kind of the way they did it you start counting with zero you want to go to num grades but it stops one before num grades which turns out to be the right one i hope that makes sense zero i want three grades zero one two that's three grades num and that's index two num grades is three but it stops at index two but if you just always go zero to num grades it'll stop one before num grades which is where you wanted it to stop anyway so let's do this and see if this will work this time i sure hope it does okay so let's see all right we're going to run this baby how many grades do you have i have three grades in my first grade and i forgot to fix that formatting my first grade is 90. okay my second grade is 95 and my third grade is a hundred and ninety ninety five a hundred that's all folks boom okay now let's try to make this a little bit nicer i should have on inputting the grades here i should have put a colon in a space so it'll look a little bit nicer and then here before i just print out the numbers i should give a little prompt and i should do it outside the for loop and just say print and then say your grades are and like that and then i should print the grades out and so let's clear this and let's try this again okay so how many grades do you have i have three grades and i have 90 and i have 95 and i have 100 and then boom your grades are 90 95 and 100. now where did the grade 90 go in position 0 where did the grade 95 go in position 1 where did the grade 100 go in position two now num grades is three but it goes zero one two it stops before you get to num grades three but it records all three numbers i know i'm repeating myself but i don't want you to get confused by this because num grades is three but it's not one two three num grades is three and so you go zero position 1 position and then position 2 and that's 1 less than num grades and you end up at the right spot so in a crazy roundabout way since you start counting at 0 you want to stop one before numgrates which is how the range function works go back and watch this again if it doesn't make sense to you okay watch it again if it doesn't make sense to you okay so we some of the things that hopefully you remember again guys you've got to remember what i teach you in earlier lessons and maybe you couldn't do this homework because you didn't remember how to put numbers in an array you start with an empty array and then you append your numbers to that array you append it and so at first grades was empty and in fact let's just do that okay as we're inputting the numbers here let's print the entire array of grades okay and you'll see how it's building it up as we do this that's kind of a neat thing i think let me let me kill that okay and i come up here and now how many grades do you have we're going to say three and then it says please enter your first grade and it's 90. and now when i print the list instead of the list being empty the list has one number in it and that's 90. now it wants my next number 95. okay now look at my list my list is 90 comma 95 and then it wants my third grade 100 and then look at that the array is the list 90 comma 95 comma 100 then we have i that is going to go from 0 to num grades and steps of 1 and it's going to print grades of 0 is 90 you see which is 90 and then grades of 1 which is 95 and then grades of two grades of two which is a hundred so this is grades of zero grades of one grades of two and then you never get to num grade because you stop one before as you should hopefully that makes sense guys hopefully that really makes sense okay i hope you guys are having as much fun taking these lessons as i'm having making them going to give you a homework now this one you got to go do on your own and then i'll show you the whole i'll show you the solution to the homework next week okay i'll show you the solution to the homework next week but i really want you to grind this one out okay i want you to ask the user for how many grades he has i want you to input those into an array and then i want you to average the grades and then i want you to print out the average and all the grades all right now i'll give you a hint you already know how to input the grades i've shown you that here you already know how to print out the grades i've shown you that here but now you are going to need to average the grades i will give you a hint there are going to be three for loops the first for loop you input the grades the second for loop you average the grades and the third four loops you print the grades out so you'll say like your average is 95 and your grades are 90 95 100. that's the way it should work you guys grind this out man pound it out you're not going to learn to be a coder you're never going to be a coder if you just sit and watch me do it now what we're going to have is we're going to have two groups of people in the world those who are legends and those who fold up like cheap lawn chairs all right we want you to be a less we want you to be a legend so figure this out and then if you really really really get hung then next week you'll see my solution also even if you get it you'll sort of see are you doing it the same way i am all right guys this is paul mcquarter from toptechboy.com if you guys enjoyed this lesson give us a thumbs up be sure to subscribe to the channel hit the bell when you do so you'll get notifications think about sharing this with your friend again paul mcquarter from toptechboy.com i will talk to you guys later ah that is good
Info
Channel: Paul McWhorter
Views: 14,595
Rating: undefined out of 5
Keywords:
Id: IzVJB-OYk9A
Channel Id: undefined
Length: 36min 39sec (2199 seconds)
Published: Wed Apr 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.