WEBVTT

1
00:00:02.250 --> 00:00:04.450
So it's now time to talk about ranges.

2
00:00:04.450 --> 00:00:07.590
Now we've actually used ranges
a few times in for loops, and

3
00:00:07.590 --> 00:00:10.390
we use that to loop
a specified number of times.

4
00:00:10.390 --> 00:00:13.820
So it's time we take a more detailed
look at what they actually are.

5
00:00:13.820 --> 00:00:17.930
So in Python 3 range is
actually a built in type.

6
00:00:17.930 --> 00:00:20.800
But in Python 2,
it was implemented as a function.

7
00:00:20.800 --> 00:00:24.660
So in a simplest form, we can create
a range just by specifying an n value.

8
00:00:24.660 --> 00:00:30.041
So we could do something
like print(range(100)).

9
00:00:30.041 --> 00:00:35.217
And if we actually run that, And

10
00:00:35.217 --> 00:00:38.160
return a range,
which is a range from 0, 130.

11
00:00:38.160 --> 00:00:42.220
It's not really terribly exciting,
but what it does do is show you to get

12
00:00:42.220 --> 00:00:45.730
back a range object with a start of 0 and
a stop of 100.

13
00:00:45.730 --> 00:00:48.430
Printing a range object
like that isn't useful.

14
00:00:48.430 --> 00:00:49.392
And when they come into their own,

15
00:00:49.392 --> 00:00:53.940
you get the most ever, you start iterating
over, for example, to cause the for

16
00:00:53.940 --> 00:00:57.030
loop to repeat a block of code
many times as we've actually seen.

17
00:00:57.030 --> 00:01:00.420
We can also use them to use them to
create another object such as the list.

18
00:01:00.420 --> 00:01:06.288
So, as an example we can come back here
and actually type my under score list,

19
00:01:06.288 --> 00:01:13.012
= listrange(10),

20
00:01:13.012 --> 00:01:15.980
and now if we actually print my list.

21
00:01:17.000 --> 00:01:17.610
Actually run that.

22
00:01:18.920 --> 00:01:21.660
You notice how we've automatically
got these 10 elements through from

23
00:01:21.660 --> 00:01:26.890
zero to nine just by actually
using the range and passing

24
00:01:26.890 --> 00:01:31.230
the end range of 10 and automatically
created those elements for us.

25
00:01:31.230 --> 00:01:34.950
So again, if you're passing a single value
to range the value's taken to be the end

26
00:01:34.950 --> 00:01:37.930
value and the start value defaults
to zero in that scenario,

27
00:01:37.930 --> 00:01:40.590
as you can see that with the first
element being zero right here at the top.

28
00:01:40.590 --> 00:01:41.913
But you can also specify start range,

29
00:01:41.913 --> 00:01:43.616
there's nothing stopping
you from doing that.

30
00:01:43.616 --> 00:01:47.496
So you could do something like,

