1
00:00:05,440 --> 00:00:07,660
Alright, so moving on now with the

2
00:00:07,660 --> 00:00:09,580
example that we started in the previous

3
00:00:09,580 --> 00:00:12,510
video. So a generator works like an

4
00:00:12,510 --> 00:00:14,790
iterator, and what we can do is, we can

5
00:00:14,790 --> 00:00:16,710
actually use next to get the next value

6
00:00:16,710 --> 00:00:19,030
instead of using it in a loop, and

7
00:00:19,030 --> 00:00:20,890
obviously, we used it in a loop in the

8
00:00:20,890 --> 00:00:23,170
previous video. So what we're going to do

9
00:00:23,170 --> 00:00:25,230
to see that working, we'll come up here to

10
00:00:25,230 --> 00:00:28,300
line 17, and actually before line 17,

11
00:00:28,300 --> 00:00:29,760
we're going to put a print statement in

12
00:00:29,760 --> 00:00:32,590
there, so let's go ahead and do that. So

13
00:00:32,590 --> 00:00:37,170
we do print, parentheses next parentheses

14
00:00:37,170 --> 00:00:41,410
big underscore range. So what we're

15
00:00:41,410 --> 00:00:43,610
going to do now is delete this line 12

16
00:00:43,610 --> 00:00:44,440
because we don't need that anymore,

17
00:00:44,440 --> 00:00:48,100
and by doing that, effectively, line 15

18
00:00:48,100 --> 00:00:49,750
becomes line 14, so I'm just going to

19
00:00:49,750 --> 00:00:51,280
change the reference to that so it's

20
00:00:51,280 --> 00:00:53,740
clear in our code. And of course that code

21
00:00:53,740 --> 00:00:55,150
we deleted was only there to show that

22
00:00:55,150 --> 00:00:57,160
my underscore range wasn't called by

23
00:00:57,160 --> 00:00:59,560
the assignment on line 14. Alright, so

24
00:00:59,560 --> 00:01:02,940
let's actually run the code, and we can

25
00:01:02,940 --> 00:01:05,040
see that it stopped on line 14, as we

26
00:01:05,040 --> 00:01:07,480
would expect it to do. So the next line

27
00:01:07,480 --> 00:01:09,630
to execute should be our call to next,

28
00:01:09,630 --> 00:01:12,060
which is currently on line 16, and

29
00:01:12,060 --> 00:01:14,020
that gets the next value from our

30
00:01:14,020 --> 00:01:16,320
generator. Now as we haven't used the

31
00:01:16,320 --> 00:01:18,090
generator yet, the next value is

32
00:01:18,090 --> 00:01:20,770
currently the first value, so in other

33
00:01:20,770 --> 00:01:23,130
words we should get zero printed here.

34
00:01:23,130 --> 00:01:26,200
So press enter and you can see we do get

35
00:01:26,200 --> 00:01:29,520
zero returned, as well as the message

36
00:01:29,520 --> 00:01:31,950
that my underscore range starts and

37
00:01:31,950 --> 00:01:34,710
we're now paused on line 22. So at this

38
00:01:34,710 --> 00:01:36,130
point before we continue, I've actually

39
00:01:36,130 --> 00:01:38,380
got a mini challenge for you. I'm not

40
00:01:38,380 --> 00:01:40,480
going to use a slide, so you can actually

41
00:01:40,480 --> 00:01:42,160
see the code which is on the screen at

42
00:01:42,160 --> 00:01:44,380
the moment. So at this point, what will be

43
00:01:44,380 --> 00:01:46,950
the first value appended to big

44
00:01:46,950 --> 00:01:50,820
underscore list on line 25? So what will

45
00:01:50,820 --> 00:01:52,300
be the first value appended to big

46
00:01:52,300 --> 00:01:55,350
underscore list on line 25, given that we've

47
00:01:55,350 --> 00:01:57,550
already had a call to the my underscore

48
00:01:57,550 --> 00:01:59,610
range function. So pause the video, have a

49
00:01:59,610 --> 00:02:00,840
think about that and see if you can

50
00:02:00,840 --> 00:02:02,430
figure it out and come back and we'll go

51
00:02:02,430 --> 00:02:04,560
through the answer.

52
00:02:04,560 --> 00:02:07,200
Alright, so let's see what happens when

53
00:02:07,200 --> 00:02:09,410
I press enter to startexecuting the loop.  

