WEBVTT 1 00:00:02.220 --> 00:00:04.930 So now's actually a good time in the course to actually discuss a very 2 00:00:04.930 --> 00:00:06.290 important feature of Python. 3 00:00:06.290 --> 00:00:10.000 And that's actually iterators and iterable objects. 4 00:00:10.000 --> 00:00:14.360 Now an iterator is an object that represents a stream of a data. 5 00:00:14.360 --> 00:00:15.850 So it actually returns the stream, 6 00:00:15.850 --> 00:00:19.080 or the actual data in the stream, one element at a time. 7 00:00:19.080 --> 00:00:24.460 Now, any object that supports iteration is called iterable, and we've actually seen 8 00:00:24.460 --> 00:00:28.490 two iterable objects already so far in the course, and that's strings and lists. 9 00:00:28.490 --> 00:00:30.360 So, as we've seen I'm gonna type in some code. 10 00:00:30.360 --> 00:00:34.310 We can actually use a for loop to iterate over each of the characters in a string. 11 00:00:34.310 --> 00:00:35.616 So let's actually type that out. 12 00:00:38.050 --> 00:00:41.207 We're gonna use string 1234567890, and 13 00:00:41.207 --> 00:00:45.230 you see this a few times now in the course, something similar. 14 00:00:45.230 --> 00:00:46.578 For char in the string. 15 00:00:46.578 --> 00:00:49.819 Print char. 16 00:00:49.819 --> 00:00:55.860 And if we actually run that, we actually get the results we can see on the screen. 17 00:00:55.860 --> 00:01:00.390 But what's actually happening behind the scenes is that there's an iterator that's 18 00:01:00.390 --> 00:01:01.762 created from the string. 19 00:01:01.762 --> 00:01:05.298 And the for loop actually uses that iterator to return each item 20 00:01:05.298 --> 00:01:07.837 of the string, or each element in this case. 21 00:01:07.837 --> 00:01:10.810 1,2, 3, 4, 5, 6, 7, 8, 9, and 0. 22 00:01:10.810 --> 00:01:14.660 And when there are no more items the iterator returns an error and the for 23 00:01:14.660 --> 00:01:16.570 loop actually terminates. 24 00:01:16.570 --> 00:01:19.810 So, note in this case that the for loop handles the error, and that's why you 25 00:01:19.810 --> 00:01:22.560 haven't actually seen an error when you're actually running the code. 26 00:01:22.560 --> 00:01:26.180 What we'll do is we'll actually create an iterator from the string 27 00:01:26.180 --> 00:01:28.820 rather than letting the for loop create one implicitly, 28 00:01:28.820 --> 00:01:31.590 and that's gonna be easier to see what's going on when we do that. 29 00:01:31.590 --> 00:01:33.370 So use the iter function to do that, so 30 00:01:33.370 --> 00:01:35.140 what I'm gonna do is just comment out the for loop. 31 00:01:36.880 --> 00:01:38.440 And what we're going to do is put my and 32 00:01:38.440 --> 00:01:43.310 the school interrator, equals hitter, and that's the function. 33 00:01:43.310 --> 00:01:47.160 And then we pass the name, in this case, the string variable that we want, and 34 00:01:47.160 --> 00:01:51.130 if we can actually do a print, my interrator, so if we actually do that and 35 00:01:51.130 --> 00:01:51.800 we actually run that. 36 00:01:52.960 --> 00:01:55.590 We actually get a string iterator object showing on the screen. 37 00:01:55.590 --> 00:01:59.390 Now this is actually just showing the object representation, 38 00:01:59.390 --> 00:02:01.740 supposedly a memory allocation of where that is, 39 00:02:01.740 --> 00:02:05.930 but to actually see it in use, we can do something like this. 40 00:02:05.930 --> 00:02:10.796 Print next my iterator and then print next 41 00:02:10.796 --> 00:02:15.810 my iterator the rest you do that and run it. 42 00:02:17.530 --> 00:02:20.520 We get the values one and two as you can see on the screen there so 43 00:02:20.520 --> 00:02:23.780 it's actually going through one element at a time and that's what the next did, 44 00:02:23.780 --> 00:02:25.610 it actually returned the first element. 45 00:02:25.610 --> 00:02:28.433 Next again and then return the second element, and so on and so forth. 46 00:02:28.433 --> 00:02:32.185 So basically each time that we use next, in my iterator, 47 00:02:32.185 --> 00:02:35.874 we're passing the iterator that we created in line 5. 48 00:02:35.874 --> 00:02:38.040 That's what it's doing, it's actually going through the list. 49 00:02:38.040 --> 00:02:41.470 And obviously again getting back to the for loop, that was doing that for 50 00:02:41.470 --> 00:02:42.690 us essentially behind the scenes. 51 00:02:42.690 --> 00:02:46.005 So what we're doing its actually created the code that more less is happening 52 00:02:46.005 --> 00:02:46.880 behind the scenes. 53 00:02:46.880 --> 00:02:49.831 So if we wanted to go right through to the end we could actually create some more. 54 00:02:49.831 --> 00:02:54.663 So we've got ten entries, so if we have another 8, 1, 2, 3, 4, 5, 6, 55 00:02:54.663 --> 00:02:59.642 7, 8, so that should actually be ten now, if we actually run that we actually get 56 00:02:59.642 --> 00:03:04.250 all the elements, 1 through to 0 essentially, so you can see there. 57 00:03:04.250 --> 00:03:06.780 So that's the exact number of elements that are in the string. 58 00:03:06.780 --> 00:03:08.760 But if we actually now, add actually one more. 59 00:03:08.760 --> 00:03:12.370 So in other words, we know now that we're already at the end of the string, 60 00:03:12.370 --> 00:03:14.980 let's actually see what happens when we try to go beyond that. 61 00:03:14.980 --> 00:03:16.900 So we actually run that. 62 00:03:16.900 --> 00:03:18.800 And you can see, we actually get an error. 63 00:03:18.800 --> 00:03:21.220 And if we just expand it out so we can see it. 64 00:03:21.220 --> 00:03:25.110 You can it manage to go through and count through to zero successfully and than it 65 00:03:25.110 --> 00:03:30.550 comes off and basically say, a trace back call last and stop it's duration. 66 00:03:30.550 --> 00:03:32.950 And that's basically line 18. 67 00:03:32.950 --> 00:03:36.480 Which has, as expected, crashed because there weren't any more elements. 68 00:03:36.480 --> 00:03:39.050 So, once we've covered error handling in the future, 69 00:03:39.050 --> 00:03:42.070 if you have nothing better to do, then you could actually write all your four loops 70 00:03:42.070 --> 00:03:45.280 by explicitly requesting an iterator using the enter function. 71 00:03:45.280 --> 00:03:47.280 And calling next until you get an error, and this is, 72 00:03:47.280 --> 00:03:50.990 again, exactly what the four loop does for us, it actually does it automatically. 73 00:03:50.990 --> 00:03:55.140 It keeps using next until it sees the stop iteration i error 74 00:03:55.140 --> 00:03:56.910 which is this area here on the screen. 75 00:03:56.910 --> 00:03:59.848 When it sees that it actually terminates for us automatically. 76 00:03:59.848 --> 00:04:04.110 So before it implicitly creates an iterator from the iterable object 77 00:04:04.110 --> 00:04:05.190 that we want to iterate over. 78 00:04:05.190 --> 00:04:06.910 Now of course we don't have to do all that. 79 00:04:06.910 --> 00:04:10.800 But it's useful to actually understand how iterators work, and it'll also become even 80 00:04:10.800 --> 00:04:15.400 more useful when we come to creating our own iterable classes later in the course. 81 00:04:15.400 --> 00:04:18.210 Just to help consort like that, let's just close down the run we're doing. 82 00:04:19.540 --> 00:04:20.370 And comment out this code. 83 00:04:22.420 --> 00:04:23.390 Now paste that code in again. 84 00:04:24.750 --> 00:04:27.050 I just want you to consider these two loops here. 85 00:04:27.050 --> 00:04:29.770 Now let me just get the right indent levels going. 86 00:04:31.820 --> 00:04:33.840 Again, very important to get your indent levels correct. 87 00:04:33.840 --> 00:04:34.730 So consider these two. 88 00:04:34.730 --> 00:04:36.960 So the first one's just a standard for loop. 89 00:04:36.960 --> 00:04:39.761 The second one, for_char in iter String. 90 00:04:43.024 --> 00:04:45.388 So I consider both of these scenarios, 91 00:04:45.388 --> 00:04:48.530 they actually both do exactly the same thing. 92 00:04:48.530 --> 00:04:52.540 But this second example here, is what's actually happening in the first case. 93 00:04:52.540 --> 00:04:54.240 The for up here, 94 00:04:54.240 --> 00:04:59.510 it's implicitly creating an iterator from the iteratable string object. 95 00:04:59.510 --> 00:05:03.320 So basically if you're just putting for char in string, then essentially what's 96 00:05:03.320 --> 00:05:07.910 happening behind the scenes is Python is actually adding this iter to that string. 97 00:05:07.910 --> 00:05:10.790 So it was actually creating that iterator for us. 98 00:05:10.790 --> 00:05:12.570 So, it can be very useful again to know that and 99 00:05:12.570 --> 00:05:15.500 you'll be finding out more about iterators as we move through the course. 100 00:05:15.500 --> 00:05:18.010 So, let's just come up now with a mini challenge 101 00:05:18.010 --> 00:05:20.930 to help you understand iterators a bit more. 102 00:05:20.930 --> 00:05:23.051 I'm gonna top that out now. 103 00:05:36.193 --> 00:05:40.110 So here is the mini challenge we've got for the writers. 104 00:05:40.110 --> 00:05:43.520 So create a list of items and you can use either strings or 105 00:05:43.520 --> 00:05:46.210 numbers in the list, it doesn't really matter, and then from that 106 00:05:46.210 --> 00:05:49.980 create an iterater using the iter function that we actually went through. 107 00:05:49.980 --> 00:05:52.890 Now, next use a for loop to loop n times. 108 00:05:52.890 --> 00:05:55.510 Now, n is the number of items in your list and 109 00:05:55.510 --> 00:05:59.630 each time around the loop is next on your list to print the next item. 110 00:05:59.630 --> 00:06:01.660 And just as a hint there, use the len function, 111 00:06:01.660 --> 00:06:05.970 we've talked about previously, rather than counting the number of items in the list. 112 00:06:05.970 --> 00:06:09.960 So, go away and see if you can figure that out, and type up some code and 113 00:06:09.960 --> 00:06:11.750 get that working, and when you're ready to come back and 114 00:06:11.750 --> 00:06:14.080 compare your code to mine, I'll be here waiting. 115 00:06:14.080 --> 00:06:16.120 So pause the video now, and I'll see you when you get back. 116 00:06:19.405 --> 00:06:20.380 So how did you get on? 117 00:06:20.380 --> 00:06:22.605 Did you come up with a good solution for that? 118 00:06:22.605 --> 00:06:24.995 So let's go through with my implementation, and 119 00:06:24.995 --> 00:06:27.405 bearing in mind this is just a implementation. 120 00:06:27.405 --> 00:06:29.475 There's lot's of different ways to do these things. 121 00:06:29.475 --> 00:06:33.765 Let's come up and start by typing the lists or creating the lists. 122 00:06:33.765 --> 00:06:37.285 Which will be my_list =, and I'm gonna use just the days of the week. 123 00:06:37.285 --> 00:06:40.665 So Monday, Tuesday, 124 00:06:43.685 --> 00:06:47.368 Wednesday Thursday, 125 00:06:47.368 --> 00:06:52.416 Friday, Saturday, 126 00:06:52.416 --> 00:06:56.750 Sunday, like so. 127 00:06:56.750 --> 00:06:58.340 Next I'm gonna create the iterator. 128 00:06:58.340 --> 00:07:02.730 That's my iterator equals iter my_list 129 00:07:02.730 --> 00:07:06.660 the iter function creates that iterator, as we talked about. 130 00:07:06.660 --> 00:07:08.790 And then we're gonna go through using the four loop. 131 00:07:08.790 --> 00:07:14.514 So I'm gonna do four i in range zero, 132 00:07:14.514 --> 00:07:19.880 I'm using lan to get the number of elements from the list. 133 00:07:19.880 --> 00:07:20.770 Mine is called list. 134 00:07:23.110 --> 00:07:27.050 Remembering that the lead in this case on a list actually returns the number of 135 00:07:27.050 --> 00:07:29.580 elements that are actually existing in the list itself. 136 00:07:29.580 --> 00:07:31.120 So, that's the for loop. 137 00:07:31.120 --> 00:07:37.170 And then we're gonna use next underscore item equals equals next my iterator so 138 00:07:37.170 --> 00:07:41.050 we're iterating through one element at a time, so that's giving us the next 139 00:07:41.050 --> 00:07:44.590 element, or the next item, in this case a string from the list. 140 00:07:44.590 --> 00:07:46.730 And we're going to print that out print next item. 141 00:07:46.730 --> 00:07:50.380 So that's the solution, so 142 00:07:50.380 --> 00:07:54.660 if we actually run that to make sure that it's working we can see we get Monday, 143 00:07:54.660 --> 00:07:57.310 Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday. 144 00:07:57.310 --> 00:07:59.270 And we can see that it's actually working fine. 145 00:07:59.270 --> 00:08:02.090 So the idea of this challenge was to help you understand iteratives. 146 00:08:02.090 --> 00:08:05.161 Now of course you wouldn't want to access the items in the list 147 00:08:05.161 --> 00:08:06.293 in that way normally. 148 00:08:06.293 --> 00:08:11.171 You'd probably want to do something a lot simpler like you've learned earlier 149 00:08:11.171 --> 00:08:15.681 that in the course, like four i in minor's called a list Print(i), and 150 00:08:15.681 --> 00:08:18.620 obviously that would return the same results. 151 00:08:21.580 --> 00:08:22.620 Oops, I forgot a call in there. 152 00:08:23.810 --> 00:08:27.710 And then again, that obviously returns the same result. 153 00:08:27.710 --> 00:08:32.610 But the point again here was to teach you what iterators are and how to use them. 154 00:08:32.610 --> 00:08:37.360 And as mentioned, later in the course we'll actually be creating our own classes 155 00:08:37.360 --> 00:08:39.060 that use iterable objects. 156 00:08:39.060 --> 00:08:41.250 And you'll be understanding a little bit more about iterators then. 157 00:08:41.250 --> 00:08:42.140 So I hope you enjoyed that. 158 00:08:42.140 --> 00:08:46.470 And then, next video, we're gonna actually start looking in more detail at ranges, 159 00:08:46.470 --> 00:08:48.360 which I've used a few times already in the course. 160 00:08:48.360 --> 00:08:49.700 So, I'll see you in that next video.