1 00:00:00,120 --> 00:00:03,760 Hello and welcome to Chapter 5, loops and iteration. 2 00:00:03,760 --> 00:00:08,240 As always, this lecture is copyright Creative Commons Attribution, including 3 00:00:08,240 --> 00:00:12,680 the audio and video and, and the slides, and the book even. 4 00:00:12,680 --> 00:00:18,800 So, now we're getting to our fourth basic pattern. 5 00:00:18,800 --> 00:00:22,180 We've talked about sequential, where steps happen one after another. 6 00:00:22,180 --> 00:00:25,270 We've talked about conditional, where steps may or may not happen. 7 00:00:25,270 --> 00:00:27,580 In Chapter Four we talked about the store and retrieve pattern. 8 00:00:27,580 --> 00:00:29,750 And now we're going to talk about the looping pattern. 9 00:00:29,750 --> 00:00:33,820 And the looping pattern is the last of our really foundational ones, and 10 00:00:33,820 --> 00:00:38,840 it potentially is the most important one, because it's the thing that allows us 11 00:00:38,840 --> 00:00:41,540 to get computers to do lots of things that, say, 12 00:00:41,540 --> 00:00:45,770 humans might get tired of, but computers don't tire of. 13 00:00:45,770 --> 00:00:50,460 And so after this we'll start sort of becoming a little more 14 00:00:50,460 --> 00:00:52,710 skilled in the basic language capabilities. 15 00:00:52,710 --> 00:00:56,120 We'll talk about strings and, and then start to talk about files. 16 00:00:56,120 --> 00:01:00,830 Add start doing some real work after we get done with this, so bear with us. 17 00:01:00,830 --> 00:01:03,380 It's going to be a while, but we'll get there. 18 00:01:03,380 --> 00:01:07,050 We'll get there. So, welcome to repeated steps. 19 00:01:07,050 --> 00:01:12,760 This is the example that I had in the first, first lecture, Chapter 1. 20 00:01:12,760 --> 00:01:15,310 And the basic idea, just to review, 21 00:01:15,310 --> 00:01:18,150 is that you have this while keyword. 22 00:01:18,150 --> 00:01:20,620 The while keyword sort of functions like an if in that 23 00:01:20,620 --> 00:01:23,890 it implicitly has a decision that it's going to make. 24 00:01:23,890 --> 00:01:26,890 And it's either going to do the code in the 25 00:01:26,890 --> 00:01:29,860 indented block or not do it, or skip it basically. 26 00:01:29,860 --> 00:01:33,020 Alright? So you either do it or you skip it. 27 00:01:33,020 --> 00:01:36,280 The difference between the while and the if is that it's going 28 00:01:36,280 --> 00:01:40,780 to do it many times, as long as this question that we have remains true. 29 00:01:40,780 --> 00:01:42,500 Okay? 30 00:01:42,500 --> 00:01:48,100 And so, in this case, n is 5, while n greater than 0 functions like an if. 31 00:01:48,100 --> 00:01:52,850 So yes, it's going to run it. Prints out 5, subtracts 1, so it's 4. 32 00:01:52,850 --> 00:01:54,160 Goes back up. 33 00:01:54,160 --> 00:01:58,100 Goes back up and asks the question again, is n still greater than 0? 34 00:01:58,100 --> 00:02:00,830 Well, since it's 4, yes, we'll continue on. 35 00:02:00,830 --> 00:02:05,823 Out comes 4 then n gets subtracted. 3, 2, 3, 2, and then 36 00:02:05,823 --> 00:02:09,579 we come through, we print 1. Print 1. 37 00:02:09,579 --> 00:02:13,230 We subtract n to 0. We go up, we go back up. 38 00:02:13,230 --> 00:02:16,940 n is now not greater than 0, so we come 39 00:02:16,940 --> 00:02:20,730 up and we execute outside the loop, we leave the loop. 40 00:02:20,730 --> 00:02:22,630 And that really means in the Python code we 41 00:02:22,630 --> 00:02:26,360 skip to whatever is lined with the while statement. 42 00:02:26,360 --> 00:02:29,290 The same indent level as the while statement. 43 00:02:29,290 --> 00:02:31,262 And so that's how it works, and 44 00:02:31,262 --> 00:02:36,050 I just print n at the end here to remind ourselves that n ended up at 0, not at 1, 45 00:02:36,050 --> 00:02:38,930 the last thing we printed out in the loop. The last thing we 46 00:02:38,930 --> 00:02:42,800 printed out in the loop was the 1, but n ended up at 0 because 47 00:02:42,800 --> 00:02:45,670 it was, this loop was going to run as long as n was greater than 0, 48 00:02:45,670 --> 00:02:49,830 so n had to sort of be not greater than 0 to get out of the loop. 49 00:02:49,830 --> 00:02:50,380 Okay? 50 00:02:50,380 --> 00:02:52,610 So that's basically a review of what we've done. 51 00:02:54,000 --> 00:02:56,170 Now, oh, wait, wait, wait, wait, 52 00:02:56,170 --> 00:02:59,460 wait, something else. Iteration variables. 53 00:03:00,740 --> 00:03:04,440 Okay, so the key to this is these loops can't run forever. 54 00:03:04,440 --> 00:03:06,010 We don't want them to run forever. 55 00:03:06,010 --> 00:03:08,310 We want them to run until, as long as we want them to run. 56 00:03:08,310 --> 00:03:11,570 They may run a very long time, but not forever. 57 00:03:11,570 --> 00:03:13,300 There's got to be a way to get out of them, otherwise we 58 00:03:13,300 --> 00:03:15,950 call them infinite loops, which we'll talk about in the next slide. 59 00:03:15,950 --> 00:03:19,950 And so the iteration variable is generally some variable 60 00:03:19,950 --> 00:03:21,299 that is changing each 61 00:03:21,299 --> 00:03:26,004 time through the loop, and we're changing it by subtracting 1 to it, from it. 62 00:03:26,004 --> 00:03:28,227 And so this thing is going to keep running, and we 63 00:03:28,227 --> 00:03:30,985 can pretty much see that Oh, this is going to exit, right? 64 00:03:30,985 --> 00:03:34,774 Whatever n is, it could be a large number, but eventually it's going to get to 0. 65 00:03:34,774 --> 00:03:35,292 Right? 66 00:03:35,292 --> 00:03:40,720 So the iteration variable controls how many times the loop runs. 67 00:03:40,720 --> 00:03:43,540 And it also allows us to do something different inside the loop. 68 00:03:43,540 --> 00:03:44,870 And, of course, this is like a trivial 69 00:03:44,870 --> 00:03:47,420 loop, where we're just printing the iteration variable. 70 00:03:47,420 --> 00:03:50,100 But it just means that this loop is going to run 71 00:03:50,100 --> 00:03:53,730 five times and it's going to do something potentially different each time. 72 00:03:53,730 --> 00:03:55,690 If you just ran the loop that did the same thing over 73 00:03:55,690 --> 00:03:58,400 and over and over again, with no data changing, that's kind of dull and pointless. 74 00:03:58,400 --> 00:04:01,147 So just because you have an iteration variable 75 00:04:01,147 --> 00:04:05,510 doesn't mean that you've properly constructed your loop. 76 00:04:05,510 --> 00:04:09,250 It's a, it's a common problem or something we want to avoid, 77 00:04:09,250 --> 00:04:13,010 is an infinite loop. And here is a, a carefully constructed loop. 78 00:04:13,010 --> 00:04:15,540 We start n at 5 at the beginning. 79 00:04:15,540 --> 00:04:18,770 We have a good question at the end, while n greater than 0. 80 00:04:18,770 --> 00:04:20,120 It's going to run this 81 00:04:20,120 --> 00:04:22,035 as long as n is greater than 0. 82 00:04:23,100 --> 00:04:25,740 But the problem is, is we don't change in the little block. 83 00:04:25,740 --> 00:04:27,440 We don't change the n. 84 00:04:27,440 --> 00:04:29,810 Which means it's going to come back and n is going to be 5. 85 00:04:29,810 --> 00:04:31,840 And it's going to run this and n is going to be 5. 86 00:04:31,840 --> 00:04:34,100 And it's going to run this and n is going to be 5. 87 00:04:34,100 --> 00:04:36,930 And so this is an infinite loop, which means this loop will never exit. 88 00:04:36,930 --> 00:04:38,530 It will never get out, 89 00:04:39,602 --> 00:04:43,310 it's just going to run forever in here, because n is not changing. 90 00:04:43,310 --> 00:04:45,250 Neither of these statements change n. 91 00:04:45,250 --> 00:04:49,580 So part of the iteration variable is there needs to be something that changes 92 00:04:49,580 --> 00:04:51,870 so that the loop will ultimately make progress to 93 00:04:51,870 --> 00:04:54,210 accomplish what it is and know when to stop. 94 00:04:54,210 --> 00:04:56,500 So this is an infinite loop and, of course, 95 00:04:56,500 --> 00:05:01,470 Lather, Rinse, Repeat is commonly put on shampoo and conditioner. 96 00:05:01,470 --> 00:05:03,520 And so you know, you can, next time you are 97 00:05:03,520 --> 00:05:04,755 in the shower, take a look at your shampoo and 98 00:05:04,755 --> 00:05:07,905 conditioner and find the infinite loop that's, 99 00:05:07,905 --> 00:05:12,120 that's on most bottles of shampoo and conditioner. 100 00:05:14,350 --> 00:05:15,390 Now here is another loop, 101 00:05:17,970 --> 00:05:20,460 just to emphasize that it's possible to structure 102 00:05:20,460 --> 00:05:22,810 these loops in a way that they never run. 103 00:05:22,810 --> 00:05:24,990 So this function is, is an if. 104 00:05:24,990 --> 00:05:27,510 The while functions as an if. 105 00:05:27,510 --> 00:05:31,720 And so when n is set to 0 and we ask the question, 106 00:05:31,720 --> 00:05:34,590 it is literally going to make the decision based on n greater than 0. 107 00:05:34,590 --> 00:05:38,780 Well it is not greater than 0, so it's going to go right by it, over 108 00:05:38,780 --> 00:05:43,270 here it's going to come in here and go right to there and never run these 109 00:05:43,270 --> 00:05:46,850 lines of code. And that's, we call this a zero trip loop. 110 00:05:48,990 --> 00:05:51,850 And that's okay. I mean, this is a silly one, of course. 111 00:05:53,570 --> 00:05:57,500 It just shows that the test, the question that's being asked is 112 00:05:57,500 --> 00:06:00,660 above the lines that make up the body of the loop. 113 00:06:00,660 --> 00:06:03,270 And if it's false, the loop never runs. 114 00:06:03,270 --> 00:06:06,650 So it's possible that these loops have zero trips. 115 00:06:06,650 --> 00:06:07,690 Okay? 116 00:06:07,690 --> 00:06:09,100 So, that's a loop. 117 00:06:09,100 --> 00:06:14,310 Now, there are more than one way to sort of control the flow 118 00:06:14,310 --> 00:06:15,680 of a loop. 119 00:06:15,680 --> 00:06:17,540 The basic flow of the loop is when it gets to the 120 00:06:17,540 --> 00:06:20,572 bottom, it goes back up to the while, and does the check. 121 00:06:20,572 --> 00:06:22,240 This is a different way of getting out of 122 00:06:22,240 --> 00:06:24,690 a loop or controlling the execution of a loop. 123 00:06:24,690 --> 00:06:28,060 There is a keyword or a part of the Python language called... 124 00:06:29,630 --> 00:06:31,250 What color do I got? 125 00:06:31,250 --> 00:06:34,460 No, green's over here. Called break. 126 00:06:34,460 --> 00:06:38,510 If you look back at the reserved words, break was one of the reserved words. 127 00:06:38,510 --> 00:06:39,450 Break says, 128 00:06:39,450 --> 00:06:42,920 hey, if I'm in a loop, stop the loop. All right? 129 00:06:42,920 --> 00:06:43,740 Get out of this loop. 130 00:06:43,740 --> 00:06:45,550 I'm done with this loop. 131 00:06:45,550 --> 00:06:47,560 And so here's this loop. 132 00:06:47,560 --> 00:06:49,420 Now the interesting thing we've done is I've 133 00:06:49,420 --> 00:06:51,110 just got done talking to you about infinite loops. 134 00:06:51,110 --> 00:06:54,760 We have just constructed an "infinite loop", because the 135 00:06:54,760 --> 00:06:58,180 question is right there, and the answer is yes. 136 00:06:58,180 --> 00:06:58,930 True. 137 00:06:58,930 --> 00:06:59,280 True. 138 00:06:59,280 --> 00:07:01,500 And that's a way to construct an infinite loop. 139 00:07:01,500 --> 00:07:03,140 We've done this because we have a different 140 00:07:03,140 --> 00:07:04,490 way of getting out of the loop, so we've 141 00:07:04,490 --> 00:07:07,060 constructed a loop that, just on the face of it, 142 00:07:07,060 --> 00:07:10,050 just looking at that line, looks like an infinite loop. 143 00:07:10,050 --> 00:07:13,350 So what this loop does is it reads a line of input, 144 00:07:13,350 --> 00:07:17,320 checks to see if the string that we've entered is done. 145 00:07:17,320 --> 00:07:20,740 And if it is, we're going to skip out with this break 146 00:07:20,740 --> 00:07:23,170 and get out of the loop, otherwise we're going to print it. 147 00:07:23,170 --> 00:07:24,950 So at a high level, what this loop is 148 00:07:24,950 --> 00:07:28,510 going to do is prompt for strings of characters 149 00:07:28,510 --> 00:07:30,000 until we enter done. 150 00:07:30,000 --> 00:07:31,140 And that's exactly what it does. 151 00:07:31,140 --> 00:07:33,100 It prompts, we say hello there, it prints that out. 152 00:07:33,100 --> 00:07:35,290 We say, we say finished, it prints that out. 153 00:07:35,290 --> 00:07:37,030 We say done, and it's done. 154 00:07:37,030 --> 00:07:39,700 So it, when we say done, it comes out and 155 00:07:39,700 --> 00:07:42,760 finishes the loop, and, and that's the end of the program. 156 00:07:42,760 --> 00:07:43,540 Okay? 157 00:07:43,540 --> 00:07:46,150 So, to look at this in some more detail. 158 00:07:48,200 --> 00:07:51,780 The first time, it comes in, does the raw input. 159 00:07:51,780 --> 00:07:54,040 Because True is true, so it's going to run it. 160 00:07:54,040 --> 00:07:57,600 And then we enter hello there. 161 00:07:57,600 --> 00:07:59,950 It checks to see if what we entered is equal to the string done. 162 00:07:59,950 --> 00:08:02,480 It is not, so it skips and it does the print. 163 00:08:03,600 --> 00:08:06,590 And we do this one more time, we type finished. 164 00:08:06,590 --> 00:08:10,020 And then the line is not done, that variable 165 00:08:10,020 --> 00:08:11,860 line does not have the value done in it. 166 00:08:11,860 --> 00:08:13,300 So we print that. We come in 167 00:08:13,300 --> 00:08:16,700 one more time, but this time this is true, so it 168 00:08:16,700 --> 00:08:21,180 goes in and executes the break and then it escapes the loop. 169 00:08:21,180 --> 00:08:22,290 And so you can think of... 170 00:08:23,870 --> 00:08:24,240 Right? 171 00:08:24,240 --> 00:08:26,300 Here is the body of this loop, 172 00:08:26,300 --> 00:08:29,700 because that's where the indentation starts and ends. 173 00:08:29,700 --> 00:08:34,730 The break says, break me out of the current loop that I am in 174 00:08:34,730 --> 00:08:38,210 and get to that next line that has the same indent as the while. 175 00:08:38,210 --> 00:08:42,050 So, whatever it is, break says, we are done with this loop. 176 00:08:42,050 --> 00:08:45,310 When that statement executes, we are done with the loop. 177 00:08:45,310 --> 00:08:46,720 We're finished with the loop. 178 00:08:46,720 --> 00:08:50,970 It'll run until that executes because we got this set to be while True. 179 00:08:53,180 --> 00:08:57,160 Okay, so there's a simpler, I mean this is sort of a simple way to draw this. 180 00:08:57,160 --> 00:09:01,100 Break is sort of a jump to the statement immediately following the loop. 181 00:09:02,530 --> 00:09:04,418 If you really want to picture this, I think 182 00:09:04,418 --> 00:09:06,830 of this as a kind of like a Star Trek transporter. 183 00:09:06,830 --> 00:09:11,590 Where you kind of come into break and then [SOUND] your molecules are sent 184 00:09:11,590 --> 00:09:15,515 to the four corners of the universe, and you reassemble outside of the loop. 185 00:09:15,515 --> 00:09:18,590 And so if we look at this in sort of my little road map 186 00:09:18,590 --> 00:09:20,536 version of these things, right? 187 00:09:20,536 --> 00:09:23,640 The while loop is going to run for a while, yada, yada. 188 00:09:23,640 --> 00:09:25,830 There can actually be more than one break 189 00:09:25,830 --> 00:09:29,880 as long as they only get this. But the moment that somehow, some if or 190 00:09:29,880 --> 00:09:36,110 whatever hits the break, then it gets out completely and so it escapes the loop. 191 00:09:36,110 --> 00:09:40,740 And so it sort of like you, you, you're zoom, zoom, 192 00:09:40,740 --> 00:09:43,520 zoom, zoom, zoom, zoom, you come in here and then you are, 193 00:09:43,520 --> 00:09:48,420 you are re-materialized outside the loop. That's what the break does, okay? 194 00:09:48,420 --> 00:09:52,570 So, break is one way to control the execution of loops. 195 00:09:54,530 --> 00:09:57,090 Now, another way to control the execution of 196 00:09:57,090 --> 00:10:00,000 loops, that doesn't actually exit the loop, is called continue. 197 00:10:01,000 --> 00:10:06,630 Continue basically says, hey, I'm done with this iteration of the loop. 198 00:10:06,630 --> 00:10:09,620 Now each time through the loop, we call that an iteration. 199 00:10:09,620 --> 00:10:13,300 Continue says, I don't want to stop the loop, but I 200 00:10:13,300 --> 00:10:17,590 want to stop this iteration and advance to the next iteration. 201 00:10:17,590 --> 00:10:20,100 And so what we have here is we have the same basic loop, 202 00:10:20,100 --> 00:10:23,040 a while True which kind of makes us an infinite loop. 203 00:10:24,570 --> 00:10:27,420 We're going to read a line prompting with a less than sign. 204 00:10:28,800 --> 00:10:30,690 And if it's done, we are going to break, that code is 205 00:10:30,690 --> 00:10:32,990 down here, and we are going to print it if we fall through. 206 00:10:32,990 --> 00:10:34,920 So normally we'll be reading and 207 00:10:34,920 --> 00:10:38,300 printing, and if the line is done, we are going to break out. 208 00:10:38,300 --> 00:10:40,700 That's we just got done doing. But the new part is right here. 209 00:10:41,960 --> 00:10:44,120 And this is, we'll learn this in the next chapter. 210 00:10:44,120 --> 00:10:46,636 If line sub 0, if the first character of 211 00:10:46,636 --> 00:10:50,650 the line, is a pound sign, we're going to continue. 212 00:10:50,650 --> 00:10:54,300 And what continue says is it doesn't actually get us out of the loop. 213 00:10:54,300 --> 00:10:56,230 It jumps back up to the top of the loop. 214 00:10:56,230 --> 00:11:00,060 Which means that it ignores, for that iteration, 215 00:11:00,060 --> 00:11:01,860 the rest of the loop. Right? 216 00:11:01,860 --> 00:11:04,230 So if execution comes in here. 217 00:11:04,230 --> 00:11:05,740 I'm going to clear that. 218 00:11:06,880 --> 00:11:12,810 If execution comes in here and hits this line, it goes back up to the while. 219 00:11:12,810 --> 00:11:13,700 Okay? 220 00:11:13,700 --> 00:11:17,190 Which means it, whatever this is, it's not coming out of this if. 221 00:11:17,190 --> 00:11:19,100 It's going back up to the while. 222 00:11:19,100 --> 00:11:19,710 Okay? 223 00:11:19,710 --> 00:11:22,520 So continue ends the current iteration and jumps to 224 00:11:22,520 --> 00:11:24,970 the top of the loop, and starts the next iteration. 225 00:11:26,300 --> 00:11:28,380 And so if we look at how the code runs, 226 00:11:28,380 --> 00:11:32,300 hello there prints, pound sign with the first character 227 00:11:32,300 --> 00:11:35,200 doesn't print, so there is no printout right here. 228 00:11:35,200 --> 00:11:39,340 Print this is not done. And we enter done, and then the loop ends. 229 00:11:39,340 --> 00:11:41,460 Now another way to sort of draw this is 230 00:11:41,460 --> 00:11:44,440 the continued jumps to the top of the loop. 231 00:11:44,440 --> 00:11:49,600 It, it does run the question, right? It does check the question. 232 00:11:49,600 --> 00:11:51,480 So here is another way to, to draw 233 00:11:51,480 --> 00:11:55,830 that picture. And so here again we have a loop and it's happily running. 234 00:11:55,830 --> 00:11:57,503 And there can be breaks in there and there could 235 00:11:57,503 --> 00:12:00,110 be continues in there, and as long as we don't 236 00:12:00,110 --> 00:12:01,760 hit a break or continue, the loop just sort of 237 00:12:01,760 --> 00:12:04,962 runs and goes up to the top. And at some point, some if, 238 00:12:04,962 --> 00:12:09,020 we hit the continue. And like a transporter, instead of 239 00:12:09,020 --> 00:12:11,320 going out of the loop, we go to the top of the loop. 240 00:12:11,320 --> 00:12:15,600 But it's important that we go and we check the question, right? 241 00:12:15,600 --> 00:12:16,505 So the continue is 242 00:12:16,505 --> 00:12:20,020 not likely to exit the loop unless the question has become false. 243 00:12:20,020 --> 00:12:22,670 So the continue is likely to come up here, 244 00:12:22,670 --> 00:12:25,540 run some more, then hit the continue. It comes up here. 245 00:12:25,540 --> 00:12:26,640 Oops! Oops, it did that backwards. 246 00:12:26,640 --> 00:12:27,830 Run some more. 247 00:12:27,830 --> 00:12:29,470 Clear this out. 248 00:12:29,470 --> 00:12:32,100 So, the continue could run many times, right? 249 00:12:32,100 --> 00:12:33,110 So we have the loop. 250 00:12:33,110 --> 00:12:36,618 Loop runs a bunch of times, then finally we hit the continue. 251 00:12:36,618 --> 00:12:37,900 Continue goes up to the top. 252 00:12:37,900 --> 00:12:40,690 If it's still true, we'll run the loop some more. 253 00:12:40,690 --> 00:12:42,136 Then you might hit the continue. 254 00:12:42,136 --> 00:12:43,606 Then you might go up to the top, come 255 00:12:43,606 --> 00:12:46,930 down, round and round and round and round, hit the continue again. 256 00:12:46,930 --> 00:12:49,240 Go up to the top, yada, yada. 257 00:12:49,240 --> 00:12:52,610 Now in this, in this particular loop, this break eventually 258 00:12:52,610 --> 00:12:55,240 is down here and that's how we get out, okay? 259 00:12:55,240 --> 00:12:58,040 So the continue goes back up to the top of the loop. 260 00:13:00,020 --> 00:13:01,935 So these loops that we construct with the 261 00:13:01,935 --> 00:13:05,340 while keyword are what we call indefinite loops. 262 00:13:05,340 --> 00:13:07,419 I mean, looking at the ones that we've 263 00:13:07,419 --> 00:13:09,630 written, which are two lines or six lines, 264 00:13:09,630 --> 00:13:12,800 we can kind of inspect them and understand when they are going 265 00:13:12,800 --> 00:13:16,780 to stop, and we are going to know that they're possible to stop them. 266 00:13:16,780 --> 00:13:20,130 A loop that won't stop is an infinite loop. 267 00:13:21,500 --> 00:13:24,864 Sometimes these loops can be rather [COUGH] complex and you may not actually be able to 268 00:13:24,864 --> 00:13:29,910 look at them, because there are many lines and, and so we don't know. 269 00:13:29,910 --> 00:13:32,460 And so, so if you, you have to be real careful when you 270 00:13:32,460 --> 00:13:37,200 construct these to make sure that they stop as, as things get more complicated. 271 00:13:37,200 --> 00:13:42,380 Now the cousin to indefinite loops are definite loops. 272 00:13:42,380 --> 00:13:46,710 And definite loops is something where we have a list of things or a set 273 00:13:46,710 --> 00:13:49,900 of things that are kind of a known set of things, a finite set of things. 274 00:13:51,050 --> 00:13:53,270 And we are going to write a loop that's going to go through that set of things 275 00:13:53,270 --> 00:13:57,460 and do something to each thing in that set of things. 276 00:13:57,460 --> 00:13:59,630 And the keyword that we use for this is the for. 277 00:13:59,630 --> 00:14:05,130 So we use the Python for keyword that says we are going to write a loop, but 278 00:14:05,130 --> 00:14:07,640 instead of it just running until some condition 279 00:14:07,640 --> 00:14:09,320 becomes true or false or we hit a break, 280 00:14:10,320 --> 00:14:13,720 we're actually going to know how many times this is going to run. 281 00:14:13,720 --> 00:14:16,640 Now you can actually use break and continue in for loops. 282 00:14:16,640 --> 00:14:19,820 We call these definite loops because the, how long 283 00:14:19,820 --> 00:14:23,120 they're going to run is kind of well known, basically. 284 00:14:23,120 --> 00:14:26,630 So here's a simple definite loop, and it's kind of like that while loop 285 00:14:26,630 --> 00:14:28,340 that we just got done looking at 286 00:14:28,340 --> 00:14:31,400 where it's counting down and then saying blastoff. 287 00:14:31,400 --> 00:14:35,290 And so the way we construct this loop is we have the for keyword, 288 00:14:35,290 --> 00:14:37,580 it's part of Python language, 289 00:14:37,580 --> 00:14:40,950 the in keyword, and then we have an iteration variable. 290 00:14:40,950 --> 00:14:43,700 I've chosen i as my iteration variable. 291 00:14:43,700 --> 00:14:46,590 And basically what we're saying is, dear Python, 292 00:14:48,560 --> 00:14:52,970 run this indented block, and there's only one line in the indented block. 293 00:14:52,970 --> 00:14:56,920 Run it once for each of the values in this little list. 294 00:14:56,920 --> 00:15:00,590 This is a Python list, square brackets make Python lists. 295 00:15:00,590 --> 00:15:02,250 Comma-separated values. 296 00:15:02,250 --> 00:15:05,210 So it says, I would like i to be 5, then run this code. 297 00:15:05,210 --> 00:15:07,230 Then I would like i to be 4, then run this code. 298 00:15:07,230 --> 00:15:09,430 Then I would like i to be 3, then run this code. 299 00:15:09,430 --> 00:15:13,540 i should be 2, then run this code. And i should be 1, then run this code. 300 00:15:13,540 --> 00:15:16,450 And so this is pretty clear, and I like this word in. 301 00:15:17,810 --> 00:15:22,570 It says you know, doop, doop, doop, doop, doop, and then run this each time. 302 00:15:22,570 --> 00:15:27,810 And so out of that comes 5, 4, 3, 2, 1, and then the loop is done. 303 00:15:27,810 --> 00:15:29,920 Python is doing all the tricky bits 304 00:15:29,920 --> 00:15:32,640 here, Python's figuring all these things out for us 305 00:15:34,880 --> 00:15:38,570 and handling all this, and then we're done. And so it's, it's, if you look at it, 306 00:15:38,570 --> 00:15:41,320 we have an iteration variable, but we didn't have to increment it, 307 00:15:41,320 --> 00:15:44,920 we didn't have to do anything. Python took care of a lot of things for us. 308 00:15:44,920 --> 00:15:47,850 And so when we're looping through a known list of things, 309 00:15:47,850 --> 00:15:49,712 or later when we read a file, 310 00:15:49,712 --> 00:15:51,480 we're going to loop through the lines in the file. 311 00:15:51,480 --> 00:15:54,460 And so the for loop is a really nice powerful. 312 00:15:54,460 --> 00:15:58,100 And it's syntactically cleaner, it's really quite nice. 313 00:15:58,100 --> 00:15:59,920 Now it's important to realize that you 314 00:15:59,920 --> 00:16:03,740 don't have to just loop through numbers. I did that one with the set of 315 00:16:03,740 --> 00:16:05,330 descending numbers, so that it was equivalent to the 316 00:16:05,330 --> 00:16:07,680 while loop that I started at the beginning. 317 00:16:07,680 --> 00:16:11,510 But this is a loop where what it's going to loop through is a list. 318 00:16:11,510 --> 00:16:14,970 Closed square brackets are a list in Python. 319 00:16:14,970 --> 00:16:18,990 This is a list of three strings, Joseph, Glenn, and Sally. 320 00:16:18,990 --> 00:16:23,110 They're string constants, and then commas are how we make lists. 321 00:16:24,370 --> 00:16:25,820 And so friends 322 00:16:25,820 --> 00:16:27,300 is a mnemonic variable. 323 00:16:27,300 --> 00:16:29,550 Python doesn't know anything about friends in particular, 324 00:16:29,550 --> 00:16:31,960 but I've chosen this variable name to be friends. 325 00:16:31,960 --> 00:16:35,110 And it's a list of three people, Joseph, Glenn, and Sally. 326 00:16:35,110 --> 00:16:37,900 And so I have an iteration variable called friend. 327 00:16:37,900 --> 00:16:40,150 And I'm going to loop through the set of friends. 328 00:16:40,150 --> 00:16:42,410 Now Python doesn't know anything about singular. 329 00:16:42,410 --> 00:16:44,260 Python doesn't know anything about plural. 330 00:16:44,260 --> 00:16:47,320 I'm just choosing these variable names because it makes a lot of sense. 331 00:16:48,350 --> 00:16:50,894 This is a set of friends. Because it has three of them in it. 332 00:16:50,894 --> 00:16:54,030 And this is a single friend. 333 00:16:54,030 --> 00:16:57,620 What it's really going to do is friend is going to take on the successive 334 00:16:57,620 --> 00:17:00,600 of values Joseph, Glenn, and Sally, and this little block of code 335 00:17:00,600 --> 00:17:05,160 is going to once for each of those three items in the set, and 336 00:17:05,160 --> 00:17:10,200 the variable friend is going to take on the successive values of that set. 337 00:17:10,200 --> 00:17:13,360 So out of this comes three lines of printout. 338 00:17:13,360 --> 00:17:15,980 Happy New Year Joseph, Happy New Year Glenn, Happy New Year Sally. 339 00:17:15,980 --> 00:17:19,940 Of course this is the i bit right down here, but we just 340 00:17:19,940 --> 00:17:23,330 made it so, hey Python, look, how ever many friends they are, run this 341 00:17:23,330 --> 00:17:26,820 code one time for each one, change this variable friend to be each 342 00:17:26,820 --> 00:17:30,400 of the successive ones in order, and then we print that we're done. 343 00:17:30,400 --> 00:17:30,888 Okay? 344 00:17:30,888 --> 00:17:32,860 So the for loop. 345 00:17:32,860 --> 00:17:34,860 Sort of if we go and try to make a picture of the for loop. 346 00:17:34,860 --> 00:17:39,420 The for loop is kind of a powerful thing. It's does, it does two things. 347 00:17:39,420 --> 00:17:41,080 It decides if we're done or not. 348 00:17:42,470 --> 00:17:44,060 Do we keep going in the loop? 349 00:17:44,060 --> 00:17:46,210 Or, well I mean as long as we keep 350 00:17:46,210 --> 00:17:49,280 going, we're going to advance the i value, the iteration variable. 351 00:17:49,280 --> 00:17:52,660 It takes care of the responsibility of changing the iteration variable. 352 00:17:52,660 --> 00:17:57,050 We do not have to add lines of code in that change the iteration variable, okay? 353 00:17:57,050 --> 00:18:00,910 And so if we take a look, you know, we come in. 354 00:18:00,910 --> 00:18:02,280 Are we done? We're not done. 355 00:18:02,280 --> 00:18:04,310 Set i to the right thing, then print it. 356 00:18:04,310 --> 00:18:07,190 Out comes 5. Advance i, 357 00:18:07,190 --> 00:18:09,370 advance i, print it. Advance it. 358 00:18:09,370 --> 00:18:10,170 Print it. Advance it. Print it. 359 00:18:10,170 --> 00:18:12,500 Oh, now we're done. 360 00:18:12,500 --> 00:18:13,350 Right? 361 00:18:13,350 --> 00:18:16,510 i was not the thing that decided when we were done. 362 00:18:16,510 --> 00:18:19,660 The for loop just keeps track internally as i moves 363 00:18:19,660 --> 00:18:22,790 through these things and it goes like oh, I'm all done. 364 00:18:22,790 --> 00:18:25,260 I'll take care of that, you finished. 365 00:18:25,260 --> 00:18:29,150 So it doesn't, there's no if in here, so it's not like like if i equals 1, stop. 366 00:18:29,150 --> 00:18:31,780 No, no, no, it just says you told me to do five things. 367 00:18:31,780 --> 00:18:32,360 I'm going to do five things 368 00:18:32,360 --> 00:18:33,710 and then we're going to stop. 369 00:18:33,710 --> 00:18:40,220 And so again the for loop, the for loop here has got sort of two functions. 370 00:18:40,220 --> 00:18:43,910 It decides how long the loop is going to run, and changes the 371 00:18:43,910 --> 00:18:47,570 iteration variable based on what you've told it to, in this in clause. 372 00:18:48,690 --> 00:18:49,190 Okay? 373 00:18:50,310 --> 00:18:53,580 So I think in is a real elegant construct. 374 00:18:53,580 --> 00:18:58,290 It's just a keyword, but it's sort of, if you think about math, 375 00:18:58,290 --> 00:18:59,680 if you're familiar with sets, 376 00:18:59,680 --> 00:19:02,120 it's like something inside of a set of something. 377 00:19:02,120 --> 00:19:03,980 I think it's a real pretty way to think about it. 378 00:19:05,080 --> 00:19:07,350 And you can kind of think of it a little more abstractly. 379 00:19:07,350 --> 00:19:10,640 That you say, well here's a little indented block of code. 380 00:19:10,640 --> 00:19:11,080 Right? 381 00:19:11,080 --> 00:19:14,990 And I want it to run some number of times for each 382 00:19:14,990 --> 00:19:18,660 of the i values in the set 5, 4, 3, 2, 1. 383 00:19:18,660 --> 00:19:19,950 That's how I kind of think of it. 384 00:19:19,950 --> 00:19:23,170 So I think that this is a real pretty syntax. 385 00:19:23,170 --> 00:19:25,490 Different languages have different looping syntax. 386 00:19:25,490 --> 00:19:28,930 I think this is really a very expressive, very pretty one. 387 00:19:32,720 --> 00:19:33,920 Yeah. 388 00:19:33,920 --> 00:19:36,870 So another way to, so, so, so one way to 389 00:19:36,870 --> 00:19:40,440 think about this picture is that, you know, the for loop 390 00:19:40,440 --> 00:19:42,760 causes sort of repeated execution in that we're 391 00:19:42,760 --> 00:19:45,410 driving in the circle and then we stop, right? 392 00:19:45,410 --> 00:19:47,890 The other way to think about is to, to 393 00:19:47,890 --> 00:19:50,588 not, to think about a little more abstractly, right? 394 00:19:50,588 --> 00:19:53,440 To say, hmm, you know, at the end of the day, all I'm 395 00:19:53,440 --> 00:19:58,130 really telling Python is, I want to execute this block of code five times, 396 00:19:58,130 --> 00:20:02,190 then I want the variable i to change from, to these three values. 397 00:20:02,190 --> 00:20:04,030 So in a way, you could think of this as expanded 398 00:20:04,030 --> 00:20:06,960 is the for loop sets it to 5, then runs your code. 399 00:20:06,960 --> 00:20:09,290 The for loop then sets it to 4, runs your code. 400 00:20:09,290 --> 00:20:11,525 The for loop sets it to 3, runs your code. 401 00:20:11,525 --> 00:20:15,830 For loop sets it to 2, runs your code. Sets it to 1, runs your code. 402 00:20:15,830 --> 00:20:18,740 These two ways of looking at it are the same 403 00:20:19,910 --> 00:20:23,390 from your perspective, because you're just asking Python 404 00:20:23,390 --> 00:20:24,590 to do something. 405 00:20:24,590 --> 00:20:28,030 Whether it does it this way, or whether it does it this way, 406 00:20:28,030 --> 00:20:32,290 you hardly can tell the difference. It's probably going to do it this way. 407 00:20:32,290 --> 00:20:37,150 But logically it's not that different. It's not different from doing it this way. 408 00:20:37,150 --> 00:20:41,440 You're saying, run this block of code, change i in the following way. 409 00:20:44,860 --> 00:20:48,600 Cool. It's like, we don't have to worry. 410 00:20:48,600 --> 00:20:51,640 I mean, we can use, mentally, either model of what's 411 00:20:51,640 --> 00:20:54,520 going on inside, because it doesn't matter, because they're the same. 412 00:20:57,375 --> 00:21:00,330 Okay, so these definite loops are really cool. 413 00:21:00,330 --> 00:21:02,960 Starting in a couple of chapters we'll mostly use definite 414 00:21:02,960 --> 00:21:07,200 loops to go through lists or dictionaries or tuples or files. 415 00:21:08,290 --> 00:21:09,970 And so that's a finite set of things. 416 00:21:09,970 --> 00:21:12,100 It can be a large set of things, but it's a finite set of things.