1
00:00:05,330 --> 00:00:09,100
So in the previous video, we started
talking about variables for Python and

2
00:00:09,100 --> 00:00:11,900
thus, we ended talking
about operator precedence.

3
00:00:11,900 --> 00:00:16,710
So you saw that by using brackets we can
make sure that the calculations we think

4
00:00:16,710 --> 00:00:18,830
work, use the correct order or

5
00:00:18,830 --> 00:00:23,220
actually are processed in the right order
to avoid getting us incorrect results.

6
00:00:23,220 --> 00:00:26,660
And that's mainly by putting brackets
around each sort of section.

7
00:00:26,660 --> 00:00:29,580
Now there's another way of doing that,
that would be easier, and

8
00:00:29,580 --> 00:00:32,140
in some cases you may wanna
instead look at something

9
00:00:32,140 --> 00:00:35,330
that's relatively complex, like you
can see with that print statement.

10
00:00:35,330 --> 00:00:38,240
You can actually use variables
to hold the intermediate values.

11
00:00:38,240 --> 00:00:44,040
So for example, we could say,
a variable with the name of c = a + b.

12
00:00:46,130 --> 00:00:49,510
And then we can also do d = c / 3.

13
00:00:49,510 --> 00:00:54,000
And then we could do e = d / 4.

14
00:00:54,000 --> 00:00:56,580
And then we could do print(e * 12).

15
00:00:56,580 --> 00:01:01,190
Now if we actually run that, we should
get the same result in both cases.

16
00:01:02,310 --> 00:01:03,690
We get 12 for this line here.

17
00:01:03,690 --> 00:01:07,150
But we also get 12 for this line here.

18
00:01:07,150 --> 00:01:10,340
And that's because we've broken up
those individual components, and

19
00:01:10,340 --> 00:01:13,080
assigned them to different
variables, to get the same result.

20
00:01:13,080 --> 00:01:17,110
So definitely there's gonna be times where
it makes sense to break up an expression

21
00:01:17,110 --> 00:01:18,860
like this into smaller parts.

22
00:01:18,860 --> 00:01:21,720
It's really gonna be up to you to
compare the two examples to see which

23
00:01:21,720 --> 00:01:23,010
one works better for you.

24
00:01:23,010 --> 00:01:26,320
Once you start getting used
to the concept of brackets,

25
00:01:26,320 --> 00:01:29,850
it's easy to see actually at a glance
with that one, as to what's happening

26
00:01:29,850 --> 00:01:33,240
with this one because you're
introducing different variables.

27
00:01:33,240 --> 00:01:36,710
It can take a bit of work to look at that
because you've actually got more output

28
00:01:36,710 --> 00:01:37,290
to look at.

29
00:01:37,290 --> 00:01:39,840
But the bottom line is there's no right or
wrong here.

30
00:01:39,840 --> 00:01:42,810
It's whatever's more appropriate and
more readable for you.

31
00:01:42,810 --> 00:01:46,960
So, moving on now, let's actually start
talking about another data type and

32
00:01:46,960 --> 00:01:48,930
that's the data type string.

33
00:01:48,930 --> 00:01:52,770
Now you've seen us use strings in earlier
examples, to print messages on the screen.

34
00:01:52,770 --> 00:01:55,930
But I want to go through now and show you
some of the other cool things you can do

35
00:01:55,930 --> 00:01:57,750
when you're actually working with strings.

36
00:01:57,750 --> 00:02:01,580
So strings are one of Python's what
they call sequence data types,

37
00:02:01,580 --> 00:02:03,540
which makes sense because
that's what it really is.

38
00:02:03,540 --> 00:02:06,480
It's really a sequence of characters
that you can put together

39
00:02:06,480 --> 00:02:08,210
that form an actual string.

40
00:02:08,210 --> 00:02:10,960
So we've seen how to print them out and
concatenate strings together.

41
00:02:10,960 --> 00:02:13,730
But we can also pick out individual
characters or substrings.

42
00:02:13,730 --> 00:02:17,460
So I'm gonna delete some of this now,
and let's just see how that works.

