WEBVTT 1 00:00:01.871 --> 00:00:05.040 So, it's now time to talk about for loops. 2 00:00:05.040 --> 00:00:07.290 So, one of the reasons that computers are so useful, 3 00:00:07.290 --> 00:00:11.670 was that they can actually repeat an operations multiple times, very quickly. 4 00:00:11.670 --> 00:00:16.080 And this of course, removes the need for human to perform a repetitive task. 5 00:00:16.080 --> 00:00:19.830 Now we've seen the ForLoop in earlier examples, but now it's time to look at 6 00:00:19.830 --> 00:00:22.390 them in more detail and actually explain what was happening. 7 00:00:22.390 --> 00:00:25.570 So, what a ForLoop does it actually takes a range of values and 8 00:00:25.570 --> 00:00:28.240 assigns them one by one to a variable. 9 00:00:28.240 --> 00:00:31.080 It then executes a block of code once for each value. 10 00:00:31.080 --> 00:00:34.760 For a simple example, we're gonna have a go printing at the values from 1 to 20. 11 00:00:34.760 --> 00:00:39.520 So the format is to be taught in the four, 12 00:00:39.520 --> 00:00:42.470 key word four i in range. 13 00:00:42.470 --> 00:00:45.320 One comma 20 in brackets, then we indent the code. 14 00:00:45.320 --> 00:00:49.060 This is the block that we want to hit, have executed once for each value. 15 00:00:49.060 --> 00:00:54.140 So I print i is now, placement field, 16 00:00:54.140 --> 00:01:02.330 this should be double brackets.format i, format i, that's the simplest example. 17 00:01:02.330 --> 00:01:06.080 Again, what we're trying to do is go to get the values from one to 20. 18 00:01:06.080 --> 00:01:08.560 And then when we run that, you can see what's happen is for 19 00:01:08.560 --> 00:01:13.090 each value of i, we've actually got the value i is now one, two, three, four five, 20 00:01:13.090 --> 00:01:16.310 right through to 19, so it didn't go through the 20. 21 00:01:16.310 --> 00:01:20.170 If you think back to this, remember how our strings losses that we use 22 00:01:20.170 --> 00:01:23.150 return the characters up to, but not including, the N number? 23 00:01:23.150 --> 00:01:24.490 Well, ranges work in the same way. 24 00:01:24.490 --> 00:01:26.986 The last value specified is not included. 25 00:01:26.986 --> 00:01:31.240 So effectively, the way to rate is for i in range (1-20). 26 00:01:31.240 --> 00:01:33.290 Would be to actually say to yourself, 27 00:01:33.290 --> 00:01:36.160 I'm going to actually loop through the values 1 to 19. 28 00:01:36.160 --> 00:01:40.720 So the computer will actually repeat this code in here in the block, 29 00:01:40.720 --> 00:01:44.830 in this case 19 times, and incrementing the value of i each time. 30 00:01:44.830 --> 00:01:47.740 So the first time it runs, you can see i was set to 1, 31 00:01:47.740 --> 00:01:52.140 i is set to 2, i is set to 3, and so on and so forth, right through into 19. 32 00:01:52.140 --> 00:01:55.270 Now we're gonna talk more about naming variables in a later video. 33 00:01:55.270 --> 00:01:58.150 Up until now we've just used simple words that describe the value 34 00:01:58.150 --> 00:01:59.230 that we've been dealing with. 35 00:01:59.230 --> 00:02:01.680 And normally, you should avoid using single character names for 36 00:02:01.680 --> 00:02:03.020 variables like we've got here, like I. 37 00:02:03.020 --> 00:02:07.335 Because I don't really get many any indication of what the variable is for, 38 00:02:07.335 --> 00:02:08.715 but there is an exception to that and 39 00:02:08.715 --> 00:02:13.355 loop control variables are one of those few places where I and J are acceptable. 40 00:02:13.355 --> 00:02:15.265 I is taken to be short for index and 41 00:02:15.265 --> 00:02:18.735 the convention is his acceptable name in a ForLoop. 42 00:02:18.735 --> 00:02:23.195 So when you are creating a ForLoop like this, using I is quite okay and 43 00:02:23.195 --> 00:02:25.750 you find that there is a lot of code out there that actually does that. 44 00:02:25.750 --> 00:02:26.730 So another example of this. 45 00:02:26.730 --> 00:02:30.890 We can actually see the variable users and index in the next example which I'm about 46 00:02:30.890 --> 00:02:34.760 to type out, where we print each character in a string once at a time. 47 00:02:34.760 --> 00:02:37.040 So I'm going to actually just put that in the next slide. 48 00:02:37.040 --> 00:02:39.810 Being sure to go to the start of the block and not to indent it. 49 00:02:39.810 --> 00:02:41.875 So we're gonna talk in a number. 50 00:02:41.875 --> 00:02:43.040 Number= 9223372036854775807. 51 00:02:43.040 --> 00:02:49.430 And what we're gonna do is gonna put for 52 00:02:49.430 --> 00:02:56.990 I in range 0 LEN stands for length for number. 53 00:02:58.470 --> 00:03:02.470 Colon again, and 54 00:03:02.470 --> 00:03:07.900 we print number, I in square brackets. 55 00:03:07.900 --> 00:03:10.790 There's a couple of things here, we've introduced a new function. 56 00:03:10.790 --> 00:03:14.150 This function here is called LEN which is an abbreviation for length and 57 00:03:14.150 --> 00:03:16.840 it actually returns the length of a string. 58 00:03:16.840 --> 00:03:19.660 So, how many characters actually exist in that string? 59 00:03:19.660 --> 00:03:23.000 So, in this case it should actually return 25 if I'm counting correctly. 60 00:03:23.000 --> 00:03:27.550 But with that said, the way that we actually access individual characters 61 00:03:27.550 --> 00:03:30.560 in a string, they actually start from zero in 62 00:03:30.560 --> 00:03:34.990 this case to whatever the length is minus one, so 24 in this case. 63 00:03:34.990 --> 00:03:37.900 And because we established earlier that when we put a range in, 64 00:03:37.900 --> 00:03:40.960 we only go up into one less than the final number, 65 00:03:40.960 --> 00:03:43.710 we know that in the case the LEN(number) is gonna be correct. 66 00:03:43.710 --> 00:03:44.700 Cuz it's only gonna up through and 67 00:03:44.700 --> 00:03:48.210 process up to one less than that final number in the ForLoop. 68 00:03:48.210 --> 00:03:50.890 So that's actually one of the reasons why the range runs up to but 69 00:03:50.890 --> 00:03:52.770 not including the final value. 70 00:03:52.770 --> 00:03:55.720 So in many other programming languages, the loop would have to be specified 71 00:03:55.720 --> 00:03:58.540 as from zero to, and you might have to do something like this. 72 00:03:58.540 --> 00:04:01.480 Length, number, take one, because you'd have to code it for 73 00:04:01.480 --> 00:04:04.580 the fact you're starting from zero, and not starting from one, 74 00:04:04.580 --> 00:04:05.840 as you can see in the example up here. 75 00:04:05.840 --> 00:04:09.700 But in this case, with Python, you don't actually have to do that, 76 00:04:09.700 --> 00:04:12.930 because of course we established that when you use a range, 77 00:04:12.930 --> 00:04:16.010 it actually finishes at one less than the number you specify anyway. 78 00:04:16.010 --> 00:04:19.340 Basically, it works more how you'd expect and your free from having to consider 79 00:04:19.340 --> 00:04:22.530 whether you need to put this minus one and other such trivia which is really cool. 80 00:04:22.530 --> 00:04:26.060 So if we actually run this now, you can see what's happened here. 81 00:04:26.060 --> 00:04:27.530 Each time it run through the loop. 82 00:04:27.530 --> 00:04:31.460 It's actually printed out one element of that string. 83 00:04:31.460 --> 00:04:33.100 And by using the square bracket, 84 00:04:33.100 --> 00:04:37.140 what that is saying is print the character at position I. 85 00:04:37.140 --> 00:04:41.940 So we started on the first time position I, I had the value of of zero, so 86 00:04:41.940 --> 00:04:44.300 position zero is nine. 87 00:04:44.300 --> 00:04:47.470 Next time I'm gonna go through and position one is the comma, and so 88 00:04:47.470 --> 00:04:50.040 I wrote it through to the end and we should have an 87 on the end there. 89 00:04:50.040 --> 00:04:53.220 The last three digits there, so, you can see this is working quite nicely. 90 00:04:53.220 --> 00:04:54.740 And it didn't crash or anything like that and 91 00:04:54.740 --> 00:04:56.220 actually come up with the right number. 92 00:04:56.220 --> 00:04:58.880 That's one example but let's take this all to the next level. 93 00:04:58.880 --> 00:05:02.870 And let's just imagine that your processing data at someone elses exported 94 00:05:02.870 --> 00:05:03.780 from a spread sheet program. 95 00:05:03.780 --> 00:05:07.880 And let's just say that the spreadsheet was set to display numbers with commas or 96 00:05:07.880 --> 00:05:10.700 some other separators like in Europe for example it could be a four stop or 97 00:05:10.700 --> 00:05:11.590 a space. 98 00:05:11.590 --> 00:05:13.260 Normally, before we could process these numbers we 99 00:05:13.260 --> 00:05:16.190 would have to strip out the separators, but there's other ways to do this. 100 00:05:16.190 --> 00:05:18.540 And one way could be to check each character and 101 00:05:18.540 --> 00:05:21.010 only print it out if it's one of the digits from zero to nine. 102 00:05:21.010 --> 00:05:23.360 So what we could do is actually take this code again. 103 00:05:23.360 --> 00:05:24.281 I'm just gonna copy that. 104 00:05:24.281 --> 00:05:26.990 And paste it down here. 105 00:05:26.990 --> 00:05:30.470 We leave the same full range in here, but we add another test here. 106 00:05:30.470 --> 00:05:37.620 And the test we're gonna add is if number I, in square brackets, 107 00:05:37.620 --> 00:05:42.830 end in a string, one, two, three, four, five, six, seven, eight, nine. 108 00:05:42.830 --> 00:05:45.550 That's the case, and then we come back onto the print line, and then we actually 109 00:05:45.550 --> 00:05:49.830 indent that because we only want that executed, if this actually returns true. 110 00:05:49.830 --> 00:05:53.920 So, if you remember the end function, it returns true if whatever we're testing on 111 00:05:53.920 --> 00:05:57.140 the left hand side is actually end whatever is in the right hand side. 112 00:05:57.140 --> 00:06:00.450 So, in this case we're looking for the element in this case nine. 113 00:06:00.450 --> 00:06:03.720 And as you get that nine, yes, it's going to actually print that out. 114 00:06:03.720 --> 00:06:06.470 So, what this should do is actually go through and print all the numbers and 115 00:06:06.470 --> 00:06:08.230 it should actually skip the commas. 116 00:06:08.230 --> 00:06:10.300 Cuz of course, a comma is not actually in this string. 117 00:06:10.300 --> 00:06:11.829 So, let's just run that and see if it works. 118 00:06:13.740 --> 00:06:19.380 And what we should probably do is just to make sure, 119 00:06:19.380 --> 00:06:22.640 we'll just comment that out for now, and run that again. 120 00:06:22.640 --> 00:06:25.790 And you can see that it's now successfully 9223372036854775807. 121 00:06:25.790 --> 00:06:30.860 So it successfully skipped anything that wasn't a digit from 0 to 9. 122 00:06:30.860 --> 00:06:34.330 Now one of the disadvantages with that is you notice that when using the print, 123 00:06:34.330 --> 00:06:37.570 automatically Python's actually editing the end of line count. 124 00:06:37.570 --> 00:06:39.630 So it's actually starting on a new line. 125 00:06:39.630 --> 00:06:42.660 So we can actually stop that if we want though, because it actually syntax. 126 00:06:42.660 --> 00:06:45.080 So you may actually seen this when you actually 127 00:06:45.080 --> 00:06:47.520 topping of coming off the numbers, we're gonna add a comma there. 128 00:06:48.620 --> 00:06:50.070 And notice how this comes up automatically and 129 00:06:50.070 --> 00:06:52.310 IntelliJ is saying there's some other options here. 130 00:06:52.310 --> 00:06:56.730 And part of fault, end=/n which is a new line character. 131 00:06:56.730 --> 00:06:59.890 So that's what happens automatically and that consequently each number, 132 00:06:59.890 --> 00:07:01.760 each time it's hitting a number in the loop. 133 00:07:01.760 --> 00:07:04.480 When we're doing the print, it's actually skipping to the next line. 134 00:07:04.480 --> 00:07:06.850 So we'll be providing a course to first parameter, 135 00:07:06.850 --> 00:07:09.810 which is obviously the object that we want printed. 136 00:07:09.810 --> 00:07:13.950 But as you can see in the screen, we can also specify separator and a value for N. 137 00:07:13.950 --> 00:07:16.750 So we're gonna come back to separators and the last two options fall on 138 00:07:16.750 --> 00:07:20.470 flush will be covered when we talk about file IO in a later video. 139 00:07:20.470 --> 00:07:22.475 But the end parameter is the one that's interesting here. 140 00:07:22.475 --> 00:07:26.230 Cuz at the moment you can see that it defaults again, to /n, which is new line. 141 00:07:26.230 --> 00:07:29.190 But we can actually override that, so for example, if we set that 142 00:07:29.190 --> 00:07:33.440 to an empty string, then a new line's not gonna be started after each character. 143 00:07:33.440 --> 00:07:38.165 So what we could do is we could type something like end = ' ', 144 00:07:38.165 --> 00:07:41.055 and that's overwriting the standard n = '\n'. 145 00:07:41.055 --> 00:07:44.720 And what we should see when we wrote it is the numbers 146 00:07:44.720 --> 00:07:45.810 now appearing from left to right. 147 00:07:45.810 --> 00:07:48.440 Because it's not a new line being automatically added each time 148 00:07:48.440 --> 00:07:49.160 the printout's made. 149 00:07:49.160 --> 00:07:53.670 Now if we wanted to use a number that we come up for performing calculations. 150 00:07:53.670 --> 00:07:57.180 What we could do is we can concatenate each character onto the new string and 151 00:07:57.180 --> 00:07:59.670 then convert the final thing to an integer. 152 00:07:59.670 --> 00:08:02.590 So, we have to initialize a string variable to hold the digits first or 153 00:08:02.590 --> 00:08:03.700 path will give an error. 154 00:08:03.700 --> 00:08:07.480 Because it obviously won't know if a plus should attempt to perform an addition or 155 00:08:07.480 --> 00:08:08.320 concatenation. 156 00:08:08.320 --> 00:08:12.960 So the way we do that is we come back up here and we'll type something like 157 00:08:12.960 --> 00:08:18.150 cleanedNumber = ' ', so that's what we're gonna start with. 158 00:08:18.150 --> 00:08:19.790 We're gonna leave the loop as it was before. 159 00:08:19.790 --> 00:08:21.870 We're gonna leave the if there as well. 160 00:08:21.870 --> 00:08:24.140 But instead of printing out at this point, 161 00:08:24.140 --> 00:08:29.328 what we're in fact going to do is we're gonna type cleanedNumber = cleanedNumber 162 00:08:29.328 --> 00:08:34.940 + number [i], I'm gonna rid of that print. 163 00:08:34.940 --> 00:08:37.540 Now back to the left, so it's gonna go through and each time it finds a number. 164 00:08:37.540 --> 00:08:41.430 It's gonna add only that digit to the cleaned number string that we've actually 165 00:08:41.430 --> 00:08:42.180 defined on line nine. 166 00:08:42.180 --> 00:08:45.680 So now what we want to do is convert that to an integer. 167 00:08:45.680 --> 00:08:50.600 So we type new number = int, clean number. 168 00:08:52.110 --> 00:08:53.270 And I could type something like print, 169 00:08:53.270 --> 00:08:56.900 because as far as the parcel's concerned this is now a number print. 170 00:08:56.900 --> 00:09:02.890 The number is Format new number. 171 00:09:02.890 --> 00:09:03.765 So if we run this. 172 00:09:04.850 --> 00:09:07.870 We get the number is 92 blah, blah, blah, blah, blah 807. 173 00:09:07.870 --> 00:09:10.680 So as far as path is concerned that is now a proper number. 174 00:09:10.680 --> 00:09:13.690 So that's how you'd actually go about converting from a string and 175 00:09:13.690 --> 00:09:16.680 then actually building up a number that's actually in a string. 176 00:09:16.680 --> 00:09:19.030 And then converting that string back to an integer. 177 00:09:19.030 --> 00:09:22.670 Thing here is that, you need to actually be very careful with your indentation, 178 00:09:22.670 --> 00:09:25.570 because we're nesting blocks within blocks and the ForLoop contains and 179 00:09:25.570 --> 00:09:29.030 if block, which contains the block that performs the concatenation. 180 00:09:29.030 --> 00:09:34.190 So, if line 15 is actually indented by 408 spaces, then the code's still gonna work, 181 00:09:34.190 --> 00:09:36.520 but new numbers gonna be assigned a value of which time through the loop. 182 00:09:36.520 --> 00:09:40.260 So, in other words if I actually tap that, so, 183 00:09:41.540 --> 00:09:44.530 actually run this and we still go the same result. 184 00:09:44.530 --> 00:09:48.400 But what's actually happened here is each time of this loop your 185 00:09:48.400 --> 00:09:50.590 numbers have been assigned that value. 186 00:09:50.590 --> 00:09:51.590 And doesn't make it, 187 00:09:51.590 --> 00:09:54.230 really it's not a really problem here when everybody got a few numbers to deal with. 188 00:09:54.230 --> 00:09:58.190 If you can imagine that if we had 10,000 numbers to actually deal with. 189 00:09:58.190 --> 00:10:01.040 You're actually repeating this assignment, or 190 00:10:01.040 --> 00:10:05.570 the value enter clean number each time we actually run, so it's a lot processing. 191 00:10:05.570 --> 00:10:08.260 So, the end result could be significant delays you're introducing, 192 00:10:08.260 --> 00:10:10.410 even if the final answer, as you saw, was still correct. 193 00:10:10.410 --> 00:10:14.230 So, in this case, it's far better to actually have that calculation 194 00:10:14.230 --> 00:10:18.120 do occur only once, and that's after you've been through the entire full loop. 195 00:10:18.120 --> 00:10:22.140 and we built up the entire clean number string perform at once 196 00:10:22.140 --> 00:10:24.730 as opposed to actually performing it each time through the loop. 197 00:10:24.730 --> 00:10:27.680 Now, note this is a specific example. 198 00:10:27.680 --> 00:10:29.820 Where our orignal number came from a spreadsheet program. 199 00:10:29.820 --> 00:10:31.970 We knew that it was a valid number. 200 00:10:31.970 --> 00:10:35.270 Just, you know they happen to contain some extraneous formatting. 201 00:10:35.270 --> 00:10:37.770 So this code that we've actually written, wouldn't be suitable for 202 00:10:37.770 --> 00:10:41.010 validating numbers that had been typed on the keyboard, for example. 203 00:10:41.010 --> 00:10:44.280 And also it wouldn't be suitable if the spreadsheet contains scientific data, 204 00:10:44.280 --> 00:10:47.610 where the numbers could be in an exponential or scientific format, 205 00:10:47.610 --> 00:10:49.680 such as 1.353e+18. 206 00:10:49.680 --> 00:10:54.770 So we're gonna be looking at catering both of those or catering for 207 00:10:54.770 --> 00:10:56.990 both those situations in future videos. 208 00:10:56.990 --> 00:10:58.090 So I'm gonna end this video here. 209 00:10:58.090 --> 00:11:01.100 In the next video, we're gonna continue with our discussion on four loops. 210 00:11:01.100 --> 00:11:02.440 So I'll see you in that next video.