1
00:00:04,710 --> 00:00:09,059
Alright so let's continue with our
ranges.py program, to look at a few

2
00:00:09,059 --> 00:00:13,049
more things we can do with ranges. If
you want to use a range just to repeat a

3
00:00:13,049 --> 00:00:16,830
block of code a certain number of times,
then there's no need to provide a start

4
00:00:16,830 --> 00:00:21,539
value. If you don't provide a start value,
it defaults to zero. So I'm going to

5
00:00:21,539 --> 00:00:26,369
change the code there and delete the
comma 16 so we've just got 10 in

6
00:00:26,369 --> 00:00:33,809
parentheses, and if we run the program
now, you can see the program has actually

7
00:00:33,809 --> 00:00:40,590
gone through 10 times, 0 down to 9. Now we get
the values from 0 to 9 because the start

8
00:00:40,590 --> 00:00:45,750
value defaults to zero and obviously
it's not included on line 1. So what else

9
00:00:45,750 --> 00:00:50,370
can we do? Well we can provide a step
value to our ranges. If we do that, we

10
00:00:50,370 --> 00:00:54,630
have to specify a start as well.
Otherwise, if we try and add just a

11
00:00:54,630 --> 00:01:00,180
step to our existing code, comma 2,
Python then interprets what I've typed

12
00:01:00,180 --> 00:01:05,519
as starting at 10 and going up by 2. The
default step value is 1 and you can't

13
00:01:05,519 --> 00:01:09,659
count from 10 to 2 in steps of 1 and
expect to get any values. So if we run

14
00:01:09,659 --> 00:01:15,869
the program as is, we get no output at
all on the screen. So to get the values

15
00:01:15,869 --> 00:01:21,180
from 0 to 10 in steps of 2, because we're
using steps, we have to specify a start

16
00:01:21,180 --> 00:01:27,689
value and I'm going to enter the value 0
here. So 0 comma 10 comma 2 and now if we

17
00:01:27,689 --> 00:01:34,200
run that, you can see it gives us the
values we wanted - 0 2 4 6 8. Note that

18
00:01:34,200 --> 00:01:38,760
we didn't get 10 printed out. If you
recall, ranges go up to but not including

19
00:01:38,760 --> 00:01:43,110
the stop value, just like slices and as
we discussed in the previous video. Now

20
00:01:43,110 --> 00:01:48,329
if you want to count backwards, you can
use a negative step range. So what we

21
00:01:48,329 --> 00:01:51,030
need to do then, because we're going
backwards we'll need to swap the

22
00:01:51,030 --> 00:01:56,070
start and end. So we're going to start at
10, go down to 0 and the step is going to

23
00:01:56,070 --> 00:02:02,670
be a minus number, minus 2. If I do that
now and run it, you can see we get the

24
00:02:02,670 --> 00:02:07,350
values 10 8 6 4 2. So the start value has
to be greater than the stop value when

25
00:02:07,350 --> 00:02:10,679
using a negative step, for obvious
reasons. For the same reason as we didn't

26
00:02:10,679 --> 00:02:15,540
get 10 in the previous output when we were
stepping up, we don't get 0 when using a

27
00:02:15,540 --> 00:02:18,550
negative step. Alright so that's an
introduction now to

28
00:02:18,550 --> 00:02:23,260
range class. We haven't created very
exciting code here but we've seen how to

29
00:02:23,260 --> 00:02:27,430
create ranges of values to iterate
over. Now something else you can do with

30
00:02:27,430 --> 00:02:32,170
a range, is check if a value is in it.
Let's open the conditions.py program

31
00:02:32,170 --> 00:02:38,170
from a previous video that we've worked
on. You can see it here, conditions.pi,

32
00:02:38,170 --> 00:02:44,470
so I'm going to open that up. Alright, so there's
our code from that program. Now on line 4

33
00:02:44,470 --> 00:02:50,650
we're checking to see if age is between
16 and 65 inclusive. We could use a range

34
00:02:50,650 --> 00:02:54,850
instead. Now I'm not saying that you
should use a range here and the current

35
00:02:54,850 --> 00:02:58,360
code is the most efficient way of
performing that test, but let's see how

36
00:02:58,360 --> 00:03:02,170
to do the same thing using a range. So
I'm going to comment out that first line,

37
00:03:02,170 --> 00:03:12,190
line 4. I'm gonna replace it with if age
in range, and in parenthesis 16 comma 66

38
00:03:12,190 --> 00:03:16,990
right parenthesis and a colon. Because
the last value isn't included in a range,

39
00:03:16,990 --> 00:03:23,650
I had to use 66 as the stop value rather
than 65. So let's run the program to

40
00:03:23,650 --> 00:03:30,670
confirm that it still works: How old are
you? Let's test another, 16 first, Have a

41
00:03:30,670 --> 00:03:35,860
good day at work.
Run it again, we'll try it with 65.

42
00:03:35,860 --> 00:03:42,080
That works okay. Run it again, this time with 15,
Enjoy your free time, and one more time,

43
00:03:42,080 --> 00:03:48,940
we'll try it with, say, the value 75.
So clearly that's working as it should.

44
00:03:50,100 --> 00:03:55,030
So you saw that I tested it with the, with
a variety of values to make sure it's

45
00:03:55,030 --> 00:03:57,940
okay. So make sure you do that whenever
you're testing your programs, to make

46
00:03:57,940 --> 00:04:00,760
sure you're actually getting a proper and
comprehensive test and obviously I could

47
00:04:00,760 --> 00:04:04,870
do a lot more testing, but we've done a
basic amount of testing to confirm that

48
00:04:04,870 --> 00:04:08,260
the codes are working pretty well as it
should. Now as I mentioned previously,

49
00:04:08,260 --> 00:04:13,000
the code that's now commented out on
line 4 is more efficient, but testing

50
00:04:13,000 --> 00:04:16,540
if a value is in a range can be useful,
and for that reason I've demonstrated

51
00:04:16,540 --> 00:04:20,890
how to do that. Alright, so I'm going to stop
the video here. In the next one, we'll

52
00:04:20,890 --> 00:04:26,220
start looking at nested for loops.
See you in the next video.

