1
00:00:04,720 --> 00:00:09,070
Back in the previous section, we used a
slice to extract all the delimiters

2
00:00:09,070 --> 00:00:12,880
from a list of numbers. I'm going to
start by reviewing that code, and I've

3
00:00:12,880 --> 00:00:16,509
called this file strings2.py and
I've grabbed that code from that previous

4
00:00:16,509 --> 00:00:19,810
section and you can see it on the screen
there. What I have done, though, is deleted

5
00:00:19,810 --> 00:00:23,769
the first few lines of code from the
file. We used a slice that started at

6
00:00:23,769 --> 00:00:28,929
position 1, which is the first comma, then
extracted every fourth character until

7
00:00:28,929 --> 00:00:32,800
it reached the end of the string. That
worked but it does rely on there being

8
00:00:32,800 --> 00:00:37,030
three digits in each of the numbers,
after the first one. That's probably fine

9
00:00:37,030 --> 00:00:40,660
if the data came from another computer
program, but it's not something we can

10
00:00:40,660 --> 00:00:46,329
rely on if the user's typing in those
numbers. In that case, we should examine

11
00:00:46,329 --> 00:00:50,829
each character in the number string to
check if it's a digit or not. As you saw

12
00:00:50,829 --> 00:00:54,910
in the previous video, a for loop can be
used to do that. What we're going to do

13
00:00:54,910 --> 00:00:59,170
here is iterate over numbers and append
anything that isn't a digit to the

14
00:00:59,170 --> 00:01:03,460
separators string. Once we've seen the
code working, we'll step through it with

15
00:01:03,460 --> 00:01:07,869
a debugger so you can see what it's
doing. The first step is to initialise

16
00:01:07,869 --> 00:01:12,580
our separators variable. That's something
that causes a lot of confusion, and I'm going

17
00:01:12,580 --> 00:01:16,990
to spend a minute discussing it, once
we've seen the code working. So I'm going

18
00:01:16,990 --> 00:01:23,170
to change this to become equal to an
empty string, and I'm gonna come down here

19
00:01:23,170 --> 00:01:28,959
and add some code, specifically starting
on line 4; for char in number

20
00:01:28,959 --> 00:01:37,119
colon if not char.isnumeric and the
parentheses are added automatically,

21
00:01:37,119 --> 00:01:45,819
colon separators equals separators plus
char. We're going to leave the code on line

22
00:01:45,819 --> 00:01:49,239
8 that prints the separators and the
rest of the file is going to remain as

23
00:01:49,239 --> 00:01:54,099
it was. Alright so I've used another
one of the string methods here on line

24
00:01:54,099 --> 00:01:57,910
5. I provided a link to the string
methods earlier in this section and we'll

25
00:01:57,910 --> 00:02:02,640
be looking at a few of them in our
examples. isnumeric will return True if

26
00:02:02,640 --> 00:02:06,459
the character is a number, and False
otherwise. We want to add things that

27
00:02:06,460 --> 00:02:12,080
aren't a digit, which is why the condition
becomes if not char.isnumeric

28
00:02:12,080 --> 00:02:16,020
Alright so I'm going to run the
program to make sure that it works.

29
00:02:16,880 --> 00:02:21,060
So the first thing we get printed out is
the string containing all the separators.

30
00:02:21,060 --> 00:02:24,120
Now that was printed by the code on line
8. I'm just going to move this down so we

31
00:02:24,120 --> 00:02:28,470
can see a bit more code. These strings
created by our for loop on lines 4

32
00:02:28,470 --> 00:02:33,300
through 6. Now we use that string of
separators on line 10 to get the

33
00:02:33,300 --> 00:02:37,140
individual numbers. Once again, don't
worry about the lines 10 and 11 at the

34
00:02:37,140 --> 00:02:40,500
moment, you'll learn all about them later.
At the moment, we're just using them to

