WEBVTT 1 00:00:00.600 --> 00:00:02.330 Hi guys and welcome back. 2 00:00:02.330 --> 00:00:03.740 In this video we're going to look 3 00:00:03.740 --> 00:00:06.490 at how to get the remainder of a division. 4 00:00:06.490 --> 00:00:08.370 And this can be a really useful thing 5 00:00:08.370 --> 00:00:10.620 when we are trying to check whether a number 6 00:00:10.620 --> 00:00:12.920 is odd or even, and that itself 7 00:00:12.920 --> 00:00:15.060 also has a number of uses. 8 00:00:15.060 --> 00:00:19.492 Let's start by doing something like division with remainder. 9 00:00:19.492 --> 00:00:23.330 Remainder like that, and we're going to do 12 10 00:00:23.330 --> 00:00:24.590 divided by five. 11 00:00:24.590 --> 00:00:28.730 So if print this out, then you'll see 12 00:00:28.730 --> 00:00:32.280 that the output is just two. 13 00:00:32.280 --> 00:00:35.430 Even though 12 divided by five should give you 14 00:00:35.430 --> 00:00:38.400 two, remainder two. 15 00:00:38.400 --> 00:00:41.250 That's because five goes into 12 two times, 16 00:00:41.250 --> 00:00:43.850 and you have two left over, so two is the remainder. 17 00:00:44.700 --> 00:00:47.386 You can get the remainder by just doing 18 00:00:47.386 --> 00:00:52.190 12 % five, and this is essentially the counterpart 19 00:00:52.190 --> 00:00:54.380 to integer division because it gives you 20 00:00:54.380 --> 00:00:56.003 the remainder of the division. 21 00:00:58.080 --> 00:01:00.010 So if we run this like that, you'll see 22 00:01:00.010 --> 00:01:02.260 that you get two in the first instance 23 00:01:02.260 --> 00:01:05.010 of 12 divided by five, and then you also get two 24 00:01:05.010 --> 00:01:08.483 which is the remainder in 12 divided by five there. 25 00:01:09.380 --> 00:01:12.410 Now if we change this to 13, you'll see 26 00:01:12.410 --> 00:01:13.310 that the numbers change. 27 00:01:13.310 --> 00:01:15.470 The first one will still be two, but the second 28 00:01:15.470 --> 00:01:17.420 one will now be three, because the remainder 29 00:01:17.420 --> 00:01:19.623 of 13 divided by five is three. 30 00:01:20.630 --> 00:01:22.890 So why is getting the remainder so popular? 31 00:01:22.890 --> 00:01:23.730 Why is it useful? 32 00:01:23.730 --> 00:01:27.810 Why does Python have it's own operator just for that? 33 00:01:27.810 --> 00:01:32.640 Look at this numbers, and you'll spot a pattern probably. 34 00:01:32.640 --> 00:01:36.360 What is the remainder of 10 divided by two? 35 00:01:36.360 --> 00:01:38.840 Or 14 divided by two, or six divided by two, 36 00:01:38.840 --> 00:01:41.260 or 340 divided by two, the remainder 37 00:01:41.260 --> 00:01:43.070 in all of these is zero. 38 00:01:43.070 --> 00:01:46.573 Because they are even numbers divided by two. 39 00:01:47.710 --> 00:01:49.540 Similarly if you have something like 40 00:01:49.540 --> 00:01:52.970 11 divided by two, or 27 divided by two, 41 00:01:52.970 --> 00:01:55.220 or three divided by two, the remainder 42 00:01:55.220 --> 00:01:57.623 in all of these cases is always one. 43 00:01:58.580 --> 00:02:01.320 So one of the reasons why this operator exists 44 00:02:01.320 --> 00:02:03.540 and why it's so popular in programming 45 00:02:03.540 --> 00:02:06.100 is because it very easily allows you to determine 46 00:02:06.100 --> 00:02:08.703 whether a number is odd or even. 47 00:02:09.720 --> 00:02:12.130 Indeed for every even number, the remainder 48 00:02:12.130 --> 00:02:14.660 when divided by two is zero. 49 00:02:14.660 --> 00:02:16.313 And for every odd number, the remainder 50 00:02:16.313 --> 00:02:18.403 when divided by two is one. 51 00:02:19.330 --> 00:02:20.720 So let's look at some code. 52 00:02:20.720 --> 00:02:25.720 X is 37, the remainder we can do x % two, 53 00:02:28.340 --> 00:02:32.360 and then we can print out whether it is odd or even 54 00:02:32.360 --> 00:02:34.010 by printing this out. 55 00:02:34.010 --> 00:02:37.950 So if this value is one, that means that it is odd, 56 00:02:37.950 --> 00:02:40.980 and if it is zero, that means that it is even. 57 00:02:40.980 --> 00:02:42.790 So at this point I'm going to delete this code here 58 00:02:42.790 --> 00:02:44.330 just before running. 59 00:02:44.330 --> 00:02:45.513 And now let's run this. 60 00:02:46.460 --> 00:02:48.750 Usually that we get one back, which tell us 61 00:02:48.750 --> 00:02:50.733 this number here is odd. 62 00:02:51.770 --> 00:02:54.230 Think about this, have you ever seen a table 63 00:02:54.230 --> 00:02:57.150 online where each row has a different colour? 64 00:02:57.150 --> 00:02:59.210 So for example the first row is grey, 65 00:02:59.210 --> 00:03:01.640 the second one is white, third one's grey, 66 00:03:01.640 --> 00:03:03.370 the fourth one's white, so you've got 67 00:03:03.370 --> 00:03:05.450 like this sort of pattern? 68 00:03:05.450 --> 00:03:08.070 This is a perfect example of where the remainder, 69 00:03:08.070 --> 00:03:09.730 or being able to calculate whether a number 70 00:03:09.730 --> 00:03:12.073 is odd or even, comes into play. 71 00:03:13.300 --> 00:03:15.940 They are probably using this to only target 72 00:03:15.940 --> 00:03:19.240 the even rows and colour them in a grey background. 73 00:03:19.240 --> 00:03:20.730 Throughout the course we may also encounter 74 00:03:20.730 --> 00:03:23.210 other instances where this can be useful, 75 00:03:23.210 --> 00:03:24.240 but that's it for this video. 76 00:03:24.240 --> 00:03:25.900 Thank you for joining me, and I'll see you 77 00:03:25.900 --> 00:03:26.733 in the next one.