54
00:02:09,410 --> 00:02:11,570
And you can see my underscore range is

55
00:02:11,570 --> 00:02:14,220
returning one, and we're in line 24, the

56
00:02:14,220 --> 00:02:17,490
inside loop. So the first value that's going

57
00:02:17,490 --> 00:02:19,590
to be added to the list is 1, and that's

58
00:02:19,590 --> 00:02:21,440
because we've already consumed the first

59
00:02:21,440 --> 00:02:23,460
value from our generator, and we did

60
00:02:23,460 --> 00:02:25,260
that when we asked for the next value on

61
00:02:25,260 --> 00:02:28,590
line 16. Now if you recall the discussion

62
00:02:28,590 --> 00:02:32,190
of iterators in section 7, a for loop is

63
00:02:32,190 --> 00:02:35,130
just calling next for us. So each time I

64
00:02:35,130 --> 00:02:37,890
press enter now, the for loop is

65
00:02:37,890 --> 00:02:40,160
effectively calling next to get the next

66
00:02:40,160 --> 00:02:42,540
value. Now because the first value's

67
00:02:42,540 --> 00:02:44,270
already been returned before the loop

68
00:02:44,270 --> 00:02:47,150
started - I just do this and finish it off -

69
00:02:47,150 --> 00:02:49,800
we actually only go around four times, as

70
00:02:49,800 --> 00:02:51,810
you can see there, and the list is

71
00:02:51,810 --> 00:02:53,610
showing the output, only one, two, three

72
00:02:53,610 --> 00:02:55,050
and four in this case, instead of the

73
00:02:55,050 --> 00:02:57,810
zero which it showed last time, prior to

74
00:02:57,810 --> 00:02:59,360
our changing the code to call the next

75
00:02:59,360 --> 00:03:02,910
function on line 16. So if you worked out

76
00:03:02,910 --> 00:03:03,770
that that's what would happen,

77
00:03:03,770 --> 00:03:06,150
congratulations and well done. Now that

78
00:03:06,150 --> 00:03:08,070
also explains why it can be a bad idea

79
00:03:08,070 --> 00:03:11,530
to assign a generator to a variable, and

80
00:03:11,530 --> 00:03:14,190
that's as we've currently got on line 13 - the

81
00:03:14,190 --> 00:03:17,070
big underscore range equals

82
00:03:17,070 --> 00:03:20,310
my underscore range, 5 in parentheses.

83
00:03:20,310 --> 00:03:21,600
Alright, so at this point we've exhausted

84
00:03:21,600 --> 00:03:24,440
all the values returned from our my

85
00:03:24,440 --> 00:03:26,490
underscore range generator. So if we go ahead

86
00:03:26,490 --> 00:03:28,310
and add some extra code to the program,

87
00:03:28,310 --> 00:03:30,360
it won't actually do anything. So what

88
00:03:30,360 --> 00:03:32,430
I'm going to do at this point, is comment

89
00:03:32,430 --> 00:03:34,500
out all those input lines so we don't

90
00:03:34,500 --> 00:03:36,360
have to press enter, moving forward,

91
00:03:36,360 --> 00:03:42,690
so let's go and do that, and I'll indent

92
00:03:42,690 --> 00:03:45,120
that one. Alright and there's also one on

93
00:03:45,120 --> 00:03:47,030
line 22 - we'll comment that out as well.

94
00:03:47,030 --> 00:03:49,080
Right now I've done that, what I'm going

95
00:03:49,080 --> 00:03:51,500
to do is add some code. I'm going to add

96
00:03:51,500 --> 00:03:53,690
some code after our prints, right down

97
00:03:53,690 --> 00:03:55,250
the bottom of the program. So let's

98
00:03:55,250 --> 00:03:57,030
going to do that, after our print

99
00:03:57,030 --> 00:03:59,250
big underscore list. So on line 31

100
00:03:59,250 --> 00:04:02,210
I'm going to do print double quotes

101
00:04:02,210 --> 00:04:07,290
looping again ... or not.

102
00:04:07,290 --> 00:04:10,120
Then we're going to do for i in big

103
00:04:10,120 --> 00:04:11,190
underscore range

104
00:04:11,190 --> 00:04:14,980
colon, then print parentheses double

105
00:04:14,980 --> 00:04:17,680
quote i is, then left and right curly

106
00:04:17,680 --> 00:04:24,730
braces dot format i. Alright, so if I run

