1
00:00:04,540 --> 00:00:08,840
Alright so let's talk about the break
statement in Python. So it's also

2
00:00:08,840 --> 00:00:13,260
possible to break out of a for loop
completely, using the break statement.

3
00:00:13,260 --> 00:00:18,080
If we replace continue with break, we won't get
a reminder to buy bread or rice because

4
00:00:18,080 --> 00:00:22,430
the loop will terminate when it gets to
spam. So what I'm going to do is delete

5
00:00:22,430 --> 00:00:27,890
this commented code out completely from
the previous video and then we're going

6
00:00:27,890 --> 00:00:35,520
to change continue to break.
Firstly, lets run it,

7
00:00:35,520 --> 00:00:37,120
and you can see what happened there.

8
00:00:37,120 --> 00:00:39,940
Basically, we saw the output milk pasta

9
00:00:39,949 --> 00:00:44,539
and eggs, but after we executed break we
didn't get a reminder to buy bread or

10
00:00:44,539 --> 00:00:47,690
rice, and that's because the loop
terminated when it got to the break

11
00:00:47,690 --> 00:00:51,980
statement on line five, when it got to
spam. So break can be very useful.

12
00:00:51,980 --> 00:00:56,200
Imagine we're searching through a list
containing thousands of items. If we find

13
00:00:56,210 --> 00:00:58,730
the item we're looking for,
there's no point looking through the

14
00:00:58,730 --> 00:01:03,480
rest of the list. You generally stop
looking when you find what you've looked for.

15
00:01:03,480 --> 00:01:08,780
So what I'm going to do now is copy the
first line, line one here,

16
00:01:08,780 --> 00:01:19,140
and we're going to create a new file called searching.

17
00:01:19,140 --> 00:01:22,140
Ok, I've pasted that line back in.

18
00:01:22,140 --> 00:01:25,880
Alright, so let's use a for loop to search for an
item. We're going to start with a couple

19
00:01:25,880 --> 00:01:30,620
of variables, so on line three I'm going to
type item_to_find

20
00:01:30,620 --> 00:01:36,460
equals spam in double quotes, and on the
next line found_at is equal

21
00:01:36,469 --> 00:01:40,670
to None, noting that it starts with a capital N.

22
00:01:40,670 --> 00:01:45,590
So we're gonna search for spam and I've
bound the variable item defined in the

23
00:01:45,590 --> 00:01:49,640
string value that we're looking for,
that's on line three. If we find it, we

24
00:01:49,640 --> 00:01:52,729
want to know its position in the list.
We're going to store that in the

25
00:01:52,729 --> 00:01:58,640
variable found at, or found_at.
Now initialize that to None on line four,

26
00:01:58,640 --> 00:02:02,720
and None is a constant that's used to
show that something doesn't have a value,

27
00:02:02,720 --> 00:02:06,770
so it's roughly equivalent to null
in C or Java. Now I'm going to come back

28
00:02:06,770 --> 00:02:10,550
to that line because new programmers
might be wondering why we've attached a

29
00:02:10,550 --> 00:02:13,910
variable to something that doesn't have
a value. It doesn't seem like a very

30
00:02:13,910 --> 00:02:17,209
useful thing to do. Well it actually is
useful. I wouldn't

31
00:02:17,209 --> 00:02:21,439
be doing it otherwise, but it'll be easier
to show why it's useful when we see our

32
00:02:21,439 --> 00:02:25,819
code working. Alright so now what we
need to do is write our for loop.

33
00:02:25,819 --> 00:02:28,969
I'm going to write this one slightly
differently. What we're going to do is

34
00:02:28,969 --> 00:02:33,480
loop over the index positions of the
list instead of looping over the items.

35
00:02:33,480 --> 00:02:40,340
So to do that, on line six I'm going to
type for index in range

36
00:02:40,340 --> 00:02:45,680
left parenthesis len left parenthesis again,
shopping_list and then we've

37
00:02:45,680 --> 00:02:51,230
got the two right parentheses and a
colon. On line seven, if shopping_list

38
00:02:51,230 --> 00:02:57,260
then in square brackets,
index and then closing right square

39
00:02:57,260 --> 00:03:04,440
bracket. That's equal to two equal signs
item_to_find :

40
00:03:04,440 --> 00:03:12,950
found_at is equal to index. Okay so
there's our code. So I've used a new

41
00:03:12,950 --> 00:03:17,269
function there - the len function.
Len is short for length and lets us know

42
00:03:17,269 --> 00:03:22,489
how many items are in the sequence. Our
shopping list contains six items, so len

43
00:03:22,489 --> 00:03:26,360
left parenthesis shopping_list
right indices on line six,

44
00:03:26,360 --> 00:03:30,739
that's going to return the value six. So that
means index, therefore, will get the

