WEBVTT 1 00:00:04.730 --> 00:00:06.950 Alright so moving on now with the 2 00:00:06.950 --> 00:00:08.870 example that we started in the previous 3 00:00:08.870 --> 00:00:11.809 video, so a generator works like an 4 00:00:11.809 --> 00:00:14.089 iterator and what we can do is we can 5 00:00:14.089 --> 00:00:16.000 actually use next to get the next value 6 00:00:16.000 --> 00:00:18.320 instead of using it in a loop and 7 00:00:18.320 --> 00:00:20.180 obviously we use it in a loop in the 8 00:00:20.180 --> 00:00:22.460 previous video, so what we're going to do 9 00:00:22.460 --> 00:00:24.529 to see that working, we'll come up here to 10 00:00:24.529 --> 00:00:27.590 line 17 and actually before line 17, 11 00:00:27.590 --> 00:00:29.059 we're going to put a print statement in 12 00:00:29.059 --> 00:00:31.880 there, so let's go ahead and do that so 13 00:00:31.880 --> 00:00:36.469 in print in parenthesis next parenthesis 14 00:00:36.469 --> 00:00:40.700 big underscore range, so what we're 15 00:00:40.700 --> 00:00:42.900 going to do now is delete this line 12 16 00:00:42.900 --> 00:00:43.730 because we don't need that anymore 17 00:00:43.730 --> 00:00:47.390 and by doing that effectively line 15 18 00:00:47.390 --> 00:00:49.040 becomes line 14, so I'm just going to 19 00:00:49.040 --> 00:00:50.570 change the reference to laps so it's 20 00:00:50.570 --> 00:00:53.030 clearly our code and of course that code 21 00:00:53.030 --> 00:00:54.440 we deleted was only there to show that 22 00:00:54.440 --> 00:00:56.450 my underscore range wasn't called by 23 00:00:56.450 --> 00:00:58.850 the assignment on line 14. Alright, so 24 00:00:58.850 --> 00:01:02.239 let's actually run the code and we can 25 00:01:02.239 --> 00:01:04.339 see that it stopped on line 14, as we 26 00:01:04.339 --> 00:01:06.770 would expect it to do. So the next line 27 00:01:06.770 --> 00:01:08.929 to execute should be our call to next 28 00:01:08.929 --> 00:01:11.359 which is count on line 16 and 29 00:01:11.359 --> 00:01:13.310 that gets the next value from our 30 00:01:13.310 --> 00:01:15.619 generator. Now as we haven't used to 31 00:01:15.619 --> 00:01:17.389 generating yet the next value is 32 00:01:17.389 --> 00:01:20.060 currently the first value, so in other 33 00:01:20.060 --> 00:01:22.429 words we should get zero printed here, 34 00:01:22.429 --> 00:01:25.490 so press enter and you can see we do get 35 00:01:25.490 --> 00:01:28.819 zero returned as well as the message 36 00:01:28.819 --> 00:01:31.249 that my underscore range starts and 37 00:01:31.249 --> 00:01:34.009 we're now paused on line 22. So at this 38 00:01:34.009 --> 00:01:35.420 point before we continue, I've actually 39 00:01:35.420 --> 00:01:37.670 got a mini challenge for you, I'm not 40 00:01:37.670 --> 00:01:39.770 going to use a slide so you can actually 41 00:01:39.770 --> 00:01:41.450 see the code which is on the screen at 42 00:01:41.450 --> 00:01:43.670 the moment so at this point what will be 43 00:01:43.670 --> 00:01:46.249 the first value append it to bigger 44 00:01:46.249 --> 00:01:50.119 than school list on line 25? So what we 45 00:01:50.119 --> 00:01:51.590 the first value appended to big 46 00:01:51.590 --> 00:01:54.649 underscore list on line 25 given that we've 47 00:01:54.649 --> 00:01:56.840 already had a call to the mu underscore 48 00:01:56.840 --> 00:01:58.909 range function. So pause the video, have a 49 00:01:58.909 --> 00:02:00.139 think about that and see if you can 50 00:02:00.139 --> 00:02:01.729 figure it out and come back and we'll go 51 00:02:01.729 --> 00:02:03.850 through the answers. 52 00:02:03.850 --> 00:02:06.490 Alright so let's see what happens when 53 00:02:06.490 --> 00:02:08.700 I press enter the static we can delete 54 00:02:08.700 --> 00:02:10.869 and you can see my underscore ranges 55 00:02:10.869 --> 00:02:13.510 returning one and we're in line 24 the 56 00:02:13.510 --> 00:02:16.780 inside loop, so the first value is going 57 00:02:16.780 --> 00:02:18.880 to be added to the list is 1 and that's 58 00:02:18.880 --> 00:02:20.739 because we've already consumed the first 59 00:02:20.739 --> 00:02:22.750 value from our generator when we did 60 00:02:22.750 --> 00:02:24.550 that when we asked for the next value on 61 00:02:24.550 --> 00:02:27.880 line 16. Now if you recall the discussion 62 00:02:27.880 --> 00:02:31.480 of iterators in section 7, a for loop is 63 00:02:31.480 --> 00:02:34.420 just calling next for us. So each time I 64 00:02:34.420 --> 00:02:37.180 press enter now the for loop is 65 00:02:37.180 --> 00:02:39.459 effectively calling next to get the next 66 00:02:39.459 --> 00:02:41.830 value. Now because the first values 67 00:02:41.830 --> 00:02:43.569 already been returned before the loop 68 00:02:43.569 --> 00:02:46.440 started, I just do this and finish it off. 69 00:02:46.440 --> 00:02:49.090 We actually only go around four times as 70 00:02:49.090 --> 00:02:51.100 you can see there, and the list is 71 00:02:51.100 --> 00:02:52.900 showing the output only one two three 72 00:02:52.900 --> 00:02:54.340 and four in this case instead of the 73 00:02:54.340 --> 00:02:57.100 zero which it showed last time prior to 74 00:02:57.100 --> 00:02:58.650 our changing the code to call the next 75 00:02:58.650 --> 00:03:02.200 function on line 16. So if you worked out 76 00:03:02.200 --> 00:03:03.069 that that's what would happen 77 00:03:03.069 --> 00:03:05.440 congratulations and well done. Now that 78 00:03:05.440 --> 00:03:07.360 also explains why it can be a bad idea 79 00:03:07.360 --> 00:03:10.829 to assign a generator to a variable 80 00:03:10.829 --> 00:03:13.480 that's as we've currently got on line 13 81 00:03:13.480 --> 00:03:16.360 big underscore range equals 82 00:03:16.360 --> 00:03:19.600 1 underscore range 5 in parentheses. 83 00:03:19.600 --> 00:03:20.890 Alright so at this point we've exhausted 84 00:03:20.890 --> 00:03:23.739 all the values returned from our minor 85 00:03:23.739 --> 00:03:25.780 score range generator, so we come ahead 86 00:03:25.780 --> 00:03:27.609 and add some extra code to the program 87 00:03:27.609 --> 00:03:29.650 and why don't you do anything? So what 88 00:03:29.650 --> 00:03:31.720 I'm going to do at this point is comment 89 00:03:31.720 --> 00:03:33.790 out all those input lines so we don't 90 00:03:33.790 --> 00:03:35.650 have to press enter moving forward, 91 00:03:35.650 --> 00:03:41.980 so let's go and do that and I'll indent 92 00:03:41.980 --> 00:03:44.410 that one. Alright and there's also one on 93 00:03:44.410 --> 00:03:46.329 line 22, we'll comment it out as well. 94 00:03:46.329 --> 00:03:48.370 Right now we've done that what I'm going 95 00:03:48.370 --> 00:03:50.799 to do is add some code I'm going to add 96 00:03:50.799 --> 00:03:52.989 some code after our prints write down 97 00:03:52.989 --> 00:03:54.549 the bottom of the program so that's 98 00:03:54.549 --> 00:03:56.320 going to do that after our print 99 00:03:56.320 --> 00:03:58.540 big underscore list, so line 30 100 00:03:58.540 --> 00:04:01.500 what I'm going to do print double quotes 101 00:04:01.500 --> 00:04:06.580 looping again or not 102 00:04:06.580 --> 00:04:09.410 then we're going to do for i in big 103 00:04:09.410 --> 00:04:10.480 underscore range 104 00:04:10.480 --> 00:04:14.270 colon then print parentheses double 105 00:04:14.270 --> 00:04:16.970 quote i is then left the right curly 106 00:04:16.970 --> 00:04:24.020 braces dot format i. Alright so if I run 107 00:04:24.020 --> 00:04:27.290 that program now, you can see that 108 00:04:27.290 --> 00:04:29.060 nothing happens in that final for 109 00:04:29.060 --> 00:04:31.940 looping again or not this print isn't 110 00:04:31.940 --> 00:04:33.620 executed there's nothing showing on the 111 00:04:33.620 --> 00:04:34.850 screen and the program has actually 112 00:04:34.850 --> 00:04:38.150 exited. So storing my underscore range 5 113 00:04:38.150 --> 00:04:40.820 in big range is fine, if you want to call 114 00:04:40.820 --> 00:04:43.430 next each time you want a new value but 115 00:04:43.430 --> 00:04:45.830 it isn't a good idea if you intend using 116 00:04:45.830 --> 00:04:47.600 it again in a for loop. So in that case 117 00:04:47.600 --> 00:04:50.270 you should just use the - core range 5 118 00:04:50.270 --> 00:04:53.510 method function call direct again, 119 00:04:53.510 --> 00:04:54.950 so we'd go ahead and change that then, 120 00:04:54.950 --> 00:04:57.050 instead of for i in big underscore 121 00:04:57.050 --> 00:05:00.639 range, we change that for i in my 122 00:05:00.639 --> 00:05:02.840 underscore range then five in 123 00:05:02.840 --> 00:05:04.070 parentheses which is the number we want 124 00:05:04.070 --> 00:05:06.050 to pass to it. So we're calling the 125 00:05:06.050 --> 00:05:09.139 function direct, run that again and you 126 00:05:09.139 --> 00:05:10.880 can see in this case now we're correctly 127 00:05:10.880 --> 00:05:12.470 getting the output because we're not 128 00:05:12.470 --> 00:05:14.360 using a variable rather were you're 129 00:05:14.360 --> 00:05:16.669 actually using the function call. Alright, 130 00:05:16.669 --> 00:05:17.900 so the output is now correct and 131 00:05:17.900 --> 00:05:20.000 returning the right values, now before we 132 00:05:20.000 --> 00:05:21.350 finish this video I should mention that 133 00:05:21.350 --> 00:05:24.440 range does work if we store it so I'm 134 00:05:24.440 --> 00:05:26.150 going to undo that last change that we 135 00:05:26.150 --> 00:05:31.280 added then back to to big underscore 136 00:05:31.280 --> 00:05:34.120 range then we're going to go ahead and 137 00:05:34.120 --> 00:05:37.220 re-enable the code I commented out on line 138 00:05:37.220 --> 00:05:40.360 12, big underscore range equals range 5. 139 00:05:40.360 --> 00:05:43.580 Now go ahead and do that then I'm going 140 00:05:43.580 --> 00:05:46.130 to comment out the call to our function 141 00:05:46.130 --> 00:05:49.430 like so and now that I've done that, 142 00:05:49.430 --> 00:05:52.430 I want to actually delete this call and 143 00:05:52.430 --> 00:05:55.340 this line 16, because we don't want to 144 00:05:55.340 --> 00:05:56.750 call next on the range object, so I'm 145 00:05:56.750 --> 00:05:59.240 going to delete that as well and then 146 00:05:59.240 --> 00:06:02.870 when you run it, you can see that in both 147 00:06:02.870 --> 00:06:05.330 cases that's working fine and the 148 00:06:05.330 --> 00:06:07.729 range class behaves like an interval in 149 00:06:07.729 --> 00:06:09.260 other words it's reset each time it's 150 00:06:09.260 --> 00:06:11.360 used which wasn't the case when you used 151 00:06:11.360 --> 00:06:13.669 a variable that actually called the 152 00:06:13.669 --> 00:06:15.860 function that we actually wrote, so again 153 00:06:15.860 --> 00:06:17.690 the range class behaves like an iterable 154 00:06:17.690 --> 00:06:19.320 each time it's call, 155 00:06:19.320 --> 00:06:21.420 it's actually reset. Now here my 156 00:06:21.420 --> 00:06:23.010 underscore range isn't really very 157 00:06:23.010 --> 00:06:24.810 useful other than to show how to write a 158 00:06:24.810 --> 00:06:27.270 generator, so in the next video we're 159 00:06:27.270 --> 00:06:28.200 going to look at some more useful 160 00:06:28.200 --> 00:06:32.300 generators so we'll see you in that next video.