1
00:00:04,700 --> 00:00:10,260
In programming languages such as C or
Java, for loops work differently.

2
00:00:10,260 --> 00:00:14,980
In those languages, you provide a starting value
and an ending value, and increment a

3
00:00:14,990 --> 00:00:18,949
variable each time around the loop.
Python's different approach to for loops

4
00:00:18,949 --> 00:00:24,080
makes them incredibly powerful but also
very flexible. We can get the same effect

5
00:00:24,080 --> 00:00:29,180
as C for loops, by iterating over a range
of values. So let's take a look at that.

6
00:00:29,180 --> 00:00:34,980
What I'm going to do is create a new file
here. We're gonna call this one ranges.py.

7
00:00:39,640 --> 00:00:43,219
Now be careful here with what I've just
done. You can get problems if you'd

8
00:00:43,219 --> 00:00:47,239
called the file range without an s,
because its name will conflict with

9
00:00:47,239 --> 00:00:51,949
pythons range class. That's why I called
this one ranges.py. Alright so let's

10
00:00:51,949 --> 00:00:56,269
have a go at printing out the values
from 1 to 20. We'll use a range object

11
00:00:56,269 --> 00:01:03,000
and give it the starting and ending
values; for i in range parentheses

12
00:01:03,000 --> 00:01:09,640
1 comma 20, and the closing parentheses
colon. Next line, print parentheses double

13
00:01:09,650 --> 00:01:13,430
quotes i is now. Then within the double
quotes, left and right curly braces

14
00:01:13,430 --> 00:01:18,530
closing double quote dot format
parentheses i in the parentheses and the

15
00:01:18,530 --> 00:01:24,079
two closing right parentheses. We'll talk
more about naming variables in a later

16
00:01:24,079 --> 00:01:28,490
video. Up till now, we've just been using
simple words that describe the values

17
00:01:28,490 --> 00:01:32,659
we've been dealing with. Normally, you
should avoid using single character

18
00:01:32,659 --> 00:01:36,229
names for variables, as they don't give
any indication of what the variable is

19
00:01:36,229 --> 00:01:41,360
for. Loop control variables, however, are
one of the few places where names like

20
00:01:41,360 --> 00:01:47,780
i or j are acceptable. i is taken to be short
for index, and the convention is that it

21
00:01:47,780 --> 00:01:51,680
is an acceptable name in a for loop, as
we're using here. Now when we run the

22
00:01:51,680 --> 00:01:57,200
program, which I'll do now, you can see the
output on the screen and you can see

23
00:01:57,200 --> 00:02:01,900
that it almost works. The range was
actually outputted from 1 to 19.

24
00:02:01,900 --> 00:02:05,479
So in other words, 20 isn't included here
and you can see clearly that we use the

25
00:02:05,479 --> 00:02:11,180
numbers 1 to 20 on line 1. So do you
remember how our strings slices

26
00:02:11,180 --> 00:02:15,860
returned the characters up to, but not
including, the end number? Well ranges

27
00:02:15,860 --> 00:02:18,590
work in the same way. The last value specified

28
00:02:18,590 --> 00:02:24,410
is not included. range produces a range
of numbers from the starting value, up to

29
00:02:24,410 --> 00:02:30,500
but not including the end number, or the
end value. To include the value 20 in our

30
00:02:30,500 --> 00:02:37,400
loop, therefore, we need to change the stop
value to 21. So I'm gonna go ahead and do that,

31
00:02:37,400 --> 00:02:40,490
and this time when I run the program we
should see the values from 1 to 20.

32
00:02:40,490 --> 00:02:45,440
You can see there that we've now got the
correct output. So experiment with

33
00:02:45,440 --> 00:02:48,980
different values in the range to
understand what it's doing. For example,

34
00:02:48,980 --> 00:02:53,880
you could try the following:
I'll change 1 to 21 to 10

35
00:02:53,880 --> 00:02:59,660
to 16, and that would give us the
numbers in the range of 10 through 15,

36
00:02:59,660 --> 00:03:03,040
and you can see that is the case with the
output at the bottom of the screen.

37
00:03:03,040 --> 00:03:07,360
When you're happy with that, I'll
see you in the next video.

