WEBVTT 00:03.320 --> 00:09.110 Welcome in his lecture, I'm going to talk about how to shrink a slice using slice expressions. 00:09.430 --> 00:10.910 Let's talk about how they work. 00:11.830 --> 00:17.500 Before talking about the expressions, let me tell you first what a sliceable value is. 00:18.370 --> 00:26.380 A sliceable value is any value that can be sliced using a slice expression, a slice value on an array 00:26.380 --> 00:26.870 value. 00:26.950 --> 00:30.580 Both are sliceable values because you can slice them. 00:31.180 --> 00:34.780 A shrink value is also a sliceable value, by the way. 00:34.810 --> 00:38.800 It is because a street value is a bitts lies behind the scenes. 00:39.310 --> 00:39.670 All right. 00:39.880 --> 00:42.850 Now let's take a look at what a slice expression looks like. 00:43.660 --> 00:45.970 First, I need to type which value? 00:45.970 --> 00:46.810 I want a slice. 00:48.080 --> 00:51.060 Then I need to type square brackets with a column. 00:52.240 --> 00:58.390 Then I need to give it a starting position, and lastly, I need to give it a stopping position, this 00:58.390 --> 01:04.540 expression Salinas's the sliceable from the starting position until to the top position. 01:05.080 --> 01:09.630 The starting position should be the index of an element inside the slice. 01:09.640 --> 01:13.410 So it will start at zero because indexes start from zero. 01:13.810 --> 01:20.170 On the other hand, the stop position should describe the position of an element, not its index. 01:20.530 --> 01:23.120 So it should start from one instead of zero. 01:23.740 --> 01:29.830 Actually, this is an index too, but it is easier to think of it as the element position instead. 01:29.860 --> 01:37.240 OK, so in the end these expressions will return a new value that describes the slightest segment of 01:37.240 --> 01:38.380 the original slice. 01:40.180 --> 01:42.670 All right, let's take a look at a few examples. 01:44.480 --> 01:45.620 He was a less. 01:46.960 --> 01:49.570 This creates a slice that looks like this. 01:50.380 --> 01:51.850 Are you ready to slice it? 01:52.640 --> 01:53.620 OK, let's do it. 01:57.670 --> 02:04.930 This last expression starts the slicing from index zero to the first element, in the end, it returns 02:04.930 --> 02:07.620 a new slice value that looks like this. 02:08.080 --> 02:11.890 These are different slices, sourceless expression returns. 02:11.890 --> 02:14.780 You are newsless value using the original slice. 02:15.150 --> 02:15.400 OK. 02:16.470 --> 02:22.680 Now let's list the original slice up to its second ealand, these returns, a new slice value with the 02:22.680 --> 02:24.960 first two elements of the original slice. 02:25.610 --> 02:29.810 As you can see, the start position describes an index inside the slice. 02:30.420 --> 02:37.140 However, the opposition describes the elements position, so it slices the slice, starting from its 02:37.140 --> 02:39.510 first index up to its second element. 02:40.200 --> 02:42.640 Let's slice it up to its third element. 02:42.660 --> 02:42.990 Now. 02:43.910 --> 02:49.010 This returns a new value with the first three elements of the original slice. 02:49.880 --> 02:53.140 Now let's slice it up to its fourth element like this. 02:54.050 --> 02:58.700 This returns a new slice with the first four elements of the original slice. 02:59.090 --> 03:02.340 Now, let's slice it up to its last element like this. 03:03.080 --> 03:08.000 This creates a similar slice, by the way, round slicing from the first element. 03:08.120 --> 03:10.310 You can throw it away like this. 03:11.330 --> 03:18.080 And when slicing up to the last element, you can also throw it away like this, so this soulless expression 03:18.080 --> 03:19.390 is equal to this. 03:20.150 --> 03:25.190 So whenever you see one of the positions are missing now, you know what they are equal to. 03:25.580 --> 03:27.540 Let's slice it beyond its length. 03:28.160 --> 03:28.970 Can I do that? 03:29.640 --> 03:30.190 Let's see. 03:30.830 --> 03:31.760 It doesn't work. 03:32.000 --> 03:39.440 There is a runtime error slice bounce out of range, so you cannot slice a slice beyond its length. 03:39.710 --> 03:44.360 OK, by the way, you don't have to slice a slice beginning from its first element. 03:44.360 --> 03:47.210 Of course you can slice it however you like. 03:47.690 --> 03:51.620 Let's slice it by admitting the first and the last elements like this. 03:52.660 --> 03:55.360 This will create a new value like this one. 03:56.370 --> 04:02.580 Or you can slice it up to the last element like so as I said, I don't need to type the stop position 04:02.580 --> 04:05.270 because it is equal to the last element by default. 04:06.120 --> 04:10.630 So this sless expansion is equal to this sless expression on the right. 04:11.190 --> 04:12.000 Here's the question. 04:12.300 --> 04:17.100 What would happen if I opened to a sliced slice value? 04:18.170 --> 04:24.200 First, I'm going to slice it up, these fourth elements like this, so it creates a new slice with 04:24.200 --> 04:24.950 four elements. 04:26.380 --> 04:32.650 Now I'm going to appoint an exclamation mark to the new slice, by the way, these exclamation mark 04:32.650 --> 04:34.070 is not important here. 04:34.540 --> 04:36.790 It could have been any other character as well. 04:37.420 --> 04:38.770 Now let's see what happens. 04:39.250 --> 04:42.040 It returns a new slice by adding the new element. 04:42.400 --> 04:49.090 It does so because the new slices lacked for so it pants the new elements right after the length of 04:49.090 --> 04:50.890 the slice as the fifth element. 04:51.520 --> 04:53.030 But this has a side effect. 04:53.050 --> 04:55.580 So let's check out the last element of the original slice. 04:55.620 --> 04:57.330 Now it will disappear. 04:58.350 --> 05:04.980 And instead, it gets replaced with the new element, so the apparent function here or writes the original 05:04.990 --> 05:12.720 slice, too, if Einstein has seen this, he will call it the spooky action at a distance, I call it 05:12.900 --> 05:19.620 it's expected is because the new slice and the original slice are connected behind the scenes. 05:20.250 --> 05:27.060 I'll talk about this in detail in the next lecture series so there is more to the slices than meets 05:27.060 --> 05:27.480 the eye. 05:28.140 --> 05:28.650 All right. 05:28.650 --> 05:30.600 Let's take a look at our coding example. 05:31.730 --> 05:37.560 Let's say you have fetched search results from a database or from a public API, and you'll receive 05:37.560 --> 05:38.960 the slice such as this one. 05:39.530 --> 05:42.510 All right, let me print it using my predecessor library. 05:43.160 --> 05:45.970 By the way, don't forget importing the package here. 05:46.580 --> 05:47.020 All right. 05:47.450 --> 05:50.480 These are the elements inside the items list. 05:50.480 --> 05:53.250 Last item for the top three elements. 05:53.600 --> 05:55.900 First, I'm going to declare a newsless variable. 05:56.450 --> 06:01.580 Then I'm going to slice the items beginning from the first index to the third element like this. 06:02.220 --> 06:05.540 Remember, slicing returns, a new slice value. 06:05.900 --> 06:07.940 So I can say it in this variable. 06:08.750 --> 06:12.400 By the way, as I said, they're slicing from the first index. 06:12.410 --> 06:13.820 You can drop it like this. 06:14.390 --> 06:15.410 They also print it. 06:17.080 --> 06:19.790 Here are the top three items from the original slice. 06:20.410 --> 06:22.930 Now, let's slice it for the last four items. 06:23.620 --> 06:29.080 To do that, I need to slice it from the 9th index to the last item like this. 06:29.690 --> 06:36.820 Remember, the items slice has 13 elements, so its last element is at the 13th position. 06:37.540 --> 06:40.660 I can also drop the last element position like this. 06:41.090 --> 06:42.130 Let me also print it. 06:43.850 --> 06:45.020 All right, limerence. 06:46.270 --> 06:53.880 Cool, it has sliced the last four items, by the way, round slicing, I can also use a variable first. 06:54.190 --> 06:58.170 I'm going to say the length of the items sliced to the variable. 06:58.780 --> 07:04.360 Then I'm going to get the last four items by replacing the nine here with the L minus four. 07:05.390 --> 07:10.700 And minus four is equal to nine, so this expression is equal to the previous one. 07:10.730 --> 07:11.060 OK. 07:12.600 --> 07:14.460 As you can see, the result is the same. 07:15.550 --> 07:21.550 Let's take a look at what happens if I slice the last four slice again, for example, let me get its 07:21.550 --> 07:26.410 elements at the middle, starting from the first index to the third item like this. 07:27.080 --> 07:27.560 OK. 07:27.910 --> 07:28.390 Print it. 07:30.030 --> 07:36.850 As you can see, you can slice a slice indefinitely, slicing your slice is called re slicing. 07:37.170 --> 07:41.600 OK, here, the last four slices indexes look interesting. 07:41.940 --> 07:48.330 For example, the defender element is at the zero index in the last slice, but it is at the ninth index 07:48.330 --> 07:49.530 in the item slice. 07:50.400 --> 07:56.250 Let me print the ninth index of the items slice and the first index of the last four slice. 07:57.850 --> 08:03.190 As you can see, both indexes point to the same element, but their indexes are different. 08:03.550 --> 08:06.460 You can also see this by looking at the indexes here. 08:07.060 --> 08:13.270 So in summary, the same elements come in different indexes in different slices, depending on their 08:13.270 --> 08:15.010 position in those slices. 08:15.430 --> 08:18.100 So indexes are relative to a slice like. 08:19.080 --> 08:25.500 Now I'm going to show you the differences between slicing and indexing, let me print the type and the 08:25.500 --> 08:26.780 values of the items. 08:27.910 --> 08:30.210 OK, let's slice it for the Tetris element. 08:30.390 --> 08:34.140 Tetris is that the second index, but in the third position. 08:34.350 --> 08:38.520 OK, now I'm going to get the Tetris element directly by using an index expression. 08:40.200 --> 08:45.480 As you can see, Selous expression returns a value, even it has a single element in it. 08:46.020 --> 08:49.720 However, the index expression returns the element value directly. 08:50.310 --> 08:56.800 So in summary, then use a slide expression even for a single item, the returned value will be a slice 08:56.820 --> 09:01.100 value, not an element value, but values an index expression. 09:01.110 --> 09:03.690 You will get an element value, not a slice value. 09:03.850 --> 09:06.640 OK, let me tell you one last thing. 09:06.660 --> 09:08.530 Men use a class expression. 09:08.550 --> 09:14.340 It returns a slice value with the same type of the original value, for example, here. 09:14.370 --> 09:19.530 That is why the items slice and the type of the returned values from this slice. 09:19.530 --> 09:21.090 Expressions are the same. 09:21.210 --> 09:23.540 They all are string slices. 09:24.010 --> 09:24.990 OK, all right. 09:25.140 --> 09:25.950 That's all for now. 09:26.220 --> 09:31.250 In the next lecture, I'm going to show you a pagination example using sliced expressions. 09:31.620 --> 09:32.600 So you there are.