1 00:00:00,980 --> 00:00:02,770 - [Instructor] In this video, we're going to take a look 2 00:00:02,770 --> 00:00:05,619 at the for iteration statement in Python, 3 00:00:05,619 --> 00:00:10,619 which is meant to iterate through a collection of items 4 00:00:10,670 --> 00:00:13,320 or what we would refer to, in many cases, 5 00:00:13,320 --> 00:00:15,940 as simply a sequence of items. 6 00:00:15,940 --> 00:00:19,720 So, for example, a string in Python is simply 7 00:00:19,720 --> 00:00:21,890 a sequence of characters. 8 00:00:21,890 --> 00:00:25,690 So one of the things we can do is use the for statement 9 00:00:25,690 --> 00:00:29,970 to iterate through each individual character in a string 10 00:00:29,970 --> 00:00:32,150 and do something with that character. 11 00:00:32,150 --> 00:00:34,700 So let's say we want to declare a variable 12 00:00:34,700 --> 00:00:37,093 called character that we'll use in the for loop, 13 00:00:37,093 --> 00:00:41,160 so we're basically saying, for each character In, 14 00:00:41,160 --> 00:00:42,810 and then we give it some string 15 00:00:42,810 --> 00:00:45,560 that we'd like to process the characters in. 16 00:00:45,560 --> 00:00:49,620 Let's do Programming just for demonstration purposes. 17 00:00:49,620 --> 00:00:54,020 So for each individual character in the string programming, 18 00:00:54,020 --> 00:00:55,470 we would like to do something, 19 00:00:55,470 --> 00:00:58,427 and again, we introduce the suite with a colon. 20 00:00:58,427 --> 00:01:01,530 Indented in the for statement now, 21 00:01:01,530 --> 00:01:04,490 let's say we simply want to display the characters. 22 00:01:04,490 --> 00:01:06,610 So we'll do a little print statement, 23 00:01:06,610 --> 00:01:10,750 we'll display each character as it is picked off 24 00:01:10,750 --> 00:01:13,330 from the string programming, if you will. 25 00:01:13,330 --> 00:01:16,140 Let's introduce another feature here. 26 00:01:16,140 --> 00:01:19,540 We're going to say we want each character 27 00:01:19,540 --> 00:01:23,690 to be separated from the next by two spaces. 28 00:01:23,690 --> 00:01:26,700 So we've provided a second argument here, 29 00:01:26,700 --> 00:01:29,560 which is called the end keyword argument, 30 00:01:29,560 --> 00:01:31,760 and I'll talk more about that in a few moments. 31 00:01:31,760 --> 00:01:34,525 But let's first go ahead and execute 32 00:01:34,525 --> 00:01:36,730 this little for statement. 33 00:01:36,730 --> 00:01:38,060 So as you can see here, 34 00:01:38,060 --> 00:01:41,735 each individual character is separated from the next 35 00:01:41,735 --> 00:01:45,170 by two spaces, in this case. 36 00:01:45,170 --> 00:01:48,640 Now, the way the for statement works is as follows. 37 00:01:48,640 --> 00:01:50,900 This variable name that you define 38 00:01:50,900 --> 00:01:54,000 between the for and In keywords 39 00:01:54,000 --> 00:01:57,340 is known as the target variable. 40 00:01:57,340 --> 00:01:59,730 During each iteration of the loop, 41 00:01:59,730 --> 00:02:04,730 one item from the sequence to the right of the In keyword 42 00:02:04,830 --> 00:02:09,210 will be automatically assigned to the target variable, 43 00:02:09,210 --> 00:02:13,700 then you can use that value in the body of the loop. 44 00:02:13,700 --> 00:02:17,010 So first, when I enter this for loop 45 00:02:17,010 --> 00:02:18,600 and start executing it, 46 00:02:18,600 --> 00:02:22,400 the letter P is going to be assigned a character. 47 00:02:22,400 --> 00:02:24,100 Then the body will execute, 48 00:02:24,100 --> 00:02:27,720 and I will display character, followed by two spaces. 49 00:02:27,720 --> 00:02:29,180 Then it will come back up 50 00:02:29,180 --> 00:02:31,310 into the first line of the for statement, 51 00:02:31,310 --> 00:02:34,670 and the letter R in Programming will be assigned 52 00:02:34,670 --> 00:02:36,530 to the target variable character, 53 00:02:36,530 --> 00:02:39,960 then I can display R, and this will continue 54 00:02:39,960 --> 00:02:43,090 for each individual value within the sequence 55 00:02:43,090 --> 00:02:46,520 until, eventually, we finish processing the sequence, 56 00:02:46,520 --> 00:02:48,410 which is, of course, the letter G 57 00:02:48,410 --> 00:02:51,020 when we come back after executing the body. 58 00:02:51,020 --> 00:02:53,730 In that case, the for loop will recognize 59 00:02:53,730 --> 00:02:56,080 that it has reached the end of the sequence 60 00:02:56,080 --> 00:02:59,343 and will automatically terminate at that point. 61 00:03:00,470 --> 00:03:02,080 Now, let's talk a little bit more 62 00:03:02,080 --> 00:03:05,245 about this second argument in the print statement. 63 00:03:05,245 --> 00:03:10,245 By default, the end keyword argument, as it's known, 64 00:03:10,622 --> 00:03:13,980 has the value of a new line character. 65 00:03:13,980 --> 00:03:15,980 So you've seen many times 66 00:03:15,980 --> 00:03:19,460 that when we went ahead and printed values 67 00:03:19,460 --> 00:03:21,350 previously with the print statement, 68 00:03:21,350 --> 00:03:25,640 that the cursor automatically dropped to the next line, 69 00:03:25,640 --> 00:03:28,220 and then, if we did another print statement, 70 00:03:28,220 --> 00:03:32,110 more output would happen on the next line at that point. 71 00:03:32,110 --> 00:03:35,850 If you don't want the automatic new line character, 72 00:03:35,850 --> 00:03:38,080 you can supply this second argument 73 00:03:38,080 --> 00:03:42,460 to the print function, and of course, 74 00:03:42,460 --> 00:03:45,900 you could have a separated list of all sorts of other things 75 00:03:45,900 --> 00:03:48,055 before this keyword argument as well. 76 00:03:48,055 --> 00:03:50,170 But basically, what happens is 77 00:03:50,170 --> 00:03:53,270 if the end keyword argument is present, 78 00:03:53,270 --> 00:03:56,870 Python will take the value that you supply as a string, 79 00:03:56,870 --> 00:04:00,013 and it will automatically use that as the separator 80 00:04:00,013 --> 00:04:02,760 between the elements being displayed. 81 00:04:02,760 --> 00:04:04,590 So for example, if, for some reason, 82 00:04:04,590 --> 00:04:08,030 you wanted to separate each of these characters by a dash, 83 00:04:08,030 --> 00:04:10,910 we could have, instead of using two spaces here, 84 00:04:10,910 --> 00:04:13,140 used a dash character instead, 85 00:04:13,140 --> 00:04:15,200 so you get a lot of flexibility 86 00:04:15,200 --> 00:04:17,643 with that end keyword argument. 87 00:04:18,660 --> 00:04:21,679 Now, there's also another keyword argument 88 00:04:21,679 --> 00:04:25,590 where if what you want to do is change 89 00:04:25,590 --> 00:04:28,670 how the output values are being separated, 90 00:04:28,670 --> 00:04:31,069 you can use the sep keyword argument, 91 00:04:31,069 --> 00:04:34,040 so let me go ahead and then do a demonstration of that. 92 00:04:34,040 --> 00:04:35,930 So let's say we want to display 93 00:04:35,930 --> 00:04:39,520 the values 10, 20, and 30. 94 00:04:41,570 --> 00:04:44,010 Now, if I were to stop right there and execute that, 95 00:04:44,010 --> 00:04:47,700 which I'm not going to do yet, recall what will happen here 96 00:04:47,700 --> 00:04:51,290 is that it will output the values 10, 20, and 30, 97 00:04:51,290 --> 00:04:54,870 each separated from the previous one by a space, 98 00:04:54,870 --> 00:04:57,920 then it will output a new line character 99 00:04:57,920 --> 00:05:00,650 to drop the output cursor to the next line 100 00:05:00,650 --> 00:05:03,730 so that you can continue doing more output. 101 00:05:03,730 --> 00:05:05,880 But what if you don't want a space 102 00:05:05,880 --> 00:05:09,100 as a result of displaying multiple items 103 00:05:09,100 --> 00:05:12,350 in a single print statement, but rather, 104 00:05:12,350 --> 00:05:14,700 maybe you want a comma and a space, 105 00:05:14,700 --> 00:05:18,310 or you want some other delimiter between the values? 106 00:05:18,310 --> 00:05:20,290 In that case, you can go ahead 107 00:05:20,290 --> 00:05:22,530 and use the sep keyword argument, 108 00:05:22,530 --> 00:05:25,660 which defines a separator string, and let's say 109 00:05:25,660 --> 00:05:28,810 we wanna use a comma and a space, in this case. 110 00:05:28,810 --> 00:05:31,130 So let me go ahead and execute that, 111 00:05:31,130 --> 00:05:34,000 and you can see that each value is separated 112 00:05:34,000 --> 00:05:37,110 from the next by comma and a space, 113 00:05:37,110 --> 00:05:40,430 and then, once the last value is displayed, 114 00:05:40,430 --> 00:05:42,890 10, 20, and 30 are the values to display, 115 00:05:42,890 --> 00:05:45,110 the cursor drops to the next line. 116 00:05:45,110 --> 00:05:46,530 Now, you may notice, by the way, 117 00:05:46,530 --> 00:05:50,650 that up above here, when we executed snippet one 118 00:05:50,650 --> 00:05:53,320 and displayed Programming with all the characters 119 00:05:53,320 --> 00:05:55,831 separated from each other by two spaces, 120 00:05:55,831 --> 00:05:59,350 the print statement did not automatically 121 00:05:59,350 --> 00:06:01,380 output a new line character, 122 00:06:01,380 --> 00:06:04,418 but the IPython interpreter's In prompt 123 00:06:04,418 --> 00:06:07,290 actually has a built-in new line character 124 00:06:07,290 --> 00:06:09,260 in front of it, which is why the In prompt 125 00:06:09,260 --> 00:06:11,610 always shows up on a new line 126 00:06:11,610 --> 00:06:15,650 every time the next In prompt is displayed. 127 00:06:15,650 --> 00:06:19,020 In this case, when we displayed 10, 20, and 30, 128 00:06:19,020 --> 00:06:21,650 because we didn't use the end keyword argument, 129 00:06:21,650 --> 00:06:24,680 we still get the default new line character, 130 00:06:24,680 --> 00:06:28,090 and therefore, the cursor, after displaying 30, 131 00:06:28,090 --> 00:06:29,530 gets moved to the next line, 132 00:06:29,530 --> 00:06:31,810 and then, the input prompt has another 133 00:06:31,810 --> 00:06:33,930 built-in new line character in it, 134 00:06:33,930 --> 00:06:37,443 which is why we get this blank line here in the output. 135 00:06:39,030 --> 00:06:41,594 Now, let's talk a little bit more 136 00:06:41,594 --> 00:06:44,490 about the for statement. 137 00:06:44,490 --> 00:06:46,570 So as we mentioned up above, 138 00:06:46,570 --> 00:06:49,950 the for statement is used to process 139 00:06:49,950 --> 00:06:52,930 each item in a sequence of items. 140 00:06:52,930 --> 00:06:55,370 Now, one of the most common sequences 141 00:06:55,370 --> 00:07:00,060 that you're going to use in Python is known as a list. 142 00:07:00,060 --> 00:07:02,470 So let's do a demonstration with a list 143 00:07:02,470 --> 00:07:04,990 so we can introduce the concept to you. 144 00:07:04,990 --> 00:07:08,170 So let's say that we want to total up 145 00:07:08,170 --> 00:07:11,380 the integers in a list of integers. 146 00:07:11,380 --> 00:07:14,670 To do that, we might start by declaring a variable total 147 00:07:14,670 --> 00:07:16,510 and initializing it to zero, 148 00:07:16,510 --> 00:07:20,730 and then, adding each item in the list to the total, 149 00:07:20,730 --> 00:07:23,750 so let's write a for statement that will help us do that. 150 00:07:23,750 --> 00:07:27,730 So for each number in a list of items, 151 00:07:27,730 --> 00:07:31,160 a list in Python is defined in square brackets, 152 00:07:31,160 --> 00:07:33,350 and I'm going to define a list 153 00:07:33,350 --> 00:07:35,520 with a two and a minus three, 154 00:07:35,520 --> 00:07:37,810 and maybe we'll throw a zero in there, 155 00:07:37,810 --> 00:07:40,440 and let's go ahead and call that our list. 156 00:07:40,440 --> 00:07:44,270 So we've got five integer values in this list, 157 00:07:44,270 --> 00:07:46,090 and for each of these items, 158 00:07:46,090 --> 00:07:48,840 we would like to add them to the total. 159 00:07:48,840 --> 00:07:50,490 Now, just as we did up above, 160 00:07:50,490 --> 00:07:53,530 we need a colon to introduce the suite. 161 00:07:53,530 --> 00:07:56,629 So the syntax of defining a list in Python 162 00:07:56,629 --> 00:07:58,730 is simply a set of square brackets 163 00:07:58,730 --> 00:08:00,480 containing a bunch of values 164 00:08:00,480 --> 00:08:04,240 separated by commas within the list. 165 00:08:04,240 --> 00:08:07,330 So the for statement already knows 166 00:08:07,330 --> 00:08:09,880 how to process each of those items, 167 00:08:09,880 --> 00:08:11,550 each item in a sequence 168 00:08:11,550 --> 00:08:14,677 or what is known as an iterable item. 169 00:08:14,677 --> 00:08:18,250 We're going to press Enter to now create the statement 170 00:08:18,250 --> 00:08:20,000 that will add each of these to the total. 171 00:08:20,000 --> 00:08:23,980 So total gets total plus number, 172 00:08:23,980 --> 00:08:26,970 so for each number, I wanna add it to the total 173 00:08:26,970 --> 00:08:28,710 and store the result in total, 174 00:08:28,710 --> 00:08:30,880 and we'll show you the plus equals operator 175 00:08:31,840 --> 00:08:34,080 subsequently in this lesson as well 176 00:08:34,080 --> 00:08:36,500 so that we can shorten this statement. 177 00:08:36,500 --> 00:08:38,650 So at this point, I'll go ahead and press Enter, 178 00:08:38,650 --> 00:08:41,370 and Enter again to execute that statement. 179 00:08:41,370 --> 00:08:45,300 And now, total contains the sum of these five values. 180 00:08:45,300 --> 00:08:48,480 So two, and then we add to it a negative three, 181 00:08:48,480 --> 00:08:50,000 which gives us negative one. 182 00:08:50,000 --> 00:08:52,128 We add zero to that, so we still have negative one. 183 00:08:52,128 --> 00:08:57,128 17 gives me 16 total because we have to subtract the one. 184 00:08:57,790 --> 00:09:02,790 And then, we add nine to that to get a total of 25. 185 00:09:02,860 --> 00:09:06,200 So there is a simple example of iterating 186 00:09:06,200 --> 00:09:08,780 through all of the items in a list. 187 00:09:08,780 --> 00:09:10,670 Now, as we move forward in Python, 188 00:09:10,670 --> 00:09:13,720 you're going to see several different sequence types. 189 00:09:13,720 --> 00:09:18,170 We've now shown you two sequence types, strings and lists. 190 00:09:18,170 --> 00:09:21,920 We'll also introduce another one called tuples as well. 191 00:09:21,920 --> 00:09:24,020 And as we work our way through, 192 00:09:24,020 --> 00:09:26,530 you'll see that all of these different sequence types 193 00:09:26,530 --> 00:09:31,190 are used frequently throughout Python programming. 194 00:09:31,190 --> 00:09:35,400 So the last thing I wanna cover in this particular video 195 00:09:35,400 --> 00:09:37,990 is the range function. 196 00:09:37,990 --> 00:09:40,830 Some of you are coming from other programming languages 197 00:09:40,830 --> 00:09:44,000 where you're used to potentially having a for loop 198 00:09:44,000 --> 00:09:48,520 that can perform counter-controlled repetition or iteration 199 00:09:48,520 --> 00:09:52,000 where you know in advance I would like to iterate, 200 00:09:52,000 --> 00:09:54,810 let's say, 10 times and do something. 201 00:09:54,810 --> 00:09:57,020 So let's create a simple example 202 00:09:57,020 --> 00:09:59,070 of a counter-controlled loop, 203 00:09:59,070 --> 00:10:02,580 but using sequences in Python. 204 00:10:02,580 --> 00:10:04,420 So the way we're going to do that is 205 00:10:04,420 --> 00:10:07,790 by using the built-in range function. 206 00:10:07,790 --> 00:10:08,623 So you remember, 207 00:10:08,623 --> 00:10:11,980 there's a lot of built-in functions in Python, 208 00:10:11,980 --> 00:10:14,000 and we're going to introduce range here, 209 00:10:14,000 --> 00:10:16,070 and then, we'll look at it in more depth 210 00:10:16,070 --> 00:10:17,310 a little bit later. 211 00:10:17,310 --> 00:10:21,810 But basically, the range function will produce for you 212 00:10:21,810 --> 00:10:23,950 a sequence of integer values. 213 00:10:23,950 --> 00:10:27,760 So if I go ahead and say for counter in range, 214 00:10:27,760 --> 00:10:30,940 and let's say 10, I would... 215 00:10:30,940 --> 00:10:33,851 Whoops, I spelled range wrong, let me fix that, there we go. 216 00:10:33,851 --> 00:10:36,670 For counter in range 10, I wanna do something, 217 00:10:36,670 --> 00:10:37,830 what's going to happen here 218 00:10:37,830 --> 00:10:42,624 is the range function is going to produce 219 00:10:42,624 --> 00:10:46,520 the sequence of integer values from zero through nine. 220 00:10:46,520 --> 00:10:49,680 So, zero, one, two, all the way up to nine. 221 00:10:49,680 --> 00:10:52,340 And for each of those items, we can do something. 222 00:10:52,340 --> 00:10:55,640 In this case, let's simply go ahead and display the values. 223 00:10:55,640 --> 00:10:58,370 So we'll print the counter value, 224 00:10:58,370 --> 00:11:02,040 and let's use the end keyword argument 225 00:11:02,040 --> 00:11:04,630 to put a space between each one of those 226 00:11:04,630 --> 00:11:06,820 so that we print them all out on one line 227 00:11:06,820 --> 00:11:09,880 instead of vertically one line at a time. 228 00:11:09,880 --> 00:11:11,760 So I'll go ahead and execute that, 229 00:11:11,760 --> 00:11:14,441 and there, you can see the range of values 230 00:11:14,441 --> 00:11:17,750 that was produced by the range function. 231 00:11:17,750 --> 00:11:19,660 So whenever you need to do 232 00:11:19,660 --> 00:11:22,620 counter-controlled iteration in Python, 233 00:11:22,620 --> 00:11:25,510 this is the typical type of statement 234 00:11:25,510 --> 00:11:27,423 that you're going to implement.