Python Programming Series (Loops 4): Nested loops

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right everybody welcome back this is the final video in the looping section but don't worry in the next section we're gonna do a bunch of examples also with loops and we're gonna include some more of the Python functions and actually a bunch of really useful interesting functions that will help make your code and your any coding projects you have a lot more fun so in this one what we're gonna do is first start off with a basic nested for loop and nesting which we talked about in the previous section on if statements is when you take one control and statement put it inside another control statement for example the if statements are conditional statements we put a conditional statement inside another conditional statement like if a is less than five and if B is less than ten and then we print it hi I don't know something like that so this is the nesting again one inside the other and you do the same thing with for loops you can just write for in range 0 to 3 that's one for loop and J in range 0 to 3 that's another for loop and if I just print these both out it's totally fine ok so this here is a for loop and another for loop inside that for loop and then a print statement inside that for loop and you could do this forever you could just keep doing for loops as long as you wanted but most of the time you never see anybody nest more than 3 times if your nesting more than 3 times you're probably doing something wrong with your code or something you're doing something something that's gonna make your code unnecessarily complex so you should figure out a different way to structure it but in this case we're just gonna print out these numbers and judging by what we have here this should go 0 1 2 and this should go 0 1 2 remember it doesn't include the 3 the 3 is it's less than 3 but it includes 0 so in this case we should get 9 things to print it out or 3 times 3 so if I run this code I do get 9 things printed out let me expand this a bit we start off where I is equal to 0 and then we get zero one two so I is equal to zero and then it runs this three times inside here then it ends and the for-loop come back up to the top check i is equal to one now so we come down and we do it again and then i equals two does this again and then it comes back out and says well i is not equal to three so it's out of the range and cut the for loop so you get nine statements and if i were to change this for example to four i'm gonna get three times 4 or twelve possible statements so it's going to print out 0 1 2 3 0 1 2 3 0 1 2 3 4 each one okay so that's pretty neat nested for-loops a lot of good purposes things like i graph a screen for 2d graphics or 3d graphics you're gonna need to have nested loops to redraw the screen so you're gonna draw pixels horizontally and vertically if you have a grid of names that you want to search through a for loop will help you search through that grid of names because you need to search horizontally and search vertically okay so nested for loops very very common in programming nested while loops not so common you will see for loops inside while loops but ah but you rarely see a while loop inside a while loop I don't see it very often mostly because things you're doing inside of a while loop usually you kind of know that information you know how much you have so you just naturally use a for loop instead of a while loop ok so now that we have a basic idea of what a nested loop looks like let's apply it and do a quick example so I'm gonna erase this and I come over here and show you this little pyramid of zeros and we want to be able to print this pyramid of zeros out using nested loops so I'm gonna drag this back over here and comment it so we can use this as a guide I'll also move this down just bitte and then we can start writing some code so the first thing we want is we want to take a look at what we got we have seven lines okay so we want to print a loop probably seven times I'm gonna say range is zero to seven so that's our outer loop we're gonna print easy rows on each line we're gonna do seven lines and this first is actually seven characters say seven characters selected so this first zero is printed seven spaces out to the right and this next one is six and then five and four and three and two and then nothing okay so we want to be able to print that zero all the way out to the right so to do that I'm going to print seven spaces in here so for J in range zero to seven and then I'm going to print a space now remember a print statement uses an end line so if I do print this I'm gonna get a whole bunch of spaces in between my zeros I'll show you what that looks like and then we'll fix it so I put a zero here now you're saying print seven spaces that the top print my zero and we're good right so maybe you make this mistake maybe you don't you print it and you notice wait a second all my zeros are just zero and then seven spaces right after it so how do you fix that well let you take this and if you remember you put end is equal to nothing and end is a variable that is part of the print function that will tell it if end is equal to zero that means our end is equal to nothing that means change what used to be this a newline character if you remember the escaped codes used to be a newline character but now it's nothing so it means no no hitting enter at the end of my print statement so it doesn't go to the newline it just stays on the same line so now it's going to print seven and then we'll print a zero so if I do it like this we should see seven zeroes seven spaces out to the right and we do there is our seven zeros seven spaces out to the right so seven seven seven but that's not what we want we need this pyramid looking thing so I'll actually lift first let's try to get the left side of the pyramid and see how I can reduce the number of spaces each time the print statement goes so right now it's running seven times each line so on the first line we want seven but on the second line when I is equal to one we want this seven to be equal to six so how do you change that well if I is equal to one and we want the seven now to be six what happens when I is equal to two well when I is equal to the this line here it's actually the third line or the third number zero one two so we went to here this line is gonna be five spaces and then when I is equal to three we're gonna we're gonna want four spaces and so on so actually what we can do is just subtract I each time this goes so when we start this up zero seven - I put that that when I equals zero we're gonna get seven spaces when I is equal to one this loop comes in and this value changes to six spaces and then five spaces and then four and the three then two one and zero basically when I is equal to the our last value this is going to run one time and when it finishes we'll be good to go so let's run this and we get all the way out there's gonna be one little space here because this only goes to six so 7 minus 6 gives me one which means it's gonna run one time we could fix that but we're just gonna leave it as is right now it's it's okay it's not gonna hurt anything okay so that's good we've got we've got the left side of the pyramid that looks nice but now we need the inside of the pyramid how do we how do we make that well the first line has is one zero the next line has three five and seven and nine and so on so it looks like it's printing out the odd number of zeros per line so we want to we want to go one three and so on now we want that to be based on these numbers up here so I don't want to just coat it in for a pyramid of seven maybe later on I want to make my pyramid even bigger so I'm gonna put another loop here for K in range of zero to what well this first line I really just want one right so I just want it to be one and I want to print one zero and I actually want the same thing I don't want it to end but after I printed my zeros then I want a new line so I don't want to actually print a new line until the very very last thing I want to start with one zero then I want to make three zeros in five and then seven but I don't want to make a new line after that so now I've changed the printing of zero to be the same as the spaces so there's no new line at the end all right so let's think about this for a second how can I change this number so that I get something here so if I is zero I still want there to be a 1 so there needs to be one but on the second one which is the when I is equal to 1 I want this to be equal to 3 here I want to print three zeros so if I is equal to 1 and I have plus 1 I guess I could add another one and that might work let's see what happens when I do I plus 1 ok I plus 1 gives us the left half of the pyramid all right well that's that's pretty interesting how can I get the right half of the pyramid and while the you have a couple options here one you could make a new for loop another for loop and print it off to the right or you could figure out how to make this count up to an odd number okay so think about it for a second if I put if I is equal to zero and so I is equal to zero first and I have plus one say I do two times I plus one okay let's see what that would do so on this first line I is zero so it was gonna be plus one so I'll print one zero on the next line I is equal to 1 so 2 times I is 2 plus 1 is 3 on the third line I is equal to 2 so 2 times 2 plus 1 gives me 5 and so on all the way down to the bottom line so now if I run this I get a full pyramid all right pretty cool right the nice thing about this is that everything we did depends on I this depends on I and this depends on i I could have done this a different way where I just used values and said you know maybe count by use like account by odd numbers and it would go up and it would count by odd numbers up to a number that I specified and then it would be done but here everything depends on I which means I can come in here and if I change I all of these other things will also change okay the only difference is this is 7 so I'd have to fix that in order to make it not 7 as well so if I change this to 10 and 10 or even better if I said pyramid width is equal to 10 and I change it width and this 2 pyramid width now I can run this in any size pyramid that I want I will be able to get just based on the number I put in here okay so you can do this in a lot of different ways I could make it upside down pyramid I can make a right side up pyramid I could make any weird-looking thing in here I could use this and I could put I could use this as the pyramid of numbers and you could add and move down okay so you can make Pascal's triangle and other things like that okay so this is a kind of a complex example maybe this seems a little bit confusing to you I would take a look at this code type it in play with it yourself see if you can make different types of pyramids maybe try to make it upside down pyramid in fact if you look in the exercises just do the exercises and you'll actually see most of these examples it's it's really good to understand how nested loops work and how to get things to work how you want and even more importantly how to make the code short concise and understandable and adjustable by adjusting this I can now make any size pyramid I want it's actually very very it's very convenient very functional code okay so uh this is the end of loops take a look at the exercises that just said practice then move on to the next section where I'm going to show you a whole bunch of useful functions that we're gonna be using in the following tutorials really okay alright thanks for watching if you have questions leave them in the comment section in youtube or on the website live peel calm thanks for watching and see you soon
Info
Channel: Left Peel
Views: 102,489
Rating: 4.8899999 out of 5
Keywords: leftpeel
Id: shO5VbD2rNI
Channel Id: undefined
Length: 14min 45sec (885 seconds)
Published: Fri Jul 29 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.