WEBVTT 1 00:00:02.088 --> 00:00:05.450 So in this video we're gonna continue on talking about ranges. 2 00:00:05.450 --> 00:00:08.940 And just at the end of the previous video we started talking about 3 00:00:08.940 --> 00:00:10.090 equality with ranges. 4 00:00:10.090 --> 00:00:12.590 And just to run this example again. 5 00:00:12.590 --> 00:00:15.850 So you can see that, I'll just make it more space here. 6 00:00:15.850 --> 00:00:20.775 That my range which was set here, you can see under decimals which is the range 7 00:00:20.775 --> 00:00:26.230 3:40:3 and using a range actually topping it out actually returned the same result. 8 00:00:26.230 --> 00:00:27.720 So we actually returned true, in other words, 9 00:00:27.720 --> 00:00:31.880 saying both ranges were the same effectively, or equal in other words. 10 00:00:31.880 --> 00:00:38.367 What would actually happen, do you think, if we actually typed this? 11 00:00:38.367 --> 00:00:46.962 Print range 052 equal to range 062. 12 00:00:46.962 --> 00:00:48.754 So, what do you think that's going to print? 13 00:00:48.754 --> 00:00:49.770 True or false? 14 00:00:49.770 --> 00:00:50.450 Let's just run that. 15 00:00:51.590 --> 00:00:54.370 If there the numbers are different, we're actually gonna get true. 16 00:00:54.370 --> 00:00:57.430 So why you might be asking, is that equal when clearly they numbers are different? 17 00:00:57.430 --> 00:01:01.090 Well it's all got to do with the same sequence of values. 18 00:01:01.090 --> 00:01:05.150 So it's regardless of how the values were specified, it's what the end result is. 19 00:01:05.150 --> 00:01:05.740 So in other words, 20 00:01:05.740 --> 00:01:10.950 it's what the range actually returns that is important here to test equality. 21 00:01:10.950 --> 00:01:13.350 Obviously, it was quite easy on line three, testing equality, 22 00:01:13.350 --> 00:01:14.210 because they're identical. 23 00:01:14.210 --> 00:01:18.880 But on line four, of course, we've got one which has got 052 as the range, 24 00:01:18.880 --> 00:01:20.680 and the second one is 062. 25 00:01:20.680 --> 00:01:23.172 Can easily see what's going on here by actually printing out a list. 26 00:01:23.172 --> 00:01:29.618 So if we do a print, list, range, 052, and 27 00:01:29.618 --> 00:01:34.629 then we do the same thing for 062. 28 00:01:34.629 --> 00:01:37.809 It'll actually become clear why that is actually equal when you think maybe at 29 00:01:37.809 --> 00:01:39.120 first glance it shouldn't be. 30 00:01:39.120 --> 00:01:43.900 And you can see that because we're stepping by two, 31 00:01:43.900 --> 00:01:48.570 the actual sequence that ended up being generated was 024 in both cases. 32 00:01:48.570 --> 00:01:51.050 And, that's actually what the test for equality is. 33 00:01:51.050 --> 00:01:53.310 So, it was actually testing what the result of the range is, 34 00:01:53.310 --> 00:01:56.650 not the actual numbers that were typed in to come up with the range. 35 00:01:56.650 --> 00:01:58.380 So, it's very important distinction there. 36 00:01:58.380 --> 00:02:01.570 And just in relation to slicing again, we did start doing that. 37 00:02:01.570 --> 00:02:05.730 So a slice connects to the step value of a slice could also be negative. 38 00:02:05.730 --> 00:02:11.490 So it can go r equals range, 0 comma 100, 39 00:02:11.490 --> 00:02:16.491 print r, and we're gonna do for i in r, 40 00:02:16.491 --> 00:02:21.212 colon, colon, minus 2, print i. 41 00:02:21.212 --> 00:02:24.660 And if you run that, you can say basically we're stepping back, 42 00:02:24.660 --> 00:02:26.740 going backwards to start two at a time. 43 00:02:26.740 --> 00:02:30.270 Starting from the hundred and basically going down to one. 44 00:02:30.270 --> 00:02:35.210 Which of course is identical to typing for i in range. 45 00:02:35.210 --> 00:02:39.106 99, 0, -2, 46 00:02:39.106 --> 00:02:44.710 print i, and fix the typo. 47 00:02:46.160 --> 00:02:48.790 So you can see those two commands were basically identical. 48 00:02:48.790 --> 00:02:50.320 Okay we see that they're both the same, and 49 00:02:50.320 --> 00:02:52.250 we can actually confirm that by just doing a print. 50 00:02:56.120 --> 00:02:59.821 Separate them and you can see we got one counting down to one and standing for 51 00:02:59.821 --> 00:03:03.837 99 and the second one is 99 counting down to one so that's actually working. 52 00:03:06.921 --> 00:03:10.629 And do another space there, and take careful note of this line. 53 00:03:10.629 --> 00:03:15.435 To print(range(0, 54 00:03:15.435 --> 00:03:21.204 100)[::-2] == 55 00:03:21.204 --> 00:03:28.421 is equal to range 99, 0, -2. 56 00:03:28.421 --> 00:03:31.629 And if you run that, we should get true. 57 00:03:31.629 --> 00:03:33.000 And you can see we actually get true there. 58 00:03:33.000 --> 00:03:34.340 So that's correct. 59 00:03:34.340 --> 00:03:38.968 But if we actually do this for i in range, 0, 100, 60 00:03:38.968 --> 00:03:45.980 -2, print i. 61 00:03:45.980 --> 00:03:47.921 We gonna run that. 62 00:03:47.921 --> 00:03:48.930 We actually get nothing. 63 00:03:48.930 --> 00:03:51.730 We only get the true which is from this line here. 64 00:03:51.730 --> 00:03:53.620 We don't actually get any output from that. 65 00:03:53.620 --> 00:03:56.390 The reason is because in the negative slice example, 66 00:03:56.390 --> 00:03:59.180 a negative step causes a slice to be reversed. 67 00:03:59.180 --> 00:04:03.350 Now, the same effect is obtained by specifying a negative step in the range, 68 00:04:03.350 --> 00:04:05.190 and you can see whether we've done that. 69 00:04:05.190 --> 00:04:08.434 But the start and end values must also be reversed, 70 00:04:08.434 --> 00:04:11.904 otherwise the reading, the way it's coming across, 71 00:04:11.904 --> 00:04:16.671 is that it starts at 0 and moves in steps of minus 2, until you hit 100. 72 00:04:16.671 --> 00:04:19.653 So you can see what the problem is there and that's never gonna obviously work if 73 00:04:19.653 --> 00:04:22.480 it's moving back from zero, which is why we didn't get any output. 74 00:04:22.480 --> 00:04:26.810 But the slice, in contrast to that, is returning items from a sequence and 75 00:04:26.810 --> 00:04:30.990 that as we saw if a negative index is specified then you start counting 76 00:04:30.990 --> 00:04:32.170 from the end of the sequence. 77 00:04:32.170 --> 00:04:36.242 So it's -1 being the last item, -2 second to last, etc. 78 00:04:36.242 --> 00:04:39.130 So, a range with a negative step can be very useful for counting a for 79 00:04:39.130 --> 00:04:42.950 loop backwards, but a negative slice is also extremely useful, and 80 00:04:42.950 --> 00:04:43.850 I'm gonna show you why. 81 00:04:43.850 --> 00:04:46.670 So, we're gonna first comment out this code. 82 00:04:46.670 --> 00:04:49.160 And let's try an example. 83 00:04:49.160 --> 00:04:50.670 So, I would type backstring. 84 00:04:51.810 --> 00:04:55.010 So, what I will do, for reasons that will come up is I am going to paste this in. 85 00:04:55.010 --> 00:04:57.480 I will paste that in, backstring. 86 00:04:57.480 --> 00:05:03.140 Then I will actually type, print backstring colon, colon, negative one. 87 00:05:04.600 --> 00:05:07.750 We'll actually run that, Python is a very powerful language. 88 00:05:07.750 --> 00:05:09.010 So you can see what it's actually done there. 89 00:05:09.010 --> 00:05:12.670 It's basically a simple way to process the range in reverse order, 90 00:05:12.670 --> 00:05:16.580 as you can see in that example where the words were deliberately typed in reverse. 91 00:05:16.580 --> 00:05:17.870 It was that one line, 92 00:05:17.870 --> 00:05:20.680 we managed to get it to actually appear as Python is a very powerful language. 93 00:05:20.680 --> 00:05:24.420 So applying this last finagly step to an existing range is also, 94 00:05:24.420 --> 00:05:27.130 is a very simple way to process that range in reverse order. 95 00:05:27.130 --> 00:05:32.325 And as another example, r = range(0, 96 00:05:32.325 --> 00:05:36.631 10), for i in r[::-1], 97 00:05:36.631 --> 00:05:40.796 print(i), and we can do that. 98 00:05:40.796 --> 00:05:42.170 And we get a similar result. 99 00:05:42.170 --> 00:05:45.550 We get it reverse, so we get the results of the range respectively reversed 100 00:05:45.550 --> 00:05:49.610 from 9 to 0, in the same way that when we actually printed the string, 101 00:05:49.610 --> 00:05:51.340 we actually got the results backward as well. 102 00:05:51.340 --> 00:05:54.190 So that's actually it, so it's now time for a challenge. 103 00:05:54.190 --> 00:05:56.430 So, the challenge is gonna be a little bit different this time. 104 00:05:56.430 --> 00:05:59.590 It's gonna get you to, well I'm gonna type in what to do, but 105 00:05:59.590 --> 00:06:03.470 it's more a bit of experimentation required on your behalf. 106 00:06:03.470 --> 00:06:05.254 So basically I want you to experiment. 107 00:06:08.380 --> 00:06:11.688 With different ranges, 108 00:06:11.688 --> 00:06:17.046 in slices to get a feel for how they work. 109 00:06:31.921 --> 00:06:35.733 So basically the challenge is not so much a challenge, it's more a way, 110 00:06:35.733 --> 00:06:40.046 it's more of a request to actually get you to experiment with different ranges and 111 00:06:40.046 --> 00:06:40.960 slices. 112 00:06:40.960 --> 00:06:42.920 The idea is to get a feel for how they're gonna work. 113 00:06:42.920 --> 00:06:46.320 So remember that you can actually print the range as well as iterating through it 114 00:06:46.320 --> 00:06:47.480 to print the values. 115 00:06:47.480 --> 00:06:49.790 And also you can actually check that also checks that you, 116 00:06:49.790 --> 00:06:52.040 the range is actually what you expected. 117 00:06:52.040 --> 00:06:54.740 Because as you can see in some circumstances, as I've shown in this 118 00:06:54.740 --> 00:06:57.850 video, ranges don't always come back as you would think they would. 119 00:06:57.850 --> 00:07:00.830 And you might also want to include things like I've shown there, 120 00:07:00.830 --> 00:07:04.410 o = range(0, 100, 4), printing out for contents of o, and 121 00:07:04.410 --> 00:07:09.580 actually using the slice with the colon colon 5 I have there on line 8, 122 00:07:09.580 --> 00:07:13.750 printing out the values of p, and using a for i in p and printing out i. 123 00:07:13.750 --> 00:07:16.650 And basically see if you can work out what this code is going to do and 124 00:07:16.650 --> 00:07:19.200 what's gonna be printed before you actually run the program. 125 00:07:19.200 --> 00:07:21.900 Now if you're unsure what's gonna happen, you can actually use for 126 00:07:21.900 --> 00:07:26.450 loop to print out the values of o to see why p actually returns what it does. 127 00:07:26.450 --> 00:07:27.440 We're gonna actually run this, and 128 00:07:27.440 --> 00:07:29.980 then I'll leave this with you to actually experiment with. 129 00:07:29.980 --> 00:07:33.980 This is one of those things, range is where it becomes a lot more familiar and 130 00:07:33.980 --> 00:07:37.040 makes more sense once you've actually spent some time testing it and 131 00:07:37.040 --> 00:07:38.020 going through it. 132 00:07:38.020 --> 00:07:40.310 So, let's just run this result or run this little program. 133 00:07:40.310 --> 00:07:42.830 And there's the results that it printed out, which may or 134 00:07:42.830 --> 00:07:44.430 may not be what you thought it would be. 135 00:07:44.430 --> 00:07:47.340 All right, so I'm going to actually leave this video here now, 136 00:07:47.340 --> 00:07:49.570 leave it to do some experimentation. 137 00:07:49.570 --> 00:07:52.780 In the next video, we're gonna talk about tuples or tuples, depending on how you 138 00:07:52.780 --> 00:07:57.730 pronounce it, which is something that is a very, very useful concept in Python.