Nested loops in Python are easy ➿

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody so I guess in today's topic I'm going to be explaining nested Loops it looks like so a nested Loop think of it as a loop found within the code of another loop you have a loop right any code within that Loop is indented underneath that Loop well you could have a looping structure found within the code of another looping structure the loop on the outside is the outer loop the internal Loop within the outer loop is known as the inner loop where you'll encounter nested Loops it's really situational you could have a while loop inside of a while loop a for Loop inside of a for Loop a for Loop inside of a while loop a while loop inside of a for Loop Etc so here's a demonstration let's begin by displaying the numbers one through nine but we'll use a loop 4 x x is our counter in range one comma ten remember that the second number in this case 10 that's exclusive then I will print our counter X this program will print the numbers one through nine now we have an exercise at the end of this topic I should probably explain this feature so with a print statement we end each print statement with a new line character if I need all of these numbers on the same line at the end of my print statement I can add comma and equals an empty string normally with a print statement each ends with the new line character but we can set that to be something else so when I run this again all of these numbers are on the same line or you could add a different symbol like Dash or space each of these characters is now separated with the space but let's stick with an empty string okay so we have used a loop to count the numbers one through nine what if I would like to repeat this three times well I could create another loop four x in range you could say one comma four or you could just say three either way whatever code is within this Loop will be executed three times let's cut our original for Loop then place it within the code of our new Loop our outer loop will have this code repeat entirely three separate times but we do have one thing we need to pay attention to we have two counters with the same name you'll want to be sure that they're different let's rename the counter of the inner loop to be y instead of X and be sure to change that here as well now when I run this code we're completing let's see 27 iterations to exit this for Loop we need to count the numbers one through nine once we do so that is one iteration of the outer loop but our outer loop is saying hey we still need three total iterations now if you would like these on separate lines let's make this look a little different let's add each iteration of the outer loop onto a new line So within the outer loop but not within the inner loop I'm going to create just a blank print statement this will just print a new line let's try this again with the inner loop we count the numbers one through nine after we exit the for Loop we will print a new line then repeat this all over again until our outer loop is satisfied so that's basically a nested loop it's just a loop that's inside of another looping structure so let's create a project we're going to print a rectangle made of some symbol that we set we'll have the user type in how many rows and columns this rectangle will have we'll reuse this code that we have already written so this time let's accept some user input rows equals input enter the number of rows then we should type cast this input as an integer let's copy this line paste it change rows to columns for the second line enter the number of columns then let's create a symbol symbol equals input enter a symbol to use we already have this rectangle structure right think of it as the outer loop is in charge of the rows let's change in range 3 to in range rows the inner loop will be in charge of the columns 4y in range columns we will print our symbol whatever the user chooses so let's try this again enter the number of rows how about four rows 10 columns I'll use a dollar sign so here's our rectangle we have four rows then 10 columns 1 2 3 4 5 6 7 8 9 10. let's try one more time three rows five columns and I'll use an asterisk yep three rows five columns so yeah that's a nested Loop really it's just a loop that's inside of another loop the type of loop really doesn't matter as well as what's within each loop it's just a situation where you have a loop inside of another loop and yeah those are nested Loops in Python
Info
Channel: Bro Code
Views: 89,073
Rating: undefined out of 5
Keywords: python tutorial for beginners, python course, python tutorial
Id: APWy6Pc83gE
Channel Id: undefined
Length: 5min 34sec (334 seconds)
Published: Wed Oct 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.