107
00:04:24,730 --> 00:04:28,000
that program now, you can see that

108
00:04:28,000 --> 00:04:29,770
nothing happens in that final for loop,

109
00:04:29,770 --> 00:04:32,650
looping again or not. This print isn't

110
00:04:32,650 --> 00:04:34,330
executed - there's nothing showing on the

111
00:04:34,330 --> 00:04:35,560
screen and the program has actually

112
00:04:35,560 --> 00:04:38,860
exited. So storing my underscore range 5

113
00:04:38,860 --> 00:04:41,530
in big range is fine, if you want to call

114
00:04:41,530 --> 00:04:44,140
next each time you want a new value, but

115
00:04:44,140 --> 00:04:46,540
it isn't a good idea if you intend using

116
00:04:46,540 --> 00:04:48,310
it again in a for loop. So in that case,

117
00:04:48,310 --> 00:04:50,980
you should just use the my underscore range 5

118
00:04:50,980 --> 00:04:54,220
method function call direct again.

119
00:04:54,220 --> 00:04:55,660
So we'd go ahead and change that then -

120
00:04:55,660 --> 00:04:57,760
instead of for i in big underscore

121
00:04:57,760 --> 00:05:01,340
range, we change that for i in my

122
00:05:01,340 --> 00:05:03,550
underscore range, then five in

123
00:05:03,550 --> 00:05:04,780
parentheses, which is the number we want

124
00:05:04,780 --> 00:05:06,760
to pass to it. So we're calling the

125
00:05:06,760 --> 00:05:09,840
function direct. Run that again and you

126
00:05:09,840 --> 00:05:11,590
can see, in this case now, we're correctly

127
00:05:11,590 --> 00:05:13,180
getting the output because we're not

128
00:05:13,180 --> 00:05:15,070
using the variable - rather we're 

129
00:05:15,070 --> 00:05:17,370
actually using the function call. Alright,

130
00:05:17,370 --> 00:05:18,610
so the output is now correct and

131
00:05:18,610 --> 00:05:20,710
returning the right values. Now before I

132
00:05:20,710 --> 00:05:22,060
finish this video, I should mention that

133
00:05:22,060 --> 00:05:25,150
range does work if we store it. So I'm

134
00:05:25,150 --> 00:05:26,860
going to undo that last change that we

135
00:05:26,860 --> 00:05:31,990
added. Go back to big underscore

136
00:05:31,990 --> 00:05:34,830
range. Then we're going to go ahead and

137
00:05:34,830 --> 00:05:37,930
re-enable the code I commented out on line

138
00:05:37,930 --> 00:05:41,070
12 - big underscore range equals range 5.

139
00:05:41,070 --> 00:05:44,290
So go ahead and do that, then I'm going

140
00:05:44,290 --> 00:05:46,840
to comment out the call to our function,

141
00:05:46,840 --> 00:05:50,140
like so, and now that I've done that,

142
00:05:50,140 --> 00:05:53,140
I want to actually delete this call,

143
00:05:53,140 --> 00:05:56,050
this line 16, because we don't want to

144
00:05:56,050 --> 00:05:57,460
call next on the range object, so I'm

145
00:05:57,460 --> 00:05:59,950
going to delete that as well. And now

146
00:05:59,950 --> 00:06:03,580
when we run it, you can see that in both

147
00:06:03,580 --> 00:06:06,040
cases that's working fine, and the

148
00:06:06,040 --> 00:06:08,430
range class behaves like an iterable. In

149
00:06:08,430 --> 00:06:09,970
other words, it's reset each time it's

150
00:06:09,970 --> 00:06:12,070
used, which wasn't the case when you used

151
00:06:12,070 --> 00:06:14,370
a variable that actually called the

152
00:06:14,370 --> 00:06:16,570
function that we actually wrote. So again,

153
00:06:16,570 --> 00:06:18,400
the range class behaves like an iterable -

154
00:06:18,400 --> 00:06:20,030
each time it's called,

155
00:06:20,030 --> 00:06:22,130
it's actually reset. Now here my

156
00:06:22,130 --> 00:06:23,720
underscore range isn't really very

157
00:06:23,720 --> 00:06:25,520
useful, other than to show how to write a

158
00:06:25,520 --> 00:06:27,980
generator, so in the next video we're

159
00:06:27,980 --> 00:06:28,910
going to look at some more useful

160
00:06:28,910 --> 00:06:33,010
generators. So I'll see you in that next video.