45
00:03:30,740 --> 00:03:37,580
values 0 1 2 3 4 and 5. Remember that the
last value in the range isn't included.

46
00:03:37,580 --> 00:03:41,840
So indexing a list is exactly the same
as indexing a string, and we covered

47
00:03:41,840 --> 00:03:46,519
that in the last section. The item at
index position 0 is milk, the item at

48
00:03:46,519 --> 00:03:51,889
position 1 is pasta, eggs is at
position 2 and so on. If we find the item

49
00:03:51,889 --> 00:03:55,430
we're looking for, which is the test on
line 7, we store its position in the

50
00:03:55,430 --> 00:04:00,180
variable found_at on line
8. When the loop terminates the value of

51
00:04:00,180 --> 00:04:05,140
found_at, we'll either still be
null as we defined it originally on line 4,

52
00:04:05,140 --> 00:04:08,599
if we didn't find the item we want, or
it'll be in the index position of the

53
00:04:08,599 --> 00:04:13,489
item in the list, which was set on line
8. Alright, so the last bit of code now

54
00:04:13,489 --> 00:04:18,349
we need to add, is to print out what we found.
So let's do that. Just as a reminder here,

55
00:04:18,349 --> 00:04:21,519
I'm gonna put a comment on line 6.

56
00:04:23,260 --> 00:04:28,340
This is what effectively is happening
here, that line seven, that's the len

57
00:04:28,350 --> 00:04:32,160
shopping list returns 6, and that's why
it's equivalent to what I've typed there

58
00:04:32,160 --> 00:04:36,600
if we weren't using the len function.
Alright so on line eleven then, we're

59
00:04:36,600 --> 00:04:43,470
going to type print parentheses double
quotes Item found at position and left

60
00:04:43,470 --> 00:04:48,570
and right curly braces dot format,
and in parentheses found_at

61
00:04:48,570 --> 00:04:53,780
and then we've got our closing right parenthesis.
If you run the program,

62
00:04:55,000 --> 00:04:58,740
you can see it says Item found at
position 3, and that's correct.

63
00:04:58,740 --> 00:05:02,660
Spam is the fourth item in the list, at
index position three, and obviously as

64
00:05:02,669 --> 00:05:06,660
the count starts from zero. Now I don't
think I need to step through the for

65
00:05:06,660 --> 00:05:09,720
loop from the beginning, but do that if
you want to see, going around from the

66
00:05:09,720 --> 00:05:14,100
start. What I'm going to do is set a
breakpoint on line nine, where we assign

67
00:05:14,100 --> 00:05:23,060
the index number to our found_at
variable. If you run the code in the debugger,

68
00:05:23,060 --> 00:05:27,360
you can see index is set to the
value of three and we've found the item

69
00:05:27,360 --> 00:05:33,160
we're looking for. found_at is
currently still None. When I step over

70
00:05:33,160 --> 00:05:37,180
you can see that three has now been
assigned to found_at

71
00:05:37,180 --> 00:05:41,700
and that's good. Our program is working and
has found spam in the list. What's not so

72
00:05:41,700 --> 00:05:45,150
good is what happens next. We found what
we're looking for and there's no point

73
00:05:45,150 --> 00:05:49,590
continuing to search, but when I step
over again which I'll do now, we can see

74
00:05:49,590 --> 00:05:53,220
that the code's still searching. You'll
have to continually step over here, till

75
00:05:53,220 --> 00:05:57,900
that finishes its work. Eventually you can
see the program is now completed. So this

76
00:05:57,900 --> 00:06:06,660
is where break can be useful. What we're
going to do is add a break after line nine,

77
00:06:06,660 --> 00:06:10,410
indented to the same level, as you
can see there. So if we repeat the test

78
00:06:10,410 --> 00:06:17,220
I'm going to debug it again, found_at
currently is set to None. If I step over,

79
00:06:17,220 --> 00:06:20,580
found_at is now 3. When we're
ready to execute the break statement, a

80
00:06:20,580 --> 00:06:25,830
break causes Python to jump out of the
loop, so when I step over now, you can see

81
00:06:25,830 --> 00:06:29,520
the execution has gone straight to line
twelve. That's more efficient because we

82
00:06:29,520 --> 00:06:32,849
don't keep going around the loop after
findng the item we want. Step over one

83
00:06:32,849 --> 00:06:36,449
more time, and that's terminated the
program, as you can see.

84
00:06:36,449 --> 00:06:40,110
So that's the break statement. If you
need to jump out of a loop, that's how to

85
00:06:40,110 --> 00:06:44,140
do it. We'll be using break in some other
examples, later in the course.

86
00:06:44,140 --> 00:06:47,720
I'll stop the video here and in the next one,
we'll look at what happens when the item we

87
00:06:47,729 --> 00:06:52,430
want isn't in the list.
See you in the next video

