1
00:00:04,529 --> 00:00:09,250
One of the reasons that computers are so
useful is that they can repeat an

2
00:00:09,250 --> 00:00:13,480
operation multiple times, very quickly.
That removes the need for a human to

3
00:00:13,480 --> 00:00:18,310
perform a repetitive task. Python
provides several ways to repeat a block

4
00:00:18,310 --> 00:00:23,380
of code, things like for loops, while
loops and list comprehensions and

5
00:00:23,380 --> 00:00:27,340
generator expressions. In the next few
videos we'll look at for loops.

6
00:00:27,340 --> 00:00:31,570
We'll cover while loops after that but
we'll be leaving list comprehensions and

7
00:00:31,570 --> 00:00:36,160
generator expressions for a later
section. We've actually seen a for loop

8
00:00:36,160 --> 00:00:40,270
in earlier examples, and now it's time to
look at them in more detail and explain

9
00:00:40,270 --> 00:00:45,130
what was happening in that code. A for
loop works by iterating over some set of

10
00:00:45,130 --> 00:00:50,840
values. It assigns each of the values, one
by one, to one or more variables.

11
00:00:50,840 --> 00:00:56,520
It then executes a block of code once for each
value. The set of values comes from a

12
00:00:56,530 --> 00:01:01,960
sequence or some other iterable object.
We've seen one sequence type, the string

13
00:01:01,960 --> 00:01:07,360
type. I'll use that for our first examples.
An iterable type is anything that can

14
00:01:07,360 --> 00:01:12,670
be iterated over. So that means a
sequence is also an iterable. In simple

15
00:01:12,670 --> 00:01:17,319
terms, if you can use it with a for loop,
then its iterable. And I'll be

16
00:01:17,320 --> 00:01:22,920
introducing one iterable type - the range -
in some of the following examples.

17
00:01:22,920 --> 00:01:25,008
Alright, so back to Intellij and what we're going

18
00:01:25,008 --> 00:01:34,900
to do here is create a new file, and we'll call this forloops.py.

19
00:01:34,920 --> 00:01:38,140
Alright, so we'll use the parrot string from an earlier video.

20
00:01:38,140 --> 00:01:45,860
So parrot is equal to "Norwegian Blue".

21
00:01:45,860 --> 00:01:52,480
So you saw how to use in to check if any particular
letter is in the sequence. In also has another use

22
00:01:52,480 --> 00:01:56,540
when used with a for loop. So let's see
how that works, so we're going to type for

23
00:01:56,540 --> 00:02:07,680
character in parrot:
print (character)

24
00:02:07,680 --> 00:02:13,000
So a for loop starts with the keyword for.
Next we provide the names of one or more

25
00:02:13,000 --> 00:02:16,819
variables. We'll stick to using a single
variable at first because

26
00:02:16,819 --> 00:02:21,049
using more than one won't make much
sense just yet. In this example, we've

27
00:02:21,049 --> 00:02:24,829
called the variable, character. You can
call the variable anything you want,

28
00:02:24,829 --> 00:02:29,120
within the rules for variable names, of
course. Variable names can't start with a

29
00:02:29,120 --> 00:02:34,489
digit, for example. Next we have the
keyword, in. Our variable character will

30
00:02:34,489 --> 00:02:39,650
be bound to each of the items in the
sequence, in turn. Finally we specify what

31
00:02:39,650 --> 00:02:43,819
the values are in. We're using the parrot
variable here but we could use any

32
00:02:43,819 --> 00:02:49,159
iterable object. What should happen is
that the variable character gets each of

33
00:02:49,159 --> 00:02:52,459
the characters in the string. On line
4 we're printing out the value of

34
00:02:52,460 --> 00:02:57,820
character, so let's run the program and
see if it does what we expect it to do.

35
00:02:59,240 --> 00:03:03,019
And that looks right, so if I scroll the
output there, you can see each letter of

36
00:03:03,019 --> 00:03:07,760
the string is output on a separate line,
and I'm just going to increase this just

37
00:03:07,760 --> 00:03:11,000
so we can see a bit more of it. You can't
quite see at all without actually

38
00:03:11,000 --> 00:03:13,639
scrolling all the source code off
the screen but it's enough of it anyway.

39
00:03:13,639 --> 00:03:17,750
So that output's probably not very useful
but it does demonstrate what our for

