WEBVTT 1 00:00:01.990 --> 00:00:03.700 Hi guys, and welcome back. 2 00:00:03.700 --> 00:00:05.720 In this video we're going to learn about 3 00:00:05.720 --> 00:00:08.630 the else keyword that we've used previously 4 00:00:08.630 --> 00:00:10.360 with if statements but now we're going 5 00:00:10.360 --> 00:00:11.793 to use it with a loop. 6 00:00:12.870 --> 00:00:15.760 Let's look at this code that you saw in the last video. 7 00:00:15.760 --> 00:00:18.700 We've got our cars, which is a list of strings. 8 00:00:18.700 --> 00:00:22.570 It contains multiple okay strings and a faulty one. 9 00:00:22.570 --> 00:00:23.950 Then we've got a for loop. 10 00:00:23.950 --> 00:00:27.050 Where we iterate for status in cars. 11 00:00:27.050 --> 00:00:28.690 And then if the status is faulty 12 00:00:28.690 --> 00:00:30.798 we're going to print, stopping the production line 13 00:00:30.798 --> 00:00:33.240 and we're going to break. 14 00:00:33.240 --> 00:00:36.250 So in this case, when we encounter the break keyword, 15 00:00:36.250 --> 00:00:39.270 we completely terminate the loop. 16 00:00:39.270 --> 00:00:40.620 Then, if it wasn't faulty, 17 00:00:40.620 --> 00:00:42.917 we're going to print that this car is okay. 18 00:00:42.917 --> 00:00:45.490 And we're going to ship it away to the customer. 19 00:00:45.490 --> 00:00:48.410 Imagine you wanted to print something out, 20 00:00:48.410 --> 00:00:51.233 if all of the cars were shipped successfully. 21 00:00:52.700 --> 00:00:53.840 Because at the moment you're going to 22 00:00:53.840 --> 00:00:56.120 print something out when you find a faulty one. 23 00:00:56.120 --> 00:00:57.983 But you can't print something out 24 00:00:57.983 --> 00:01:00.680 when there are no faulty ones. 25 00:01:00.680 --> 00:01:03.890 A naive approach, something that a new programmer might do 26 00:01:05.083 --> 00:01:08.390 is might say, all successful is true. 27 00:01:08.390 --> 00:01:10.597 And then, if a car status is faulty, 28 00:01:10.597 --> 00:01:12.150 then you set it to false. 29 00:01:12.150 --> 00:01:14.240 All successful is false. 30 00:01:14.240 --> 00:01:16.430 Because clearly, one of them was faulty. 31 00:01:16.430 --> 00:01:18.011 So you set all successful to false, 32 00:01:18.011 --> 00:01:20.340 and now after the loop you can say, 33 00:01:20.340 --> 00:01:21.883 if all successful, 34 00:01:23.910 --> 00:01:26.843 then print all cars built successfully. 35 00:01:28.010 --> 00:01:29.023 No faulty cars. 36 00:01:30.450 --> 00:01:32.440 This would be a naive approach. 37 00:01:32.440 --> 00:01:34.910 And an approach that involves a lot of coding 38 00:01:34.910 --> 00:01:38.070 in order to not use something that's already 39 00:01:38.070 --> 00:01:39.830 built into the language. 40 00:01:39.830 --> 00:01:42.580 What we do here is we set a variable, to true, 41 00:01:42.580 --> 00:01:44.650 and then only when we encounter a problem 42 00:01:44.650 --> 00:01:46.560 do we set it to false. 43 00:01:46.560 --> 00:01:47.830 Then we break. 44 00:01:47.830 --> 00:01:49.257 And then after the loop, 45 00:01:49.257 --> 00:01:52.940 if they were all successful, i.e. we did not break, 46 00:01:52.940 --> 00:01:54.697 we did not encounter a faulty car, 47 00:01:54.697 --> 00:01:57.920 then we print that they were all built successfully. 48 00:01:57.920 --> 00:02:01.010 But actually, the loops in Python 49 00:02:01.010 --> 00:02:03.163 already come with something like this. 50 00:02:04.150 --> 00:02:07.197 There is a way to run a piece of code 51 00:02:07.197 --> 00:02:11.390 if the loop did not encounter a break. 52 00:02:11.390 --> 00:02:14.240 And that, is actually the else keyword 53 00:02:14.240 --> 00:02:15.900 that we've seen before. 54 00:02:15.900 --> 00:02:19.240 Now we've got the for loop, 55 00:02:19.240 --> 00:02:21.340 then the for loop body, 56 00:02:21.340 --> 00:02:23.200 and then this else statement 57 00:02:23.200 --> 00:02:27.200 directly in the same indentation level as the for loop. 58 00:02:27.200 --> 00:02:29.510 Which tells Python that they are related. 59 00:02:29.510 --> 00:02:31.520 Remember, it's the same thing for if statements, 60 00:02:31.520 --> 00:02:35.310 but now the else is in the same line as the for loop. 61 00:02:35.310 --> 00:02:36.863 And what this means in Python, 62 00:02:38.722 --> 00:02:41.750 is that if the loop completely ran 63 00:02:41.750 --> 00:02:43.930 through all of the elements, 64 00:02:43.930 --> 00:02:45.960 without encountering a break, 65 00:02:45.960 --> 00:02:49.670 or an error, then we run this code here. 66 00:02:49.670 --> 00:02:51.320 That's what it means. 67 00:02:51.320 --> 00:02:53.870 So we no longer need this variable here. 68 00:02:53.870 --> 00:02:55.390 We can just do this. 69 00:02:55.390 --> 00:02:57.170 And you'll see that nothing gets printed out. 70 00:02:57.170 --> 00:02:58.310 All cars built successfully. 71 00:02:58.310 --> 00:03:01.220 No faulty cars, does not appear because 72 00:03:01.220 --> 00:03:04.050 we stop the production line halfway through. 73 00:03:04.050 --> 00:03:08.300 But if we remove the faulty, then you'll see that 74 00:03:08.300 --> 00:03:09.950 all cars were built successfully. 75 00:03:09.950 --> 00:03:11.620 No faulty cars. 76 00:03:11.620 --> 00:03:16.160 So the else keyword can actually be applied to loops, 77 00:03:16.160 --> 00:03:18.880 both the for loop and the while loop. 78 00:03:18.880 --> 00:03:22.039 And it means that this code here will run 79 00:03:22.039 --> 00:03:24.510 if the loop runs to completion, 80 00:03:24.510 --> 00:03:28.760 i.e. there are no breaks and no errors. 81 00:03:28.760 --> 00:03:31.460 This is actually something that not a lot of people know. 82 00:03:31.460 --> 00:03:33.740 That you can use the else keyword on loops, 83 00:03:33.740 --> 00:03:35.920 but it can save you quite a few lines of code, 84 00:03:35.920 --> 00:03:37.530 and it can make your code a bit easier 85 00:03:37.530 --> 00:03:39.300 to read, as well. 86 00:03:39.300 --> 00:03:41.220 So I hope you've learned something in this video. 87 00:03:41.220 --> 00:03:42.400 Thank you for joining me. 88 00:03:42.400 --> 00:03:44.050 And I'll see you in the next one.