31
00:01:47.496 --> 00:01:52.536
even = list(range (0, 10, 2).

32
00:01:52.536 --> 00:01:57.983
Or we can try odd = list(range (1, 10, 2).

33
00:01:57.983 --> 00:02:03.770
Print (even), print (odd).

34
00:02:03.770 --> 00:02:05.940
That's just another more
extended use of the range.

35
00:02:07.480 --> 00:02:09.352
And for some reason we've
some extra brackets here,

36
00:02:09.352 --> 00:02:11.110
we'll get rid of those
brackets to make it valid.

37
00:02:11.110 --> 00:02:12.491
We'll try running that.

38
00:02:12.491 --> 00:02:16.611
And we can see now we've got 0,
2, 4, 6, 8, and 1, 3, 5, 7, 9.

39
00:02:16.611 --> 00:02:20.510
So we're creating the list called even,
the first one from a range.

40
00:02:20.510 --> 00:02:23.740
And we put the range of 0 as
the start ten as the end, but

41
00:02:23.740 --> 00:02:28.060
you also use the steps,
that third number there to skip by two.

42
00:02:28.060 --> 00:02:30.140
So we've actually got only even numbers.

43
00:02:30.140 --> 00:02:33.730
By starting on zero and
skipping two with two, four, six, eight.

44
00:02:33.730 --> 00:02:34.770
And likewise with the odd.

45
00:02:34.770 --> 00:02:35.880
We did the opposite there.

46
00:02:35.880 --> 00:02:37.010
We started on one.

47
00:02:37.010 --> 00:02:39.400
Still went to the end rows of ten,
but skip by two, and

48
00:02:39.400 --> 00:02:41.720
then went to skip only the odd numbers.

49
00:02:41.720 --> 00:02:44.850
So one, three, five, seven,
nine in this example.

50
00:02:44.850 --> 00:02:48.131
Now Python 3 ranges are very
efficient in terms of memory.

51
00:02:48.131 --> 00:02:54.896
In other words,
if you did something like this,

52
00:02:54.896 --> 00:03:00.671
range[0,10] or range[0, say.

53
00:03:00.671 --> 00:03:04.280
They're actually both gonna use the same
amount of memory, believe it or not.

54
00:03:04.280 --> 00:03:04.910
So that's fine.
But

55
00:03:04.910 --> 00:03:08.390
if we actually change the code up here,
and actually start using large numbers,

56
00:03:08.390 --> 00:03:10.799
like instead of 10, putting 1,000, and

57
00:03:10.799 --> 00:03:14.800
1,000 for both of those, that will
still actually work, as you can see.

58
00:03:14.800 --> 00:03:17.890
Notice there was a slight pause there, and

59
00:03:17.890 --> 00:03:19.660
now we've actually got
a huge set of numbers.

60
00:03:19.660 --> 00:03:23.155
And It's literally going to be
1,000 elements, as you can see,

61
00:03:23.155 --> 00:03:25.001
right through to 1,000 there.

62
00:03:25.001 --> 00:03:27.398
998, 999 were the last elements,

63
00:03:27.398 --> 00:03:30.421
bearing in the mind the last
one's always not used.

64
00:03:30.421 --> 00:03:33.653
So you do need to go steady with
the zeroes though, because if you made

65
00:03:33.653 --> 00:03:38.450
the value say 10,000, it can actually take
quite a while for the lists to be created.

66
00:03:38.450 --> 00:03:40.920
And each list will occupy
a fair amount of memory.

67
00:03:40.920 --> 00:03:44.280
So the range itself is always
gonna return the same amount.

68
00:03:44.280 --> 00:03:49.530
I will use the same amount of space, but
when you're creating a list of that range

69
00:03:49.530 --> 00:03:52.730
then of course the list has to actually
expand out to use all the elements within

70
00:03:52.730 --> 00:03:55.860
that range and it can actually
start taking up a lot of memory.

71
00:03:55.860 --> 00:03:59.801
Kind of interesting in part two ranges
didn't behave like that at all.

72
00:03:59.801 --> 00:04:03.091
Old bellies were calculated and
returned as a sequence,

73
00:04:03.091 --> 00:04:06.723
remember we talked about sequences
to a degree in this course,

74
00:04:06.723 --> 00:04:11.541
Python 2 also had an x range that behaved
much like the Python 3 range object does.

75
00:04:11.541 --> 00:04:17.120
And Python 3 diversion 2x range has become
range and there's no longer an x range.

76
00:04:17.120 --> 00:04:21.930
So if you're converting path from
two code and you actually see the x

77
00:04:21.930 --> 00:04:26.460
range being used, you can just change
x range to range for pathway three and

78
00:04:26.460 --> 00:04:27.800
it's going to work just fine.

79
00:04:27.800 --> 00:04:30.460
Now ranges don't support
all the operations

80
00:04:30.460 --> 00:04:32.570
that you can perform on sequences.

81
00:04:32.570 --> 00:04:35.920
The reason for that is largely
because they represent sequences

82
00:04:35.920 --> 00:04:37.990
that follow a defined pattern.

83
00:04:37.990 --> 00:04:42.260
And so for example the multiplication
operator to repeat a range is not allowed,

84
00:04:42.260 --> 00:04:44.750
nor is concatenation,
pending in other words.

85
00:04:44.750 --> 00:04:48.180
Other than that you can perform all
the usual sequence operations or ranges.

86
00:04:48.180 --> 00:04:51.560
Now we didn't actually cover the index
method when dealing with two sequences,

87
00:04:52.640 --> 00:04:54.710
when we actually discussed strings and
lists.

88
00:04:54.710 --> 00:04:56.560
But I'm gonna show you
how to do that now and

89
00:04:56.560 --> 00:04:59.800
how you can access individual element
index over sequence using it.

90
00:04:59.800 --> 00:05:01.120
So it's gonna come in handy for our code.

91
00:05:01.120 --> 00:05:05.430
So I'm just gonna select that and
comment that code out.

92
00:05:05.430 --> 00:05:10.550
And let's just top something like
my underscore string equals,

93
00:05:10.550 --> 00:05:11.870
and we'll do all the letters
of the alphabet.

94
00:05:11.870 --> 00:05:15.990
A, B, C, D, E, F, G, H,
I, J, K, L, M, N, O, P,

95
00:05:15.990 --> 00:05:20.080
Q, R, S, T, U, V, W, X, Y, Z, or Zed.

96
00:05:20.080 --> 00:05:21.270
As we say in Australia.

97
00:05:21.270 --> 00:05:30.420
If I do a print(my_string.Index e).

98
00:05:30.420 --> 00:05:34.642
And if I do a print my
underscore string for,

99
00:05:34.642 --> 00:05:41.492
eventually run that So

100
00:05:41.492 --> 00:05:46.880
the index of the character e is actually
the character position four in the string.

101
00:05:46.880 --> 00:05:48.000
Remember, we count from zero.

102
00:05:48.000 --> 00:05:53.550
So zero was A, one is B,
two is C, three is D, four is E.

103
00:05:53.550 --> 00:05:56.870
So the index, dot index is
actually returning the number.

104
00:05:56.870 --> 00:05:59.360
Pointing to the character position

105
00:05:59.360 --> 00:06:01.860
of whatever you've actually
pass here as the parameter.

106
00:06:01.860 --> 00:06:04.350
So we can also apply indexes.

107
00:06:04.350 --> 00:06:07.460
Though the index function
to ranges as well.

108
00:06:07.460 --> 00:06:08.401
So we can so

109
00:06:08.401 --> 00:06:14.481
something like small underscore
decimals equals range zero, ten.

110
00:06:14.481 --> 00:06:18.910
Then we're gonna do print small decimals.

111
00:06:18.910 --> 00:06:24.510
We can also do print
small decimals.index3.

112
00:06:24.510 --> 00:06:28.880
If we actually run that,
we actually get the value three there,

113
00:06:28.880 --> 00:06:32.140
being the third,
being effectively the fourth position.

114
00:06:32.140 --> 00:06:36.560
Cuz, of course, the counting again started
from ten, so zero, one, two, three.

115
00:06:36.560 --> 00:06:39.720
And you can interestingly enough
also index into the range.

116
00:06:39.720 --> 00:06:45.044
So for example,
if you created a large range,

117
00:06:45.044 --> 00:06:50.504
like if we did something
like odd = range(1,

118
00:06:50.504 --> 00:06:54.820
10000, 2) Print odd.

119
00:06:55.981 --> 00:06:57.414
Bearing in mind this is just a range,

120
00:06:57.414 --> 00:07:00.070
not a list of ranges as we
did earlier in the video.

121
00:07:00.070 --> 00:07:03.930
We can actually do print, Odd, 985.

122
00:07:03.930 --> 00:07:04.670
Run it five.

123
00:07:07.710 --> 00:07:12.930
And you can see that actually gives us
the value from the 985th odd number,

124
00:07:12.930 --> 00:07:16.120
which is pretty handy and
also pretty fast to process as well.

125
00:07:16.120 --> 00:07:20.760
And it's also possible to check if a value
that you pass is actually in a range and

126
00:07:20.760 --> 00:07:22.600
you can use that using the in operator.

127
00:07:22.600 --> 00:07:28.860
So we can do something
like sevens equals range.

128
00:07:28.860 --> 00:07:32.598
We'll start from seven.

129
00:07:32.598 --> 00:07:34.158
Seven.

130
00:07:34.158 --> 00:07:38.320
We can put x equal int, input.

131
00:07:40.440 --> 00:07:45.619
Please enter a positive
number less than one million.

132
00:07:48.918 --> 00:07:53.416
And we can put if x in sevens,

133
00:07:53.416 --> 00:07:59.980
print is divisible using the replacement

134
00:07:59.980 --> 00:08:04.679
field, divisible by seven.

135
00:08:05.758 --> 00:08:08.384
Format x.

136
00:08:08.384 --> 00:08:09.690
So we can actually run that.

137
00:08:11.770 --> 00:08:13.450
Please enter a positive
number less than 1 million.

138
00:08:13.450 --> 00:08:18.280
So you could do something like say, 987.

139
00:08:18.280 --> 00:08:19.550
987 is divisible by seven.

140
00:08:19.550 --> 00:08:23.731
And let's do a number that we
know isn't divisible by seven.

141
00:08:23.731 --> 00:08:25.130
50.

142
00:08:25.130 --> 00:08:27.753
We don't get any error,
don't get any messages,

143
00:08:27.753 --> 00:08:31.170
I should say, because the number
50 isn't divisible by seven.

144
00:08:31.170 --> 00:08:34.965
Because, again, just to confirm, we
started a range starting at number seven,

145
00:08:34.965 --> 00:08:36.395
and went through to a million,

146
00:08:36.395 --> 00:08:40.210
and only actually showing numbers that
were incremented by seven each time.

147
00:08:40.210 --> 00:08:42.660
You can see that ranges can
actually be very powerful.

148
00:08:42.660 --> 00:08:47.460
In addition to that you can also
apply slice ranges as well.

149
00:08:47.460 --> 00:08:49.840
So we can print our small decimals
that we defined above again so

150
00:08:49.840 --> 00:08:53.240
small decimals just to confirm
we're on that same page.

151
00:08:53.240 --> 00:09:01.042
Then we can also do my_range
= small decimals 2, ::2.

152
00:09:01.042 --> 00:09:03.385
Print my range just to see what it does.

153
00:09:03.385 --> 00:09:04.306
Print my range.

154
00:09:04.306 --> 00:09:08.450
And print(my_range.index(4), and

155
00:09:08.450 --> 00:09:12.910
then actually run that after you
just actually answered a question.

156
00:09:12.910 --> 00:09:14.190
So it was 50 there.

157
00:09:14.190 --> 00:09:15.740
There's the two other ranges.

158
00:09:15.740 --> 00:09:19.210
So you can see that my_range has become
the range from zero to ten in steps of

159
00:09:19.210 --> 00:09:21.640
two, by just actually entering ::2 there.

160
00:09:21.640 --> 00:09:25.700
And the index of the value
4 in the range is 2.

161
00:09:25.700 --> 00:09:28.890
And of course the range is 0,
2, 4, 6, and 8.

162
00:09:28.890 --> 00:09:31.790
Things to remember,
is that by not specifying the start and or

163
00:09:31.790 --> 00:09:35.640
stop, it'll actually default to
the start and it'll end on the sequence.

164
00:09:35.640 --> 00:09:39.160
So in other words specifying colon,
colon two as we did here.

165
00:09:39.160 --> 00:09:43.280
What that means is to slice all
of the small decimal elements but

166
00:09:43.280 --> 00:09:44.600
stepping every other value.

167
00:09:44.600 --> 00:09:47.230
So ::2 was skipping one each time.

168
00:09:47.230 --> 00:09:49.490
So just to clarify that
with another example.

169
00:09:49.490 --> 00:09:51.380
Now we'll just comment
some of this code out now.

170
00:09:54.830 --> 00:10:02.145
So if we do something like decimals
equals range, 0, 100 print decimals.

171
00:10:02.145 --> 00:10:06.890
My_range = decimals.

172
00:10:06.890 --> 00:10:14.650
It would be something like
three 43 print my range.

173
00:10:16.605 --> 00:10:19.080
For I in my underscore range.

174
00:10:20.625 --> 00:10:25.500
print(i) and
we can do just to separate it a little bit

175
00:10:25.500 --> 00:10:29.890
put = times 40 so
put a bit of a separator there.

176
00:10:31.770 --> 00:10:35.220
And just add a bit of space here.

177
00:10:35.220 --> 00:10:38.833
And then we'll finish off with a for

178
00:10:38.833 --> 00:10:43.661
i in range(3, 40, 3):, print(i).

179
00:10:43.661 --> 00:10:45.040
Let's get rid of some of those lines now.

180
00:10:46.920 --> 00:10:48.070
Okay, so if we actually run that.

181
00:10:50.010 --> 00:10:51.680
You can actually see what's happened here.

182
00:10:51.680 --> 00:10:55.010
So, the my_range has been created
by taking a slice from decimals

183
00:10:55.010 --> 00:10:56.400
to return a new range.

184
00:10:56.400 --> 00:11:00.210
So as a result of that it shouldn't come
as any surprise that the last two ranges

185
00:11:00.210 --> 00:11:02.560
can actually be tested for
equality and return true.

186
00:11:02.560 --> 00:11:06.268
So in other words we can
actually come back here and

187
00:11:06.268 --> 00:11:08.839
actually do something like this.

188
00:11:08.839 --> 00:11:15.918
print(my_range) Is equal to range (3,
40, 3).

189
00:11:15.918 --> 00:11:18.338
Actually run that.

190
00:11:18.338 --> 00:11:21.145
We actually get the answer true,
because they're actually considered,

191
00:11:21.145 --> 00:11:23.170
well they actually are, technically equal.

192
00:11:23.170 --> 00:11:24.480
So I'm actually gonna
finish the video here.

193
00:11:24.480 --> 00:11:28.290
In the next video we're going to continue
on and go through a few more examples of

194
00:11:28.290 --> 00:11:30.520
range, and then I'm gonna
come up with a challenge for

195
00:11:30.520 --> 00:11:32.060
you to help your understanding of range.

196
00:11:32.060 --> 00:11:33.400
So I'll see you in the next video.