43
00:02:17,460 --> 00:02:21,180
I'm gonna type parrot = "Norwegian Blue".

44
00:02:21,180 --> 00:02:23,090
We can print(parrot).

45
00:02:23,090 --> 00:02:29,180
And no surprises there, if we run
that to see what actually happens.

46
00:02:30,590 --> 00:02:37,270
Norwegian Blue, we should expect, we can
actually do print(parrot[3]) like so.

47
00:02:39,560 --> 00:02:43,960
If you run that, you can see, what's
happened is by doing that it's

48
00:02:43,960 --> 00:02:46,170
actually printed the letter w.

49
00:02:46,170 --> 00:02:49,190
Now you might be looking at that and
thinking, "well why is that w?"

50
00:02:49,190 --> 00:02:53,530
because we specified the third character,
so shouldn't that be counting from left to

51
00:02:53,530 --> 00:02:57,640
right, N is the first character,
o is the second and r is the third.

52
00:02:57,640 --> 00:03:00,190
And in actual fact w is
the fourth character.

53
00:03:00,190 --> 00:03:01,270
So why did that work?

54
00:03:01,270 --> 00:03:04,860
It's actually usual for programming
languages to start counting at zero,

55
00:03:04,860 --> 00:03:06,260
and not at one.

56
00:03:06,260 --> 00:03:08,000
So that's parrot zero is N.

57
00:03:08,000 --> 00:03:10,290
So we can actually go to confirm that.

58
00:03:12,430 --> 00:03:15,720
Parrot[0], if we actually
run that is an N.

59
00:03:15,720 --> 00:03:19,320
So you'll find that in most languages,
as I said, they'll actually start counting

60
00:03:19,320 --> 00:03:22,870
from position zero and not from position
one as we would normally do as humans.

61
00:03:22,870 --> 00:03:25,720
So there's 14 characters in
Norwegian Blue, numbered from zero to 13.

62
00:03:25,720 --> 00:03:30,110
If we try to print(parrot[14]), we get
an error, string index out of range.

63
00:03:30,110 --> 00:03:37,680
So if we actually try and do that So 14.

64
00:03:37,680 --> 00:03:38,530
We'll actually get an error.

65
00:03:38,530 --> 00:03:44,180
And that's because as far as it's
concerned, there's no character

66
00:03:44,180 --> 00:03:47,640
at that position, because there's
only characters in position 0 to 13.

67
00:03:47,640 --> 00:03:50,340
The other thing you can
actually do is count backwards.

68
00:03:50,340 --> 00:03:53,070
So, you can type print(parrot[-1]).

69
00:03:53,070 --> 00:03:57,430
And if we actually run that,
negative one gives us e. So

70
00:03:57,430 --> 00:04:04,130
what's happened is that it started here at
position zero and went to negative one.

71
00:04:04,130 --> 00:04:06,120
Negative one goes all to the way
to the end of the string and

72
00:04:06,120 --> 00:04:08,020
starts counting from the end.

73
00:04:08,020 --> 00:04:12,550
So we get the letter e coming up, which
is the last character from the string.

74
00:04:12,550 --> 00:04:13,820
And we can work backwards like that.

75
00:04:13,820 --> 00:04:16,839
We can also print what
Python calls a slice by,

76
00:04:16,839 --> 00:04:20,740
instead of providing one number,
provide two numbers separated by a colon.

77
00:04:20,740 --> 00:04:22,320
So in other words,
it's gonna be like a range.

78
00:04:22,320 --> 00:04:28,260
So, we can do print(parrot[0:6]),

79
00:04:28,260 --> 00:04:33,630
if you run that, you get Norweg,
which is the six characters starting,

80
00:04:33,630 --> 00:04:36,720
which is the length of six characters
starting at position zero.

81
00:04:36,720 --> 00:04:41,290
So the first number is the number in
the string to start, printing out, and

82
00:04:41,290 --> 00:04:43,790
the sixth is how many characters
to actually print out.

83
00:04:43,790 --> 00:04:45,880
So it's actually two
ways at looking at this.

84
00:04:45,880 --> 00:04:51,100
First, you could say that
printing parrot[0:6] starts from

