WEBVTT 1 00:00:02.250 --> 00:00:05.800 So in the previous video, we started talking about the for loop in Python. 2 00:00:05.800 --> 00:00:09.280 So let's actually continue on with that now with a few more advanced examples. 3 00:00:09.280 --> 00:00:12.670 So I'm gonna actually delete the code that we had from the previous video. 4 00:00:12.670 --> 00:00:17.938 And just a bit of a recap, that unlike many other languages such as C and C++, 5 00:00:17.938 --> 00:00:23.300 for loops in Python work by having a variable iterate over a set of values. 6 00:00:23.300 --> 00:00:28.500 So the difference is with C and C++ and Java, for loops can be read as something 7 00:00:28.500 --> 00:00:34.660 along the lines of for i starting at 1 up to 20 in steps of 1, do something. 8 00:00:34.660 --> 00:00:37.260 If Python, the follow-ups read more like, for 9 00:00:37.260 --> 00:00:40.590 i, taking each of the values in the following sequence, do something. 10 00:00:40.590 --> 00:00:43.270 Now it doesn't sound like there's a great difference with that. 11 00:00:43.270 --> 00:00:48.230 But the actual difference of the different approach to for loops that Python uses, 12 00:00:48.230 --> 00:00:51.850 makes it really powerful, incredibly powerful, but also very flexible. 13 00:00:51.850 --> 00:00:55.960 So in our example in previous video, we wanted to show a follow with an index 14 00:00:55.960 --> 00:00:58.870 taking values from zero to the length of the string. 15 00:00:58.870 --> 00:01:02.880 But because the string is a sequence, what we can actually do is we can simplify that 16 00:01:02.880 --> 00:01:07.330 a great deal and set our loop variable to each of the characters in the sequence. 17 00:01:07.330 --> 00:01:09.430 So it sort of cuts out a step there, if you will. 18 00:01:09.430 --> 00:01:12.960 Now what we could do is we could leave the variable name as i, but we'll change it to 19 00:01:12.960 --> 00:01:16.540 char because it's no longer being used as an index in the string. 20 00:01:16.540 --> 00:01:18.520 And this will actually work better, make more sense, 21 00:01:18.520 --> 00:01:20.230 if you see the code up there on top. 22 00:01:20.230 --> 00:01:23.930 So in this example it's also a string of length one rather than integers sideways. 23 00:01:23.930 --> 00:01:25.590 So let's just have a look at that code now. 24 00:01:25.590 --> 00:01:26.800 So I'm going to type that number again. 25 00:01:26.800 --> 00:01:31.850 So what I'll do is undo that and go just to the first line, the first two lines, 26 00:01:31.850 --> 00:01:33.840 we'll keep those because we need to clean one as well. 27 00:01:33.840 --> 00:01:36.960 I'll just delete this and redo it again so it make sense as I'm typing it. 28 00:01:36.960 --> 00:01:43.020 So this time instead of using the len and so forth, you know the len for the number. 29 00:01:43.020 --> 00:01:47.890 We can put for char in number. 30 00:01:47.890 --> 00:01:52.670 And what that's going to do is extract a character that each position in that 31 00:01:52.670 --> 00:01:53.970 number string. 32 00:01:53.970 --> 00:02:02.570 And then we can put something like if char in 0123456789 colon. 33 00:02:02.570 --> 00:02:04.700 If it is, then we do the same as we did before. 34 00:02:04.700 --> 00:02:10.310 CleanedNumber, equals, cleanedNumber plus char. 35 00:02:10.310 --> 00:02:14.150 So, again, we're not having to use that number and the index position with the i 36 00:02:14.150 --> 00:02:17.670 anymore because we're actually using the for char directly. 37 00:02:17.670 --> 00:02:20.970 And, again, getting back to the for loop, your number. 38 00:02:22.600 --> 00:02:29.712 As it was before enter cleanedNumber, print the number. 39 00:02:29.712 --> 00:02:33.840 Is the format number. 40 00:02:33.840 --> 00:02:36.850 We should find this works exactly the same as it did before. 41 00:02:36.850 --> 00:02:38.420 Number is 92, blah, blah, blah, blah, blah, 87. 42 00:02:38.420 --> 00:02:44.290 But, you notice how this has significantly reduced the amount of code for and number. 43 00:02:44.290 --> 00:02:46.470 And it's actually a lot more readable as well. 44 00:02:46.470 --> 00:02:48.560 We no have to use that i number anymore. 45 00:02:48.560 --> 00:02:51.960 And we no longer have to access each of the characters by using number and 46 00:02:51.960 --> 00:02:53.950 i in square brackets as we used previously. 47 00:02:53.950 --> 00:02:55.180 So, just to confirm that the for 48 00:02:55.180 --> 00:02:59.170 loop assigns each character in the sequence number to a loop 49 00:02:59.170 --> 00:03:03.630 variable character as it's going through each element in the actual string itself. 50 00:03:03.630 --> 00:03:06.970 So each time around the loop the variable char has the value of the next character 51 00:03:06.970 --> 00:03:07.870 from our string number. 52 00:03:07.870 --> 00:03:10.790 Now, we're gonna be actually looking at lists soon, but 53 00:03:10.790 --> 00:03:13.920 a list is just a sequence of things that we can use a for 54 00:03:13.920 --> 00:03:17.660 loop for to iterate through them, as in this examples. 55 00:03:17.660 --> 00:03:19.730 I'm actually talk some more code to show you what I mean. 56 00:03:19.730 --> 00:03:26.674 So, I'm gonna put something like for, 57 00:03:26.674 --> 00:03:31.370 state in ["not pinin", 58 00:03:31.370 --> 00:03:38.330 "no more"] a stiff, bereft of life. 59 00:03:38.330 --> 00:03:41.030 We'll run this and then we will just go through and talk about it. 60 00:03:42.300 --> 00:03:44.600 This parrot is plus state and 61 00:03:44.600 --> 00:03:48.578 that's opposed to doing something like this, print. 62 00:03:48.578 --> 00:03:54.265 This parrot is, you could have done, 63 00:03:54.265 --> 00:03:56.950 .format state. 64 00:03:56.950 --> 00:03:59.560 Now not because our sequence was a list of strings, you can see here. 65 00:03:59.560 --> 00:04:01.278 We've actually got a list of strings. 66 00:04:01.278 --> 00:04:06.180 So that's actually square bracket and the list of strings separated with commas. 67 00:04:06.180 --> 00:04:08.050 Now, because of that, because it's a sequence of strings, 68 00:04:08.050 --> 00:04:12.420 we can just state to the text this parrot is plus state. 69 00:04:12.420 --> 00:04:14.990 And what it's gonna do each time it goes through the loop, 70 00:04:14.990 --> 00:04:17.700 it's going to actually add one of these to it automatically. 71 00:04:17.700 --> 00:04:19.370 And best way to see this is actually run it. 72 00:04:20.480 --> 00:04:24.340 This parrot is, you see the first time it went through, not that one there. 73 00:04:24.340 --> 00:04:27.700 The second time I went through the for loop, is no more. 74 00:04:27.700 --> 00:04:32.170 This parrot, is a stiff, and fourth this parrot is bereft of life, 75 00:04:32.170 --> 00:04:33.752 that's supposed to be, not lift. 76 00:04:33.752 --> 00:04:34.610 [LAUGH] Of life. 77 00:04:34.610 --> 00:04:38.140 So you can see how the for loop is actually worked there, it's actually 78 00:04:38.140 --> 00:04:42.148 sort of went through the sequence one element at a time, so to speak. 79 00:04:42.148 --> 00:04:45.367 And just as a confirmation I've commented it out but 80 00:04:45.367 --> 00:04:49.698 we could equally put the parrot is and use {}".format(state). 81 00:04:49.698 --> 00:04:53.160 And it would be up to you to decide which one you think would be more readable. 82 00:04:53.160 --> 00:04:56.430 My personal opinion is this one is more readable and is basically less 83 00:04:56.430 --> 00:05:00.030 code as well, and just you to actually look at the previous one and that is, 84 00:05:00.030 --> 00:05:05.040 okay the four state values set to each time we go through the four loop. 85 00:05:05.040 --> 00:05:10.040 And speaking of for loops the range, and we used the range previously for 86 00:05:10.040 --> 00:05:12.560 a for loop, the range is also a sequence tie. 87 00:05:12.560 --> 00:05:16.450 It just specifies the start and end, rather than listing all values. 88 00:05:16.450 --> 00:05:20.600 In other words it's like a sequence of values, much like this, but 89 00:05:20.600 --> 00:05:22.300 instead of actually listing each and all of them, 90 00:05:22.300 --> 00:05:25.520 we're actually listing just the start and the end as I mentioned. 91 00:05:25.520 --> 00:05:28.150 In actual fact, the range is more than a start and end value, but 92 00:05:28.150 --> 00:05:29.570 it can also include step. 93 00:05:29.570 --> 00:05:31.100 We haven't seen that before. 94 00:05:31.100 --> 00:05:33.340 So let's actually go through and type that in. 95 00:05:33.340 --> 00:05:38.863 So we can do something like 4 i in range 0, 100, 96 00:05:38.863 --> 00:05:43.870 but we can also introduce the third parameter which is the step. 97 00:05:43.870 --> 00:05:46.680 And that's how much it's going to jump in between each loop. 98 00:05:46.680 --> 00:05:48.190 So if we actually run this. 99 00:05:48.190 --> 00:05:50.854 Probably would be the easiest way to see 100 00:05:50.854 --> 00:05:55.670 remain.format i, so, we actually run that now. 101 00:05:55.670 --> 00:05:58.970 So, you can see it's actually started on 102 00:05:58.970 --> 00:06:02.170 element zero which would expect because we started on the value of zero. 103 00:06:02.170 --> 00:06:05.699 But, notice how it skipped to five, and that's what this third thing here, 104 00:06:05.699 --> 00:06:06.634 this step value is. 105 00:06:06.634 --> 00:06:08.660 It means to step ahead. 106 00:06:08.660 --> 00:06:11.140 How many each time we go through the loop should we had two Is. 107 00:06:11.140 --> 00:06:14.610 So, in other words, i zero, the first time five, the second time 10, 108 00:06:14.610 --> 00:06:18.130 the third time 15 and so on through until we got to 95. 109 00:06:18.130 --> 00:06:22.460 Bring them on then it goes one less than the actual range, 110 00:06:22.460 --> 00:06:24.270 the ending range that we've added here. 111 00:06:24.270 --> 00:06:28.505 And because we're stepping in values of five, I finished at 95 instead of 100. 112 00:06:28.505 --> 00:06:32.313 Now just as you've seen nesting in for [INAUDIBLE] used, if statement and so 113 00:06:32.313 --> 00:06:33.880 forth to nest model ifs. 114 00:06:33.880 --> 00:06:35.680 We can actually nest loops as well. 115 00:06:35.680 --> 00:06:38.260 So we can actually nest a for loop within another loop, 116 00:06:38.260 --> 00:06:41.020 and that's a really powerful way to process data. 117 00:06:41.020 --> 00:06:44.290 So here's an example of how we can generate the times table used to teach 118 00:06:44.290 --> 00:06:45.940 children multiplication. 119 00:06:45.940 --> 00:06:48.400 So we can put for i. 120 00:06:48.400 --> 00:06:50.380 Don't forget, making sure we're starting to the left, 121 00:06:50.380 --> 00:06:53.270 so we're not getting caught up in any previous code. 122 00:06:53.270 --> 00:06:56.092 For i in range. 123 00:06:56.092 --> 00:07:02.095 Should be i range 1,13, and obviously that's gonna go and 124 00:07:02.095 --> 00:07:06.410 give us, go through 1 and 12, 1 to 12. 125 00:07:06.410 --> 00:07:13.040 And for j in range 1 to 13 as well. 126 00:07:14.150 --> 00:07:16.006 And then we can do print. 127 00:07:16.006 --> 00:07:22.552 Times zero is two .format, and 128 00:07:22.552 --> 00:07:28.616 it's gonna be i j, i times j. 129 00:07:29.633 --> 00:07:30.640 And then we can go print. 130 00:07:30.640 --> 00:07:33.060 We can actually put a spacer 131 00:07:33.060 --> 00:07:36.600 between each major number that we're actually going through and processing. 132 00:07:36.600 --> 00:07:39.970 So if I actually run that, you can see what it's actually done there. 133 00:07:39.970 --> 00:07:40.950 Scrolling back up to the top. 134 00:07:40.950 --> 00:07:43.770 We start on the one times table, one times one is one. 135 00:07:43.770 --> 00:07:44.720 Two times one is two. 136 00:07:44.720 --> 00:07:46.190 And so on right to 12. 137 00:07:46.190 --> 00:07:49.360 Then we start at the two times table and three times table, and 138 00:07:49.360 --> 00:07:52.200 right through until we got to the last part which was a 12 times table. 139 00:07:52.200 --> 00:07:54.237 12 times 12 is 144. 140 00:07:54.237 --> 00:07:58.594 And you can see how that was created with only a minimal amount of code, and 141 00:07:58.594 --> 00:08:03.441 that also the indent level again is really important here because if we put print in 142 00:08:03.441 --> 00:08:08.120 the wrong spot, actually print it up in there to that level instead. 143 00:08:08.120 --> 00:08:09.920 We get a completely different result. 144 00:08:09.920 --> 00:08:14.400 We actually get the equal signs with each time we've actually done a calculation. 145 00:08:14.400 --> 00:08:17.680 So, again, make sure you do watch your indent levels to make sure they're 146 00:08:17.680 --> 00:08:20.520 actually in the right spot when you're actually processing your code. 147 00:08:20.520 --> 00:08:23.080 And just another thing we can do here just before we end the video. 148 00:08:23.080 --> 00:08:26.730 We can actually change the print function's n character for a tab. 149 00:08:26.730 --> 00:08:29.287 And once we do that we can actually produce a pretty neat table. 150 00:08:29.287 --> 00:08:34.796 So, we can come back up here to print and put end equals slash t and 151 00:08:34.796 --> 00:08:38.382 put a comma there as well to make it valid. 152 00:08:38.382 --> 00:08:41.112 And now you can actually run that you can see we have something completely 153 00:08:41.112 --> 00:08:43.500 different, and we're actually going from left to right now. 154 00:08:43.500 --> 00:08:46.289 And we just make this a little bit wider it will probably make more sense. 155 00:08:48.050 --> 00:08:50.040 And that would just be a way to actually expand it out. 156 00:08:50.040 --> 00:08:52.740 And actually I've got the equals sign probably shouldn't be there now with 157 00:08:52.740 --> 00:08:53.290 this example. 158 00:08:53.290 --> 00:08:55.100 So I'm just gonna comment those out. 159 00:08:55.100 --> 00:08:57.374 We run this again. 160 00:08:57.374 --> 00:08:58.860 And we're going from left to right now. 161 00:08:58.860 --> 00:09:01.880 You can see 1 times 1 is 1, 2 times 1 is 2. 162 00:09:01.880 --> 00:09:03.580 We're going right across from left right. 163 00:09:03.580 --> 00:09:05.100 And just to pretty it up a little bit more, 164 00:09:05.100 --> 00:09:09.560 what we could also do then is here on this indent level. 165 00:09:09.560 --> 00:09:10.310 Let's actually print. 166 00:09:10.310 --> 00:09:12.300 Let's go to a new line at this point, just so 167 00:09:12.300 --> 00:09:16.010 it actually reads a little bit nicer for each time we change the number. 168 00:09:16.010 --> 00:09:17.850 We'll just print an empty string like so. 169 00:09:17.850 --> 00:09:21.945 So now if we run that, you see we're going to for each times table now, and 170 00:09:21.945 --> 00:09:23.045 it's looking a little bit more order. 171 00:09:23.045 --> 00:09:24.295 So it's just another way to actually try and 172 00:09:24.295 --> 00:09:27.115 sort things from left to right, just close it. 173 00:09:27.115 --> 00:09:29.825 Run down, window down, so and again, we have to print an empty string 174 00:09:29.825 --> 00:09:34.810 after the inner loop terminates in order to start each new set of on a new line. 175 00:09:34.810 --> 00:09:37.100 And notice we didn't have to specify a character for end. 176 00:09:37.100 --> 00:09:38.290 It goes back to being a new line. 177 00:09:38.290 --> 00:09:41.300 The change to a tab is not remembered from one print call to another. 178 00:09:41.300 --> 00:09:45.540 So, in other words, doing this here is not remembered when we get to this stage. 179 00:09:45.540 --> 00:09:46.730 That doesn't override that. 180 00:09:46.730 --> 00:09:49.770 Sort of, each time you use your print statement it doesn't remember anything 181 00:09:49.770 --> 00:09:51.090 from the previous one. 182 00:09:51.090 --> 00:09:52.480 Okay, so that's actually it. 183 00:09:52.480 --> 00:09:54.500 So we're going to end this video here now. 184 00:09:54.500 --> 00:09:56.880 We're not going to actually do a challenge for the for loop. 185 00:09:56.880 --> 00:10:00.190 But believe me you'll get plenty of time to actually complete some challenges. 186 00:10:00.190 --> 00:10:02.800 So in the next video we're going to actually start talking about the continue 187 00:10:02.800 --> 00:10:05.650 and break commands which actually work hand in hand with what 188 00:10:05.650 --> 00:10:08.480 you've been learning in this section of the course. 189 00:10:08.480 --> 00:10:09.740 So I'll see you in the next video.