1 00:00:07,910 --> 00:00:11,895 Welcome back. So, we've already covered sequences, 2 00:00:11,895 --> 00:00:14,625 and in this lesson, we're going to cover iteration. 3 00:00:14,625 --> 00:00:18,030 Iteration runs a piece of code for every item in a sequence, 4 00:00:18,030 --> 00:00:20,580 be it a string, list or a tuple. 5 00:00:20,580 --> 00:00:22,125 In order to do iteration, 6 00:00:22,125 --> 00:00:23,580 we're going to use a for loop, 7 00:00:23,580 --> 00:00:27,945 which is the first of many control structures that we're going to see in this course. 8 00:00:27,945 --> 00:00:30,855 So, let's look at a for loop in action. 9 00:00:30,855 --> 00:00:34,680 In order to create a for loop that's going to iterate over a sequence, 10 00:00:34,680 --> 00:00:37,155 we start out by saying the keyword for, 11 00:00:37,155 --> 00:00:39,975 which specifies that we want to create a for loop. 12 00:00:39,975 --> 00:00:46,540 After the for, then we create something called a loop variable. 13 00:00:50,330 --> 00:00:54,560 We could call our loop variable any valid variable name, 14 00:00:54,560 --> 00:00:56,755 but in the case of this for loop, 15 00:00:56,755 --> 00:00:59,535 we're calling our loop variable name. 16 00:00:59,535 --> 00:01:02,175 After we declare a loop variable, 17 00:01:02,175 --> 00:01:05,860 then we say in, and after that, 18 00:01:05,860 --> 00:01:09,155 we specify what sequence we're actually iterating over. 19 00:01:09,155 --> 00:01:12,965 So, in this case, we're iterating over this list, 20 00:01:12,965 --> 00:01:14,840 which has one, two, three, 21 00:01:14,840 --> 00:01:17,555 four, five, six, seven items. 22 00:01:17,555 --> 00:01:21,140 What this for loop is going to do is it's first going to 23 00:01:21,140 --> 00:01:24,380 assign name to be the first item in the sequence, so, 24 00:01:24,380 --> 00:01:27,650 Joe, and then it's going to assign name to be the second item, 25 00:01:27,650 --> 00:01:30,950 and then it's going to assign it to be the third item and so on. 26 00:01:30,950 --> 00:01:35,255 After it assigns it to be any particular item in this sequence, 27 00:01:35,255 --> 00:01:39,565 then we're going to run whatever is inside of the for loop. 28 00:01:39,565 --> 00:01:45,380 In this case, the only code that we have inside of this for loop is this print statement, 29 00:01:45,380 --> 00:01:48,860 which prints out "Hi" and then whatever name 30 00:01:48,860 --> 00:01:53,330 our loop variable's value is "Please come to my party on Saturday". 31 00:01:53,330 --> 00:01:56,170 So, when we run this code, 32 00:01:56,170 --> 00:01:58,965 what we get is "Hi Joe, 33 00:01:58,965 --> 00:02:01,815 please come to my party on Saturday" and then, "Hi Amy, 34 00:02:01,815 --> 00:02:03,630 please come to my party on Saturday", 35 00:02:03,630 --> 00:02:07,650 "Hi Brad, please come to my party on Saturday", and so on. 36 00:02:07,650 --> 00:02:13,070 So, in general, what's happening here is that Python is running this code, 37 00:02:13,070 --> 00:02:18,610 this print statement for every single item in this sequence, 38 00:02:18,610 --> 00:02:21,025 and for every single item in the sequence, 39 00:02:21,025 --> 00:02:23,780 it's its first assigning name to be that item, 40 00:02:23,780 --> 00:02:27,275 and then we're printing out "Hi" and then whatever that name is, 41 00:02:27,275 --> 00:02:29,450 "Please come to my party on Saturday". 42 00:02:29,450 --> 00:02:32,600 So, for all seven items in this list, 43 00:02:32,600 --> 00:02:36,450 then we get seven print statements. 44 00:02:36,640 --> 00:02:40,855 So, let's look at the same code inside of CodeLens. 45 00:02:40,855 --> 00:02:48,780 So again, we're iterating over this list of names and our loop variable, is called name. 46 00:02:48,780 --> 00:02:52,375 So, the first time that we run this code, 47 00:02:52,375 --> 00:02:55,865 you'll see that name gets assigned to the first item in our list. 48 00:02:55,865 --> 00:02:58,915 In this case, name gets assigned to Joe. 49 00:02:58,915 --> 00:03:02,630 Now, when we run this print statement to print out "Hi name, 50 00:03:02,630 --> 00:03:04,550 please come to my party on Saturday", 51 00:03:04,550 --> 00:03:07,055 then we print out, "Hi Joe, 52 00:03:07,055 --> 00:03:09,860 please come to my party on Saturday. 53 00:03:09,860 --> 00:03:14,975 Then next, name gets reassigned to the second item in our list. 54 00:03:14,975 --> 00:03:17,930 So, in our case, it gets assigned to Amy, 55 00:03:17,930 --> 00:03:20,390 and now when we print out "Hi name, 56 00:03:20,390 --> 00:03:22,190 please come to my party on Saturday", 57 00:03:22,190 --> 00:03:23,735 we get "Hi Amy, 58 00:03:23,735 --> 00:03:26,150 please come to my party on Saturday". 59 00:03:26,150 --> 00:03:30,770 Then name gets reassigned to the third item or Brad. 60 00:03:30,770 --> 00:03:32,720 So now, when we print out "Hi name, 61 00:03:32,720 --> 00:03:34,460 please come to my party on Saturday", 62 00:03:34,460 --> 00:03:35,600 we get "Hi Brad, 63 00:03:35,600 --> 00:03:37,640 please come to my party on Saturday". 64 00:03:37,640 --> 00:03:43,880 This pattern repeats for every single item in the sequence that we're iterating over, 65 00:03:43,880 --> 00:03:45,920 until we are done iterating over all of 66 00:03:45,920 --> 00:03:49,910 the items in which case our program is terminated. 67 00:03:49,910 --> 00:03:52,450 So, when we create a for loop, 68 00:03:52,450 --> 00:03:56,495 let's say abstractly, it looks like something on the left hand side here. 69 00:03:56,495 --> 00:03:59,975 So, we have some code that might come before, 70 00:03:59,975 --> 00:04:02,240 we have some code that might come after, 71 00:04:02,240 --> 00:04:04,970 and then we have our actual for loop. 72 00:04:04,970 --> 00:04:08,695 We declare our for loop again by saying, "for", 73 00:04:08,695 --> 00:04:11,580 and then our loop variable, 74 00:04:14,900 --> 00:04:22,090 and then we say "in" and then we say whatever sequence we're iterating over. 75 00:04:26,000 --> 00:04:28,890 Then inside of the for loop, 76 00:04:28,890 --> 00:04:31,010 we have what's called a code block. 77 00:04:31,010 --> 00:04:35,060 So, here the code block is represented in green. 78 00:04:35,060 --> 00:04:39,500 The way that we say that this loop code block is inside of the for loop 79 00:04:39,500 --> 00:04:44,165 as opposed to outside of the for loop is through either tabs or spaces. 80 00:04:44,165 --> 00:04:47,360 So, we have to indent the loop code block to 81 00:04:47,360 --> 00:04:51,820 indicate that this code belongs to this leaf. 82 00:04:51,820 --> 00:04:55,240 So, when we declare a for loop like this, 83 00:04:55,240 --> 00:04:59,030 then this is equivalent to first designing the loop variable, 84 00:04:59,030 --> 00:05:03,010 in our case x, to the first item in the sequence, 85 00:05:03,010 --> 00:05:06,035 and then running the loop code block. 86 00:05:06,035 --> 00:05:11,725 Then assigning our loop variable x to the second item in our sequence, 87 00:05:11,725 --> 00:05:16,230 and then running the loop code block and so on and so forth. 88 00:05:16,230 --> 00:05:19,410 So, assigning x to the third item, the fourth item, 89 00:05:19,410 --> 00:05:25,685 and so on until we finally assign our loop variable x to the last item, 90 00:05:25,685 --> 00:05:29,370 and running the loop code block. 91 00:05:29,450 --> 00:05:32,630 So, one thing to note here is that 92 00:05:32,630 --> 00:05:36,320 the loop code block may or may not reference our loop variable, 93 00:05:36,320 --> 00:05:38,800 but if it does reference our loop variable, 94 00:05:38,800 --> 00:05:42,035 then that reference is going to change every single time. 95 00:05:42,035 --> 00:05:44,920 So, for example, in our previous code, 96 00:05:44,920 --> 00:05:49,970 we referenced our loop variable name inside of the code block in our loop. 97 00:05:49,970 --> 00:05:55,990 So, that means that name is going to change for every single item in our list. 98 00:05:55,990 --> 00:05:58,885 So, in addition to iterating over lists, 99 00:05:58,885 --> 00:06:01,915 for loops allow you to iterate over any sequence, 100 00:06:01,915 --> 00:06:04,720 including strings, lists and tuples. 101 00:06:04,720 --> 00:06:08,290 So, let's go over an example of iterating over a string. 102 00:06:08,290 --> 00:06:13,185 So, here we're iterating over the string "Go Spot Go", 103 00:06:13,185 --> 00:06:17,040 and we're calling our loop variable achar. 104 00:06:17,040 --> 00:06:22,040 So, what that means is that the first time this loop body runs, 105 00:06:22,040 --> 00:06:26,800 then achar is going to be assigned to the character capital G, 106 00:06:26,800 --> 00:06:31,150 the second time that it runs it's going to be assigned to the character lowercase o, 107 00:06:31,150 --> 00:06:34,990 the third time that it runs it's going to be assigned to a space character, 108 00:06:34,990 --> 00:06:37,935 and then capital S, and so on. 109 00:06:37,935 --> 00:06:41,735 So, what that means is that when we run our code, 110 00:06:41,735 --> 00:06:47,135 then when we're first going to print capital G, and then lowercase o, 111 00:06:47,135 --> 00:06:51,670 and space and so on for every character in the string, 112 00:06:51,670 --> 00:06:55,370 and because print prints out characters on a separate line, 113 00:06:55,370 --> 00:06:59,630 then we get Go Spot go printed out vertically. 114 00:06:59,630 --> 00:07:02,810 So, let's go over a multiple choice question. 115 00:07:02,810 --> 00:07:07,285 First, we assign the string S to be python rocks, 116 00:07:07,285 --> 00:07:12,650 and then we say for every character in s. So, 117 00:07:12,650 --> 00:07:16,280 again here, our loop variable is ch, 118 00:07:16,280 --> 00:07:19,630 and the thing that we're iterating over is s, 119 00:07:19,630 --> 00:07:25,790 and our loop body consists of one print statement that prints out the string "HELLO". 120 00:07:25,790 --> 00:07:31,070 The question is how many times is the word "HELLO" printed by the following statements? 121 00:07:31,070 --> 00:07:33,480 So, one thing that we know is 122 00:07:33,480 --> 00:07:36,260 that ch is going to be assigned to the first character in s, 123 00:07:36,260 --> 00:07:37,310 then the second character, 124 00:07:37,310 --> 00:07:39,215 then third character and so on. 125 00:07:39,215 --> 00:07:43,550 What that means is that this loop body is going to run 126 00:07:43,550 --> 00:07:48,640 once for every character in the string s. So in other words, 127 00:07:48,640 --> 00:07:53,765 this loop body is going to run however many times s is long. 128 00:07:53,765 --> 00:07:55,970 So, if s were one-character, 129 00:07:55,970 --> 00:07:57,635 then it would run one time, 130 00:07:57,635 --> 00:08:00,545 if s for two characters it would run two times. 131 00:08:00,545 --> 00:08:04,940 So, in order to find out how many times the word "HELLO" is printed out in this code, 132 00:08:04,940 --> 00:08:07,340 I'm going to find out what's the length of s, 133 00:08:07,340 --> 00:08:10,265 and I find out that it's one, two, three, four, 134 00:08:10,265 --> 00:08:14,835 five, six, seven, eight, nine, 10, 11, 135 00:08:14,835 --> 00:08:21,000 12 characters long, which means that I should expect this code to run 12 times. 136 00:08:22,870 --> 00:08:26,360 So, next we're asked how many 137 00:08:26,360 --> 00:08:29,765 times is the word HELLO printed out by the following statement? 138 00:08:29,765 --> 00:08:32,740 So, here we're assigning S to the same value, 139 00:08:32,740 --> 00:08:35,840 and we have the same loop variable ch. 140 00:08:35,840 --> 00:08:39,770 Now, rather than iterating over the entirety of s, 141 00:08:39,770 --> 00:08:43,130 we're iterating over a slice of s. Specifically, 142 00:08:43,130 --> 00:08:47,315 we're iterating over s from indices three to eight. 143 00:08:47,315 --> 00:08:53,145 So, what that means is that we're iterating over s from zero, one, two, 144 00:08:53,145 --> 00:08:57,135 three from here to four, 145 00:08:57,135 --> 00:09:01,455 five, six, seven, eight to here. 146 00:09:01,455 --> 00:09:03,780 So, we should expect this to run one, 147 00:09:03,780 --> 00:09:07,600 two, three, four, five times. 148 00:09:07,640 --> 00:09:15,650 So, we can see that because a five character sequence is returned by the slice, 149 00:09:15,650 --> 00:09:19,740 then we run our for loop five times. 150 00:09:20,350 --> 00:09:23,345 So, in this code example, 151 00:09:23,345 --> 00:09:25,850 we create a list that has four items, 152 00:09:25,850 --> 00:09:28,625 and we have a loop variable named afruit, 153 00:09:28,625 --> 00:09:31,550 and we iterate over our for item list. 154 00:09:31,550 --> 00:09:35,495 Now, when we print out the value of our loop variable afruit, 155 00:09:35,495 --> 00:09:40,620 then we should expect to get every item of our list printed out on a separate line. 156 00:09:43,480 --> 00:09:49,195 For loops also work even if we don't care about the sequence that we're iterating over. 157 00:09:49,195 --> 00:09:52,280 So, here we have an example that uses turtle, 158 00:09:52,280 --> 00:09:56,575 and let's suppose that we want to draw a square with our turtle, 159 00:09:56,575 --> 00:09:58,360 one way to draw a square, 160 00:09:58,360 --> 00:10:02,820 is by going forward and then left four different times. 161 00:10:02,820 --> 00:10:05,475 So, we first set up our turtle, 162 00:10:05,475 --> 00:10:07,665 and we call it turtle alex, 163 00:10:07,665 --> 00:10:12,040 and then we create a for loop and we have our loop variable i 164 00:10:12,040 --> 00:10:16,740 and we have the sequence that we're iterating over which is a four items list, 165 00:10:16,740 --> 00:10:19,745 but here inside of our for loop, 166 00:10:19,745 --> 00:10:22,885 we're not referencing our loop variable at all. 167 00:10:22,885 --> 00:10:27,340 Instead, we're taking advantage of the fact that we're repeating this code four 168 00:10:27,340 --> 00:10:33,830 times in order to form a square by going left and forward four times in a row. 169 00:10:36,020 --> 00:10:39,550 It doesn't matter what we're actually repeating over 170 00:10:39,550 --> 00:10:42,475 if we're not referencing the loop variable. 171 00:10:42,475 --> 00:10:45,085 So, for example, here we're looping over 172 00:10:45,085 --> 00:10:47,830 a list of strings that represent different colors, 173 00:10:47,830 --> 00:10:49,824 but we're still not referencing 174 00:10:49,824 --> 00:10:54,790 our loop variable aColor in any of the code inside of our for loop. 175 00:10:54,790 --> 00:10:56,635 So, when we run our code, 176 00:10:56,635 --> 00:10:58,525 then we still get the same result, 177 00:10:58,525 --> 00:11:00,775 which is a square with four sides, 178 00:11:00,775 --> 00:11:02,810 and every side is still black. 179 00:11:02,810 --> 00:11:05,800 If we did want to reference our loop variable, 180 00:11:05,800 --> 00:11:08,050 then we might want to say something like this. 181 00:11:08,050 --> 00:11:09,670 So, here we're referencing 182 00:11:09,670 --> 00:11:14,395 our loop variable aColor by assigning that color to our turtle alex. 183 00:11:14,395 --> 00:11:17,075 So, first aColor is assigned to yellow, 184 00:11:17,075 --> 00:11:19,455 then red then purple then blue. 185 00:11:19,455 --> 00:11:23,450 So, the result here is that every side of our square 186 00:11:23,450 --> 00:11:27,500 ends up being a different color because we first go with yellow, 187 00:11:27,500 --> 00:11:31,285 then red, then purple, then blue. 188 00:11:31,285 --> 00:11:35,180 So, let's answer just a few more questions on for loops. 189 00:11:35,180 --> 00:11:38,955 So, here we have a list that has one, 190 00:11:38,955 --> 00:11:41,370 two, three, four, five, six, 191 00:11:41,370 --> 00:11:43,315 seven, eight, nine items, 192 00:11:43,315 --> 00:11:45,545 and we're asked how many times will the for loop 193 00:11:45,545 --> 00:11:47,885 iterate through the following statements? 194 00:11:47,885 --> 00:11:52,070 So, because ch gets assigned to all nine items once, 195 00:11:52,070 --> 00:11:55,415 then we should expect this to execute nine times. 196 00:11:55,415 --> 00:11:57,200 So, this question asks, 197 00:11:57,200 --> 00:12:00,865 how does Python know what statements are contained in a loop body. 198 00:12:00,865 --> 00:12:03,860 So, Python always strikes the indentation of 199 00:12:03,860 --> 00:12:07,805 statements to check to see if it's in the loop body. 200 00:12:07,805 --> 00:12:12,245 So, we can have more than one line in the loop body 201 00:12:12,245 --> 00:12:16,050 even though many of the examples that we went over only had one line, 202 00:12:16,050 --> 00:12:19,145 and the way that Python knows if something is in the loop body 203 00:12:19,145 --> 00:12:22,910 is if it's indented the same after we declare the for loop. 204 00:12:22,910 --> 00:12:26,070 That's all for now, until next time.