 Okay, here's a quick walkthrough of some exercises about how to learn how to use loops to simplify your code and to make it more efficient and maintainable. So I'm gonna walk you through step by step and then it gets a little bit more complicated at the end but you'll see hopefully through doing this process you'll learn how to apply loops to various problems. So let's take a look here at number one. So it's pretty clear what this is gonna do. So let me go ahead and run this and this is the output we would expect from number one. Let me go ahead and just make that big. It's a little noisy here, I'm at work and everybody's been doing their thing. So you see I got one, two, three, four, five and what I've done is I've used basically some print statements, print one, print two, print three, print four, print five and this code works but what we wanna do is we want to eliminate any repeat code. So in this case that would be all these print statements are extraneous. So what we have to look for is the pattern involved here. So the pattern in this case is clearly one and two and three and four and five. So what I'm gonna do is I'm going to create a loop. So for X in range, I'm starting at one. It goes to five so that means I have to put six here because that's how loops work. It's not inclusive at the end. I'm going to indent this print statement, change the one to an X and then eliminate or delete I should say all of these print statements. Now if I run it and then I take a look at that, scroll up and you can see that I still have the exact same outputs. So I took five lines of code and using a loop I turned it into two lines of code. Now part two, number two. I want to do the exact same thing essentially. Again, I have repeated code but I also have a sequence of numbers in this case I'm looking at. So I'm starting at zero, I'm going to four. So the exact same thing. I'm gonna go ahead and make a loop four. And this time I'm gonna use the index as my variable name because this is the index of the word. So E is at index zero. The dash is at index one, index two, index three, index four. And the range here it starts at zero and it's gonna go up to four. So I should put five here. Or I could put length of word so it would work with any string but we'll just put the five here and make it easier to understand what we're doing. So just like last time I want to delete all of these extra ones. I've got to indent it so it goes in the for loop. I'm gonna put index. I'm gonna go ahead and run that and just check it, make sure that it works. And you can see here I still get E dash boy. All right, moving on, number three. Now number three looks a lot more complicated. There's more code but it's the exact same principle. There's no change here from the first two examples. I've got some repeated code and then I've got a sequence of numbers that I'm looking at. So zero, one, two, three, four, five, six, and seven. So I'm doing the exact same process. I'm gonna go here, I'm gonna add a for loop and again this is an index, oops, and see what happened there, there we go. For index in range, my range is zero and we said seven so I'm gonna put an eight there. Then this section of code needs to be indented and then I can delete all the extra code and don't forget here we have the zeros, this is gonna have to be index now. So I'm gonna go ahead and save that and I'm gonna run it and hopefully we'll get VSCO. Okay, you can see now VSCO. So this basically found all of the capital letters and printed them and not the lower case letters. So VSCO. Okay, moving right along. Number four, now we're starting to get a little bit more interesting, a little bit more complex. This requires a little bit of extra work but you can see we still have a sequence. We're still got zero and then we got zero, one, zero, one, two, zero, one, two, three, zero, one, two, three, four, zero, one, two, three, four, five. So what I'm gonna do and there are other ways to do this. I know before people start messaging me and leaving comments, yes I know we could do this a different way but I'm trying to do it this way for a certain reason which hopefully will become clear later. So I'm gonna be starting at zero, I'm gonna use X in this case, in range, zero. And I'm going up to five, so again I'm gonna use six in this case and let's say print. So if I did print X, for example, it's just gonna print zero, then one, then two, then three, four, five, up to five. But what I wanna do is I wanna keep adding a number each time. So what I wanna do here is I'm gonna make a string and I'll call it message, it doesn't matter. Message equals and I'm gonna make it blank. And what I'll do is each time through the loop I'm gonna say message plus equals STR string X. So I convert the number to a string. And I'm gonna say print message. And what that lets me do now is to delete all those lines. And now if I run it, I should get the exact same output. Okay, zero, zero, one, zero, one, two, three, all the way down to zero, one, two, three, four, five. Now notice, and this is important for later, because I put this inside the loop, it's printing every time. If I take it outside the loop, it will only print the final result, zero, one, two, three, four, five. Okay, so that's gonna be very important for solving some of the next ones. Okay, so number five. Now this just gets a little bit more complicated. So number five, we're counting backwards. So we're going five, four, three, two, one, zero. But what we're doing here is we're doing basically this same thing, but one, two, three, four, five, six times. So what I could do here is copy this, I'll copy the whole message thing here. So what you really wanna do is recognize kind of there's a pattern here. So this printed out zero, one, two, three, four, five as the last sequence. So basically if I run this right now, it's gonna give me zero, one, two, three, four, five. Okay, we just saw that. But what we want to happen is zero, one, two, three, four, five, zero, one, two, three, four, zero, one, two, three, zero, one, two, et cetera, et cetera. So we've basically got a sequence inside of a sequence. So what I'll do is I'll say four max in range. I'm starting at five, going to zero, so I'll just do negative one, and I'm counting down by negative one. Actually that's incorrect, that should be a six, because this is a six. So then here I'm gonna put max, and then I'm just gonna indent that. And so if I get rid of this and run it, okay, well, I almost got it. Zero, you can see I did it, but it's still adding. So after I print message, I actually put this inside the loop, I'll put message equals quotes. So let's move that there. Because each time that the main loop is done, I need to reset it. So that was my mistake there, so let's try that again. Okay, and there we go. So the first sequence was zero through five, the second sequence was zero through four, third sequence was zero through three, and so on and so forth. Okay, so you can see how we took this concept, and then we put that in here, because we recognize that there was a pattern that we'd already seen. And as you do these types of problems, you'll start to see more and more of these patterns, it'll be easier to spot. But you can't kind of think your way through them, if you know what you're doing. You know, this next one is pretty interesting. It's very, very, very, very similar to what we did, that's the wrong way, what we did here. Okay, so I can, in a way, I can use that almost as my, I can use that as my kind of template there a little bit. But you notice we also have the opposite coming in. So we got one, zero, one, two, one, zero, one, two. Again, I know I could do this a different way with some slices and things like that, but we don't want to do that, we're practicing loops here. So zero, zero, one, zero, one, two is the same problem we've already had. So I could just go ahead and actually just kind of copy that. Why not? It's basically number four. And so I'm gonna go ahead and just kind of put that in there. And I want to start, and this will give me zero, zero, one, zero, one, two. Let's test it, just make sure it's working. Sure, got that right. Okay, so we've already got half of this problem solved. Okay. So we're just missing the one, zero, one, et cetera, et cetera. So what we gotta think about is this pattern. So we got zero, one, actually nothing, because the zero comes from the other half. So we got nothing here. We've got one, two, one, three, two, one, four, three, two, five, four, three, two, one. So that's clearly going to be a, okay, I put it counting down. So what I'm gonna do is the following. Four max in range, zero comma six. Okay, and I did that same mistake. So message equals blank should go here. It doesn't have to be a message. I just, I find that easy to use. And then I'll put that into there. So let me test this and see if it does what I want it to do. Okay, so it's okay. So I'm still getting, okay, that should be unindentic because I'm getting too much output here. So let's run that. Okay, so you can see here I'm still getting this right part of the equation. Zero, zero, one, zero, one, two, zero, one, two, three. Okay, so now I just need the left part. So that has to come first. So to do that, now I'm gonna be counting down. So I'm gonna say for x in range, max, because I'm starting at the max, going down to zero and going down by negative one. And then message plus equals strx. So it's essentially the reverse of this. Okay, so let's go ahead and try that. And you can see now I've got the exact same result and I can delete these lines. Now of course, in this case, this is actually longer code. But now what I could do is I could make that 30 and no matter what I put there, it's going to work whatever values I use. So that's the power of using a loop. But we'll put that back to six so it matches. And now this last problem is exactly this problem, but we're adding spaces. So I'm gonna go ahead and just copy and paste that. So essentially what we've got here is we've got three parts. We've got spaces, plus the left side, plus zero, plus the right side. Now in my code, I combine zero and right into one set of code. That was this section here. Okay, that was that section there. I could have combined it left and zero. So I could have put that here, but I chose not to. So what I need to now do is just to add the spaces. So the question is, how many spaces do I need to add? So if you count these on the first line, it's five. Then it goes to four, then it goes to three, then two, then one, then zero. So what I'm gonna do here is I'm gonna say for X in range, now watch this, zero to five minus max. And I'm gonna say message plus equals space. Now I know in Python, I can do this differently. I'm doing it this way because when my students get to Java next year and they can't do that other way because it's only in Python, they're not confused. And I'm really trying to get them to practice loops here. So yes, I do know there are other better, easier ways to do this before anybody starts commenting. So what I'm doing is I'm adding spaces. So the first line, there's five spaces. Minus max, max is zero. And it comes around five minus max, which is one is now four, five minus two, five minus three, five minus four, and five minus five, no spaces. So let's go ahead and run that and you see we got the exact same result. So that is really the trick to these types of questions. Our problems or whatever you wanna call them is you need to look for patterns. And in the case of these last couple, we need to assemble it piece by piece and really think about where you're starting, where you're ending and what you're combining. This one was pretty tricky because of the five minus max thing, but it's something that you'll see in various programming activities and exercises and contexts. So it's good to kind of get an idea of how to do that. So that's it, that's how you solve those. Thanks for watching. Subscribe if you haven't and hit like, appreciate it. Take care.