35
00:02:40,500 --> 00:02:44,610
show why we want to extract all the
separators from number. Alright so our

36
00:02:44,610 --> 00:02:48,959
program works, but how? What we'll do is
we'll set a breakpoint on the for loop,

37
00:02:48,960 --> 00:02:57,820
line 4 over here, and let's run the program in the debugger.

38
00:02:57,820 --> 00:03:03,380
In the variables pane we can see the value of number.
So it's a string containing 7 numbers, separated by

39
00:03:03,390 --> 00:03:08,489
various separated characters. Below that
we can see that the variable separators

40
00:03:08,489 --> 00:03:12,600
is currently bound to an empty string.
Two quotes with nothing between them is

41
00:03:12,600 --> 00:03:16,440
an empty string. As I said, I'll come back
to that later. Now when I step over the

42
00:03:16,440 --> 00:03:20,880
code, which I'll do now, you can see that
the variable char gets the first

43
00:03:20,880 --> 00:03:25,019
character from the string. That's the
digit 9. We're about to test if it's a

44
00:03:25,019 --> 00:03:30,209
digit, so I'm going to step over line 5
to do that. The character 9 does

45
00:03:30,209 --> 00:03:34,860
represent a number so isnumeric returns
True. We're checking for not isnumeric,

46
00:03:34,860 --> 00:03:39,930
which is False, so therefore the code on
line 6 is skipped. Alright so we're

47
00:03:39,930 --> 00:03:44,850
back now on line 4. I'm gonna step over
again. char now gets the next character from

48
00:03:44,850 --> 00:03:48,870
number which is a comma, as you can see.
A comma obviously doesn't represent a

49
00:03:48,870 --> 00:03:53,820
number and each numeric should return
False. Not False is True and we should

50
00:03:53,820 --> 00:03:59,190
get line 6 executed this time. Let's step
over to check that, and that's good. Line

51
00:03:59,190 --> 00:04:02,670
6 is about to be executed. When you're
using the debugger like this, always

52
00:04:02,670 --> 00:04:06,620
spend time working out what's going to
happen before it does.

53
00:04:06,620 --> 00:04:11,040
That'll really help you to learn how to read code
without ultimately having to run it in

54
00:04:11,040 --> 00:04:16,140
the debugger. Ok separators is currently
an empty string. Line 6 will append the

55
00:04:16,140 --> 00:04:19,649
value of char to it, which should result
in separators becoming a string

56
00:04:19,649 --> 00:04:25,860
containing a comma. Let's step over and
see. In the variables pane, separators is

57
00:04:25,860 --> 00:04:30,390
a comma. So we've seen how char is
getting each of the characters in turn

58
00:04:30,390 --> 00:04:34,530
and we really don't need to keep watching
line 4. We've also said that line 6

59
00:04:34,530 --> 00:04:39,210
is only executed when we get a character
that's not numeric. So it's line 6 that

60
00:04:39,210 --> 00:04:43,230
we're really interested in here. We can
debug more efficiently by clearing the

61
00:04:43,230 --> 00:04:50,300
breakpoint on line 4 and setting one
on line 6 instead, like so.

62
00:04:50,300 --> 00:04:54,380
When we've done that, we can use the button that
looks like a typical video player icon,

63
00:04:54,390 --> 00:04:59,520
a vertical bar on a triangle. That's this
button over here, resume program, and what

64
00:04:59,520 --> 00:05:02,940
that will do is run our code at full
speed until it hits our breakpoint.

65
00:05:02,940 --> 00:05:07,920
So I'm going to click that button now and the
debugger executes the code for the 2, 2

66
00:05:07,920 --> 00:05:13,410
and 3 in number, and char now
contains a semicolon. That's obviously

67
00:05:13,410 --> 00:05:17,400
not numeric and that's why we've got our
breakpoint hit on line 6. When I

68
00:05:17,400 --> 00:05:21,660
execute that line, which I'll come over here
and step over, we can see that the