40
00:03:17,750 --> 00:03:22,940
loop is actually doing. Students often
find this confusing, especially if

41
00:03:22,940 --> 00:03:26,959
they've never programmed before. Even
students who've used other languages can

42
00:03:26,959 --> 00:03:31,280
be a bit confused because the for loop
in Python behaves differently to C or

43
00:03:31,280 --> 00:03:35,690
Java. So let's set a breakpoint on line
3 and run the program in the

44
00:03:35,690 --> 00:03:38,320
debugger to see what's happening.

45
00:03:41,780 --> 00:03:44,280
So you can see now that I've chosen debug for loops,

46
00:03:44,280 --> 00:03:47,620
the debugger stopped on line 3,
where we set the breakpoint. In the

47
00:03:47,620 --> 00:03:51,280
variables pane we can see the value of
parrot. It's bound to the string

48
00:03:51,280 --> 00:03:56,380
Norwegian Blue. If we use step over,

49
00:03:56,380 --> 00:03:58,080
you can see having done that,

50
00:03:58,080 --> 00:04:00,760
the variables pane's now showing the value of character.

51
00:04:00,760 --> 00:04:06,460
It's been given the first character in Norwegian Blue,
so it's bound to the capital letter N. When we

52
00:04:06,460 --> 00:04:11,920
step over again, execution has going back
to line 3, as you can see there,

53
00:04:11,920 --> 00:04:17,000
and the print on line 4 has been executed.
If I switch to the console pane, you can see

54
00:04:17,000 --> 00:04:22,920
we've got the first letter there, N.
Alright, so if I step over again now,

55
00:04:22,920 --> 00:04:27,040
we can see that line 3 is executed again.
Now we haven't yet printed again so

56
00:04:27,040 --> 00:04:31,860
there's no change to the console pane, but
if I switch back to the debugger pane now,

57
00:04:31,860 --> 00:04:35,210
you can see that character now
has the value o, the next character from

58
00:04:35,210 --> 00:04:39,260
Norwegian Blue. So a for loop executes
all the code in its code block

59
00:04:39,260 --> 00:04:43,220
repeatedly. It loops around until there's
no more values, in other words.

60
00:04:43,220 --> 00:04:48,500
We're ready to execute the print line again so
I'm going to step over, and I won't keep

61
00:04:48,500 --> 00:04:50,990
switching between the debugger and
console panes. That I'll just get

62
00:04:50,990 --> 00:04:55,220
confusing, but if you do you'll see that
the letter o has now been printed and we're

63
00:04:55,220 --> 00:05:00,800
now back on line 3. Step over again,
which I did to execute the line once

64
00:05:00,800 --> 00:05:05,390
more and now character has the value r.
So each time we get back to line 3

65
00:05:05,390 --> 00:05:09,950
and execute it, character gets the next
letter from the parrot string, and that's

66
00:05:09,950 --> 00:05:14,030
why I called the variable character, by
the way. It's been assigned each of the

67
00:05:14,030 --> 00:05:17,750
characters in the string, one after the
other. There's nothing special about that

68
00:05:17,750 --> 00:05:22,310
name - it just reflects the values that
our code will assign to it. Alright, so

69
00:05:22,310 --> 00:05:24,860
I'm going to continue stepping over the
code and you'll see each letter being

70
00:05:24,860 --> 00:05:29,620
assigned to our character variable,
each time around the loop.

71
00:05:31,259 --> 00:05:35,620
And you can see finally now, we've hit
the last letter e. If we step over once

72
00:05:35,620 --> 00:05:41,080
more now. It's now executed the print.
If we have a look on the console, we can see

73
00:05:41,080 --> 00:05:46,160
we've got the entire line there,
Norwegian Blue, and back to the debugger.

74
00:05:46,160 --> 00:05:50,580
One more time, I'll press step over. This
time you can see what's happened. It's

75
00:05:50,580 --> 00:05:54,520
caused the for loop to terminate because
there's nothing left for it to do,

76
00:05:54,520 --> 00:05:58,360
and again, you just saw that when I tapped
over to the console pane; all letters had

77
00:05:58,360 --> 00:06:02,529
been printed out. Alright, so I'll stop
the video here. Run the code a few more

78
00:06:02,529 --> 00:06:05,440
times in the debugger to make sure you
understand what's happening here.

79
00:06:05,440 --> 00:06:10,440
When you're happy with this basic for loop,
I'll see you in the next video.