85
00:04:51,100 --> 00:04:56,350
character's position index zero up to but
not including index position six.

86
00:04:56,350 --> 00:04:59,420
But we can also say the six
character starting from index zero.

87
00:04:59,420 --> 00:05:03,440
But in Python, the first is technically
more correct even if it seems to amount

88
00:05:03,440 --> 00:05:07,090
where they both seem, well actually both
do end up with showing the same amount.

89
00:05:07,090 --> 00:05:08,580
If the first number is omitted,

90
00:05:08,580 --> 00:05:11,130
the slice is gonna start from
the beginning of the string.

91
00:05:11,130 --> 00:05:14,000
If the second number is omitted,
it's gonna run to the end of the string.

92
00:05:14,000 --> 00:05:15,360
Just to show you what I mean by that.

93
00:05:15,360 --> 00:05:23,680
Type print(parrot[:6].

94
00:05:23,680 --> 00:05:25,040
That's gonna start from automatically,

95
00:05:25,040 --> 00:05:26,540
because we haven't
specified a number here.

96
00:05:26,540 --> 00:05:29,410
It's gonna start from
the start of the string.

97
00:05:29,410 --> 00:05:31,680
That's gonna print the same
as the previous line.

98
00:05:31,680 --> 00:05:34,930
But if we also did print(parrot[6:]) and

99
00:05:34,930 --> 00:05:39,710
didn't specify the second number,
it's gonna run to the end of the string.

100
00:05:39,710 --> 00:05:43,800
So if you run that, you can see it's
moved, started at the position, so

101
00:05:43,800 --> 00:05:47,080
it's actually went to effectively
the seventh position, so it's counting,

102
00:05:47,080 --> 00:05:51,600
zero, one, two, three,
four, five, six, ian Blue.

103
00:05:51,600 --> 00:05:54,880
So ian Blue and it's going along because
we haven't specified where to end,

104
00:05:54,880 --> 00:05:58,180
so it's actually moved on to the end,
continued and done the entire string.

105
00:05:58,180 --> 00:06:00,140
And if you start dealing
with negative indices,

106
00:06:00,140 --> 00:06:03,000
it quickly becomes apparent why
the second definition is not gonna work.

107
00:06:03,000 --> 00:06:09,430
So in other words, if we start
trying to do this, print(parrot[-4:2])

108
00:06:09,430 --> 00:06:16,370
that's not actually gonna print anything.

109
00:06:16,370 --> 00:06:18,750
That's because you can't go back
from the starting position in this

110
00:06:18,750 --> 00:06:19,610
context.

111
00:06:19,610 --> 00:06:22,900
But if we actually go back and
put a -2 there, and run that.

112
00:06:24,880 --> 00:06:28,360
You can see it's actually started and
produced Bl and that's because it

113
00:06:28,360 --> 00:06:31,850
started from the start of the string, it's
gone back four characters, up to and not

114
00:06:31,850 --> 00:06:36,030
including the second to last character
in the string which was the u.

115
00:06:36,030 --> 00:06:39,220
Another thing you can actually do
is you can actually do a print.

116
00:06:39,220 --> 00:06:42,540
You can actually do funny slices that
skip characters, counting in steps.

117
00:06:42,540 --> 00:06:48,380
So I could do print(parrot[0:6:2]).

118
00:06:48,380 --> 00:06:50,340
And that should give us the answer Nre.

119
00:06:50,340 --> 00:06:53,270
So we run that, Nre as you can see.

120
00:06:53,270 --> 00:06:57,900
So starting from index position zero,
extract all the characters up to, but

121
00:06:57,900 --> 00:07:01,210
not including index position
six in steps of two.

122
00:07:01,210 --> 00:07:05,200
As you saw in the first example and
that started on N.

123
00:07:05,200 --> 00:07:07,690
And skip to r and skip to e.

124
00:07:07,690 --> 00:07:14,520
And we can change that and
do something like print(parrot[0:6:3]),

125
00:07:14,520 --> 00:07:19,400
so make the step increment three, and
we should get N and w in that case, and

126
00:07:19,400 --> 00:07:21,740
w because it's skipping
three characters at time.

127
00:07:21,740 --> 00:07:24,550
So slightly useless application, but

128
00:07:24,550 --> 00:07:28,910
I'm gonna show you an example.
Type number = "9.

129
00:07:28,910 --> 00:07:31,820
And there's numbers here,

130
00:07:31,820 --> 00:07:37,900
223,372,036,854,775,807".

131
00:07:37,900 --> 00:07:42,410
And if I actually do
a print(number[1::4]) with two colons,

132
00:07:42,410 --> 00:07:47,460
if I actually run that, you can see how
it's actually skipped the numbers and

133
00:07:47,460 --> 00:07:52,150
actually just printed out the commas
at each position automatically.

134
00:07:52,150 --> 00:07:55,210
So that's a good way to step through
and extract portions out of the particular

135
00:07:55,210 --> 00:07:56,760
string that you wanna work with.

136
00:07:56,760 --> 00:08:01,810
But using a better example,
let's just try numbers = "1,

137
00:08:01,810 --> 00:08:06,760
2, 3, and notice I'm putting
one space after each, 4,

138
00:08:06,760 --> 00:08:09,500
5, 6, 7, 8, 9" like so.

139
00:08:09,500 --> 00:08:14,840
And if we actually do, print(number

140
00:08:14,840 --> 00:08:20,200
print(numbers[0::3]).

141
00:08:20,200 --> 00:08:21,170
We actually run that.

142
00:08:22,580 --> 00:08:25,280
You can see we've quickly
extracted out the numbers, and

143
00:08:25,280 --> 00:08:27,270
we've avoided the commas and
the spaces there.

144
00:08:27,270 --> 00:08:30,640
So you can see how it would be useful, if
you wanted to extract information out in

145
00:08:30,640 --> 00:08:31,620
a particular format.

146
00:08:31,620 --> 00:08:34,760
So moving on, let's talk a little
bit about string operators.

147
00:08:34,760 --> 00:08:36,130
So we've talked about operators before,
and

148
00:08:36,130 --> 00:08:39,470
you've seen the plus operator when
we use strings to concatenate.

149
00:08:39,470 --> 00:08:43,710
So if we had something like this.

150
00:08:43,710 --> 00:08:46,810
String1 = "he's "

151
00:08:46,810 --> 00:08:49,180
String2 = "probably ".

152
00:08:51,170 --> 00:08:55,930
We could do print(string1 + string2).

153
00:08:55,930 --> 00:08:57,490
We've seen that before.

154
00:08:57,490 --> 00:09:00,590
If we run that, the two strings
get added together, he's probably.

155
00:09:00,590 --> 00:09:02,280
But if you're using a string literal, and

156
00:09:02,280 --> 00:09:05,710
that's when you're just typing the value
in direct and you're not using a variable,

157
00:09:05,710 --> 00:09:07,560
you don't actually need
to use the plus sign.

158
00:09:07,560 --> 00:09:12,440
So we could do something like this,
we could do

159
00:09:12,440 --> 00:09:17,710
print("he's " "probably " "pining").

160
00:09:17,710 --> 00:09:18,730
We can run that.

161
00:09:19,910 --> 00:09:23,710
We can see how Python quite happily allows
you do actually do something like that.

162
00:09:23,710 --> 00:09:25,640
Now I'm mentioning that just
to show you how it works.

163
00:09:25,640 --> 00:09:28,780
I wouldn't think you'll ever wanna do
something like that because it's a little

164
00:09:28,780 --> 00:09:29,920
bit totally confusing.

165
00:09:29,920 --> 00:09:33,110
And probably not really useful because
you can obviously just do this,

166
00:09:33,110 --> 00:09:37,310
you can actually delete that out anyway
and obviously you get the same result.

167
00:09:37,310 --> 00:09:41,010
But it's just that, showing you
that it can potentially be done.

168
00:09:41,010 --> 00:09:43,480
So next,
you can actually multiply strings.

169
00:09:43,480 --> 00:09:46,490
It sounds a bit weird because
multiplication generally is only on

170
00:09:46,490 --> 00:09:52,460
numbers, but if you do something
like this: print("Hello " *5).

171
00:09:52,460 --> 00:09:54,850
Python will give you,
Hello Hello Hello Hello Hello so

172
00:09:54,850 --> 00:09:57,290
it will give you whatever's
in the string.

173
00:09:57,290 --> 00:09:58,340
And it will do that five times.

174
00:09:58,340 --> 00:10:00,190
It's sort of like appending
to that five times.

175
00:10:00,190 --> 00:10:02,510
And operator precedence applies.

176
00:10:02,510 --> 00:10:07,780
So if we do something
like print("Hello " *5

177
00:10:07,780 --> 00:10:12,340
+4) remembering what we know
about operator precedence.

178
00:10:12,340 --> 00:10:13,040
We try and run that.

179
00:10:14,400 --> 00:10:18,210
We actually get an error, can't convert
int object into a string implicitly.

180
00:10:18,210 --> 00:10:21,890
And that's because, hello,
the times 5, this part here,

181
00:10:21,890 --> 00:10:26,700
that actually evaluates to a string, and
the attempt to then add 4 to it fails,

182
00:10:26,700 --> 00:10:31,440
and that's really not the way to do it,
so if you want to do something like that,

183
00:10:31,440 --> 00:10:34,510
you'd actually either put it brackets,
like so.

184
00:10:34,510 --> 00:10:37,870
And if you run that you then
get the correct result.

185
00:10:37,870 --> 00:10:42,530
Alternatively you would type
print("Hello " * 5 + "4").

186
00:10:42,530 --> 00:10:45,800
If you run that, that's

187
00:10:45,800 --> 00:10:50,470
actually if you wanted to append the number

188
00:10:50,470 --> 00:10:55,520
four onto the end of the strings that
have been modified out five times.

189
00:10:55,520 --> 00:10:58,410
There's also an operator
to check if one string

190
00:10:58,410 --> 00:11:01,850
is a substring of another, which can
be useful for searching purposes.

191
00:11:01,850 --> 00:11:04,970
So we did something like today = "friday".

192
00:11:07,010 --> 00:11:10,630
We can do print("day" in today).

193
00:11:10,630 --> 00:11:13,310
So that's the in operator.

194
00:11:13,310 --> 00:11:15,070
So that should return True.

195
00:11:15,070 --> 00:11:18,340
Returns a true or
false to indicate whether it thinks

196
00:11:18,340 --> 00:11:22,650
that whatever we've typed here, day in
this case, is actually in that word.

197
00:11:22,650 --> 00:11:24,940
So if we run that, we should get True.

198
00:11:24,940 --> 00:11:26,330
And so you've got True there on the screen.

199
00:11:26,330 --> 00:11:30,510
If we did something like print("fri" in

200
00:11:30,510 --> 00:11:34,690
today) we should also get a True,
which we'll test shortly.

201
00:11:34,690 --> 00:11:38,710
And if we do something
different like print("thur" in

202
00:11:38,710 --> 00:11:41,750
today) we should get a False for that.

203
00:11:41,750 --> 00:11:44,430
And lastly, if we did something like,

204
00:11:44,430 --> 00:11:49,640
("parrot" in "fjord") that should
obviously be a False as well.

205
00:11:49,640 --> 00:11:54,960
So True, True, "day" in today,
if "fri" in today. if Thursday or

206
00:11:54,960 --> 00:11:58,510
Thur is not in that variable called today.

207
00:11:58,510 --> 00:12:03,130
And, obviously the word,
parrot, is not in fjord either.

208
00:12:03,130 --> 00:12:06,170
So that's you got some basic
information about operators.

209
00:12:06,170 --> 00:12:09,430
And I hope you got a lot out of
the two videos on variables in Python.

210
00:12:09,430 --> 00:12:11,240
In the next video,
we're gonna take it to the next level.

211
00:12:11,240 --> 00:12:14,740
We're gonna look at various ways that
we can format strings, and also some of

212
00:12:14,740 --> 00:12:18,510
the string methods that are built into
Python, to allow us to manipulate them.

213
00:12:18,510 --> 00:12:19,900
So, I'll see you in that next video.