69
00:05:21,660 --> 00:05:25,980
semicolon is appended to separators. In
the variables pane, separators now has

70
00:05:25,980 --> 00:05:30,720
two characters; a comma and a semicolon.
Now it can be easier to see the values in

71
00:05:30,720 --> 00:05:34,800
the editor pane. This IDE shows them in
grey, as you can see up there. At the end of

72
00:05:34,800 --> 00:05:37,920
line 2 we've got the comma and the
semicolon. We've also got the current

73
00:05:37,920 --> 00:05:42,600
value of char at the end of line 4.
Alright so I'm going to resume the program

74
00:05:42,600 --> 00:05:47,260
again by clicking on the icon, and that
ran past the 3, 7 and 2.

75
00:05:47,260 --> 00:05:51,920
char, as you can see, is now a colon, and line 6
will cause that to be appended to our

76
00:05:51,930 --> 00:05:56,700
separator string when I step over, which
I'll do now. You can see the colon has

77
00:05:56,700 --> 00:06:01,830
been added to separators now. Next we
should get a comma, the one before 7

78
00:06:01,830 --> 00:06:07,140
7 5, so let's resume. It actually
forgot about the space there, you can see

79
00:06:07,140 --> 00:06:10,830
that, obviously, we had a space in the
string which is non numeric. So we've hit

80
00:06:10,830 --> 00:06:15,300
that, so therefore a space should be
added to our string if I step over which

81
00:06:15,300 --> 00:06:19,830
I'll do now, and you can see the string,
the space rather, has been added to our

82
00:06:19,830 --> 00:06:24,330
separators variable. If I come over now
and click on resume, next we should get a

83
00:06:24,330 --> 00:06:30,270
comma and that's the one that's just
before the 775. Resume program, we've got  a comma

84
00:06:30,270 --> 00:06:34,980
in char now. If I step over you can see
that's added to our separators variable,

85
00:06:34,980 --> 00:06:39,150
and resume one more time. This time, 
as the last one that we'll match, we

86
00:06:39,150 --> 00:06:42,840
should get the semicolon which is before
the 807, and there's obviously no other

87
00:06:42,840 --> 00:06:47,130
characters. I'll click on resume.
We've got the semicolon and step over

88
00:06:47,130 --> 00:06:53,040
once more and that's been added to our
separators variable. The remaining three

89
00:06:53,040 --> 00:06:56,340
characters at this point are all numeric,
and that means that we shouldn't get to

90
00:06:56,340 --> 00:06:59,880
line 6 again and resuming the program,
which I'll do now, should cause it to run

91
00:06:59,880 --> 00:07:04,560
through to the end. And you can see
that's been the case there. Now if you

92
00:07:04,560 --> 00:07:07,710
want to use step over until the program
terminates instead of resume program,

93
00:07:07,710 --> 00:07:11,310
that I used there, to be sure about what's
happening then go ahead. It's really

94
00:07:11,310 --> 00:07:14,640
helpful to see each line being executed
when you're learning. As you can see there,

95
00:07:14,640 --> 00:07:18,740
I pressed resume program. The program ended.
We click over to the console now, we can

96
00:07:18,750 --> 00:07:22,650
see the output on screen. We've got our
separators printed out, followed by a

97
00:07:22,650 --> 00:07:26,220
list containing the seven values in
number. So I'm going to stop the video

98
00:07:26,220 --> 00:07:29,820
here but you might want to step through
the code a few more times, just to make

99
00:07:29,820 --> 00:07:33,420
sure you understand what it's doing. And
as a reminder, don't worry about lines 10

100
00:07:33,420 --> 00:07:36,730
and 11. They're quite advanced and won't
make much sense at this point in the course.

101
00:07:36,730 --> 00:07:41,190
In the next video, we'll see how a
code can handle values that a user

102
00:07:41,190 --> 00:07:45,350
types in. See you in the next video.

