1 00:00:07,970 --> 00:00:12,270 Suppose we have a line of code or a bunch of lines 2 00:00:12,270 --> 00:00:16,185 of code that we'd like to make run more than once. 3 00:00:16,185 --> 00:00:18,495 You could just write those lines of code 4 00:00:18,495 --> 00:00:21,945 and copy and paste them so that you add them again, 5 00:00:21,945 --> 00:00:26,805 but that would start to give you long programs that were hard to understand. 6 00:00:26,805 --> 00:00:34,730 So, Python gives us a special command that says do some lines of code multiple times. 7 00:00:34,730 --> 00:00:38,790 It's called a for loop or iteration. 8 00:00:38,800 --> 00:00:46,090 We begin with a special word for. 9 00:00:47,060 --> 00:00:50,870 Then we have some variable name or in this case, 10 00:00:50,870 --> 00:00:52,340 since we're not really using it, 11 00:00:52,340 --> 00:00:54,040 we just have underscore, 12 00:00:54,040 --> 00:00:56,760 and we have a special word in. 13 00:00:56,760 --> 00:01:03,610 In this case, we say do it three times, and then we have a colon. 14 00:01:03,670 --> 00:01:09,020 All of the lines of code that are indented farther in, 15 00:01:09,020 --> 00:01:11,334 in this case, by a few spaces, 16 00:01:11,334 --> 00:01:15,055 all of those lines of code are a block, 17 00:01:15,055 --> 00:01:20,925 and that block of code will be executed three times in this case. 18 00:01:20,925 --> 00:01:24,710 It's always a good idea to work on your understanding of 19 00:01:24,710 --> 00:01:29,100 code before you run it to try to predict what it will do. 20 00:01:30,050 --> 00:01:34,615 In this case, you might imagine that it's going to print. 21 00:01:34,615 --> 00:01:37,525 In our output window, this will execute first, 22 00:01:37,525 --> 00:01:39,890 and then it'll say this will execute three times. 23 00:01:39,890 --> 00:01:41,420 This line will execute three times. 24 00:01:41,420 --> 00:01:43,375 This line will execute three times. 25 00:01:43,375 --> 00:01:46,050 Then this line will also execute three times. 26 00:01:46,050 --> 00:01:48,420 This line will also execute three times. 27 00:01:48,420 --> 00:01:51,270 In fact, it doesn't do that. 28 00:01:51,270 --> 00:01:56,485 It doesn't print this line three times and then this one three times. 29 00:01:56,485 --> 00:01:59,680 What it does is it executes line four and then nine 30 00:01:59,680 --> 00:02:03,625 five and then four and then five and then four and then five. 31 00:02:03,625 --> 00:02:05,920 So, the whole block is executed, 32 00:02:05,920 --> 00:02:07,960 and then the whole block is executed again, 33 00:02:07,960 --> 00:02:10,675 and the whole blocks is executed a third time. 34 00:02:10,675 --> 00:02:13,520 Let's see how that works. 35 00:02:15,180 --> 00:02:18,795 So, you can see in our output window, 36 00:02:18,795 --> 00:02:21,695 the whole block executes once, 37 00:02:21,695 --> 00:02:24,350 and the whole block executes a second time, 38 00:02:24,350 --> 00:02:27,385 and the whole block executes a third time. 39 00:02:27,385 --> 00:02:31,125 Let's go look at the next example. 40 00:02:31,125 --> 00:02:33,260 In this next example, 41 00:02:33,260 --> 00:02:35,090 we're going to be able to draw 42 00:02:35,090 --> 00:02:38,270 a more interesting turtle program by 43 00:02:38,270 --> 00:02:42,185 having a set of commands get executed a bunch of times. 44 00:02:42,185 --> 00:02:45,850 In this case, we're creating a turtle named elan. 45 00:02:45,850 --> 00:02:49,470 We're going to make elan do some commands 10 times. 46 00:02:49,470 --> 00:02:52,725 For underscore in range of 10, 47 00:02:52,725 --> 00:02:55,095 do this block from lines eight through 10. 48 00:02:55,095 --> 00:02:57,090 Do that 10 times. 49 00:02:57,090 --> 00:03:02,290 So, elan is going to go forward by 50 and then turn right 90. 50 00:03:02,690 --> 00:03:05,580 Remember, from the previous chapter, 51 00:03:05,580 --> 00:03:08,435 what distance equals distance plus 10 does. 52 00:03:08,435 --> 00:03:11,470 We had the variable distance, 53 00:03:11,930 --> 00:03:16,210 and it was initially bound to the value 50. 54 00:03:16,210 --> 00:03:20,205 We look up 50 plus 10, 55 00:03:20,205 --> 00:03:24,405 that equals 60, and we replace the 50 with 60. 56 00:03:24,405 --> 00:03:28,660 The next time that we execute line 10, 57 00:03:28,660 --> 00:03:30,815 we're going to replace it with 70. 58 00:03:30,815 --> 00:03:36,845 You can see that that distance variable is used here to determine how far elan will move. 59 00:03:36,845 --> 00:03:39,705 So, each time elan moves forward, 60 00:03:39,705 --> 00:03:42,210 he's going to go forward a little bit farther, 61 00:03:42,210 --> 00:03:43,740 50 pixels the first time, 62 00:03:43,740 --> 00:03:45,645 then 60, then 70. 63 00:03:45,645 --> 00:03:47,595 So, before I run this, 64 00:03:47,595 --> 00:03:50,560 you might want to pause the recording and see if you can envision in 65 00:03:50,560 --> 00:03:55,820 your mind what is this going to look like when I run it. 66 00:03:56,520 --> 00:03:59,600 Here we go. Let's see. 67 00:04:00,000 --> 00:04:03,575 So, it's getting a little bit longer each time. 68 00:04:03,575 --> 00:04:06,625 That means instead of just making a square, 69 00:04:06,625 --> 00:04:09,080 it ends up making a spiral. 70 00:04:12,770 --> 00:04:16,045 So, how many times does it do this block? 71 00:04:16,045 --> 00:04:19,970 It does it 10 times because it says range of 10 there. 72 00:04:20,120 --> 00:04:25,520 Now, what if I made the angle increase every time, too? 73 00:04:25,520 --> 00:04:30,650 Suppose instead of turning right by 90 degrees, 74 00:04:30,650 --> 00:04:34,450 I turned right by whatever the current value of the angle was, 75 00:04:34,450 --> 00:04:37,460 and it started at 90. 76 00:04:37,490 --> 00:04:42,385 If I do this, I get just exactly the same picture that I have right now. 77 00:04:42,385 --> 00:04:46,750 But suppose that I change the angle every time. 78 00:04:46,750 --> 00:04:51,000 Maybe I'll make it a little bit smaller. 79 00:04:52,690 --> 00:04:56,390 So, again, you might want to pause the video and see if you can 80 00:04:56,390 --> 00:05:01,830 predict what this is going to look like. Here we go. 81 00:05:04,280 --> 00:05:08,660 So, the angle is getting a little smaller each time. 82 00:05:08,660 --> 00:05:13,680 So, we end up with this even more interesting spiral. 83 00:05:14,150 --> 00:05:18,060 Now, if I wanted the spiral to go even farther, 84 00:05:18,060 --> 00:05:21,130 I could increase this range. 85 00:05:30,950 --> 00:05:36,160 This means it's going to go forward and turn more times. 86 00:05:38,180 --> 00:05:42,340 He's a little off the screen there, and now he's back. 87 00:05:44,060 --> 00:05:47,200 Maybe he's gone forever now. 88 00:05:47,240 --> 00:05:50,580 So, that's making the turtle. 89 00:05:50,580 --> 00:05:55,100 Do a set of commands a bunch of times so that we can have relatively short program, 90 00:05:55,100 --> 00:05:58,680 but have it do a lot of drawing on the screen.