1
00:00:05,320 --> 00:00:09,070
So continuing on from the previous
video in our introduction to Python, so

2
00:00:09,070 --> 00:00:10,830
we've learned about the print command and

3
00:00:10,830 --> 00:00:14,980
we've learned a little bit about streams
and a little bit about operators and

4
00:00:14,980 --> 00:00:17,200
a little bit about adding numbers and
so forth together.

5
00:00:17,200 --> 00:00:18,910
And let's now use another function.

6
00:00:18,910 --> 00:00:21,360
So what we can do to make
this a bit easier and

7
00:00:21,360 --> 00:00:24,880
a bit more useful is we can
create a way to actually show

8
00:00:24,880 --> 00:00:28,450
a name that's entered from
the keyboard on the screen.

9
00:00:28,450 --> 00:00:30,990
So what I'm gonna do is I'm gonna
delete this other code that has

10
00:00:30,990 --> 00:00:33,120
already been used,
cuz we don't need that anymore.

11
00:00:33,120 --> 00:00:36,390
Complete delete all that and
we're gonna start by typing greeting.

12
00:00:36,390 --> 00:00:38,160
And greeting again is
the name of our variable,

13
00:00:38,160 --> 00:00:39,940
I will be talking more
about variables shortly.

14
00:00:41,040 --> 00:00:41,960
Hello.

15
00:00:41,960 --> 00:00:48,560
We can type name = input
("Please enter your name ").

16
00:00:50,960 --> 00:00:58,540
Then we can type print(greeting + notice
I've added a space there, plus Name.

17
00:00:58,540 --> 00:01:01,390
And we just run that to see what it does.

18
00:01:01,390 --> 00:01:04,510
And the other thing is once you
actually have got the run window open,

19
00:01:04,510 --> 00:01:07,910
and we don't need to right click,
although we can run it that way,

20
00:01:07,910 --> 00:01:11,150
you can also just come over here and click
on the green arrow and run it that, so

21
00:01:11,150 --> 00:01:12,840
it's asking us to please enter our name.

22
00:01:12,840 --> 00:01:16,970
I can type in Tim, press enter, and
it actually comes up and says, Hello Tim.

23
00:01:16,970 --> 00:01:18,870
So it's actually looked
at what we've typed, and

24
00:01:18,870 --> 00:01:22,100
it's recorded that into
this variable called name.

25
00:01:22,100 --> 00:01:25,900
And the message has then printed the
greeting, which is hello, another space,

26
00:01:25,900 --> 00:01:28,380
then added what we've typed,
which is in this case, name.

27
00:01:28,380 --> 00:01:31,260
So essentially as you saw, the input
function displays the text that's

28
00:01:31,260 --> 00:01:34,060
been provided to it then waits for
text to be entered into the keyboard.

29
00:01:34,060 --> 00:01:36,810
So, in other words, display this
message: please enter your name,

30
00:01:36,810 --> 00:01:39,770
then waited until we typed something,
then we actually pressed enter.

31
00:01:39,770 --> 00:01:43,120
The text, at that point, you typed is
stored in the variable name as a set, and

32
00:01:43,120 --> 00:01:45,260
that's used by the print function
to actually show the greeting.

33
00:01:45,260 --> 00:01:48,200
Now in terms of strings there's
also a special character called

34
00:01:48,200 --> 00:01:49,210
the backslash character.

35
00:01:49,210 --> 00:01:52,810
That's actually got a special meaning
in strings, so it's used to escape

36
00:01:52,810 --> 00:01:55,680
the character that follows it to
give that character special meaning.

37
00:01:55,680 --> 00:01:58,130
So there's lots of different
control characters, and

38
00:01:58,130 --> 00:01:59,490
we won't go through them all now.

39
00:01:59,490 --> 00:02:02,050
But some of them will actually
do things like start a new line,

40
00:02:02,050 --> 00:02:04,430
or provide a tab to actually
provide tabbed output.

41
00:02:04,430 --> 00:02:06,080
So let's just go through and
look at a couple of those.

42
00:02:06,080 --> 00:02:10,620
So we can type something like

43
00:02:10,620 --> 00:02:15,730
'splitString = "This string

44
00:02:15,730 --> 00:02:20,640
has been, I am typing
split

45
00:02:20,640 --> 00:02:25,960
over
several
lines"'.

46
00:02:25,960 --> 00:02:26,640
And if we print that.

47
00:02:27,750 --> 00:02:30,710
And that's when I typed the first
few characters of the variable,

48
00:02:30,710 --> 00:02:33,680
IntelliJ quite nicely comes up and
says, or do you mean splitString.

49
00:02:33,680 --> 00:02:36,510
So again if you want that, you don't
have to type the whole variable name.

50
00:02:36,510 --> 00:02:39,330
You can just press enter at that point,
and it actually puts the rest in for

51
00:02:39,330 --> 00:02:40,160
us automatically.

52
00:02:40,160 --> 00:02:43,460
So if I run that, or actually I need to
finish off that from the last one, so

53
00:02:43,460 --> 00:02:44,870
I'll pop my name in.

54
00:02:44,870 --> 00:02:48,070
I can see how that worked, and let me just
actually delete this first part up here.

55
00:02:48,070 --> 00:02:49,720
Actually what I'll do,
is I'll actually comment it out.

56
00:02:49,720 --> 00:02:54,620
So I can press Cmd+Slash on my Mac, and
notice how I put the slashes in, and

57
00:02:54,620 --> 00:02:57,640
down at the bottom it tells you
if press Ctrl+Slash on Windows or

58
00:02:57,640 --> 00:02:58,860
Linux machines it'll do the same.

59
00:02:58,860 --> 00:03:01,190
So, what I've done is I've
commented that code out,

60
00:03:01,190 --> 00:03:03,390
which means that that now won't run.

61
00:03:03,390 --> 00:03:06,110
Because, remember, a comment is
there purely for your reference.

62
00:03:06,110 --> 00:03:06,910
So if I run this again,

63
00:03:06,910 --> 00:03:09,870
we'll finally get straight to this
little example of the string.

64
00:03:09,870 --> 00:03:11,440
So you can see what's happened in this,

65
00:03:11,440 --> 00:03:14,890
has actually caused the cursor 
to start on the next line.

66
00:03:14,890 --> 00:03:18,190
So consequently, for each one of
those
's we added that actually

67
00:03:18,190 --> 00:03:20,380
spit onto a separate
line each time we did it.

68
00:03:20,380 --> 00:03:23,180
We could do something
similar with a tab string.

69
00:03:26,640 --> 00:03:34,360
So if we type tabbedString =
"1\t2\t3\t4\t5" and another tab again.

70
00:03:34,360 --> 00:03:37,250
And if we do print tabbedString,
if you run that,

71
00:03:37,250 --> 00:03:40,180
you can see how that's done
something a little bit different.

72
00:03:40,180 --> 00:03:42,950
It's used a standard tab function
which you've probably seen in a word

73
00:03:42,950 --> 00:03:45,910
processor before to tab things out which
is a way of sort of organizing things

74
00:03:45,910 --> 00:03:47,270
in a certain position.

75
00:03:47,270 --> 00:03:50,300
So basically,
the point is that the backslash character

76
00:03:50,300 --> 00:03:53,070
is actually interpreted
specifically by Python.

77
00:03:53,070 --> 00:03:56,650
And then depending on the character
that is typed after that backslash, and

78
00:03:56,650 --> 00:03:59,100
then it performs a special
function in that stream.

79
00:03:59,100 --> 00:04:00,470
So that's your sort of slash end,

80
00:04:00,470 --> 00:04:04,780
the first one, it cause it goes to the
start of a new line, and then the slash t

81
00:04:04,780 --> 00:04:08,710
cause it to tab to the next tab position
before I started the printing again.

82
00:04:08,710 --> 00:04:12,200
You can also use that to escape
special characters such as quote and

83
00:04:12,200 --> 00:04:15,940
double quotes, and that's useful if you've
got a string containing both characters.

84
00:04:15,940 --> 00:04:17,920
And probably the example would be
the best thing to show you there.

85
00:04:17,920 --> 00:04:19,959
I'll comment that out again.

86
00:04:21,870 --> 00:04:26,890
So I come down here and I'll type

87
00:04:26,890 --> 00:04:33,230
print ("The pet shop owner said "No,

88
00:04:33,230 --> 00:04:36,970
no, \'e\'s").

89
00:04:36,970 --> 00:04:39,330
You'll probably recognize this
if you're a Monty Python fan.

90
00:04:45,500 --> 00:04:47,340
I'll just close this down so
I can see this a bit better.

91
00:04:47,340 --> 00:04:49,780
That should have actually been a single
quote at the start and not a double.

92
00:04:49,780 --> 00:04:53,950
So what we are doing, is we are saying
that we are using both single quotes and

93
00:04:53,950 --> 00:04:57,110
double quotes in a particular string
which you may wanna use some time.

94
00:04:57,110 --> 00:05:00,360
Bearing in mind that we started off with
this string by saying it's a single quote.

95
00:05:00,360 --> 00:05:03,740
But by putting the backslash in front of
the quote you wanna use, that actually

96
00:05:03,740 --> 00:05:07,490
tells the string, or tells Python, to
treat it not as a special command to close

97
00:05:07,490 --> 00:05:10,180
the string in this case, but to set the
string that can actually be printed out.

98
00:05:10,180 --> 00:05:13,040
And you can also do something
conversely like this as well,

99
00:05:13,040 --> 00:05:14,810
which will give you the same results.

100
00:05:16,610 --> 00:05:17,380
Pet shop owner.

101
00:05:19,780 --> 00:05:20,300
Said,

102
00:05:32,130 --> 00:05:32,710
Like that.

103
00:05:32,710 --> 00:05:35,070
So if we actually run that now so
you can see what I mean.

104
00:05:37,500 --> 00:05:40,450
And you can see what we've done there,
and basically got the same result.

105
00:05:40,450 --> 00:05:42,790
But how we've used the start and
the end quotes and

106
00:05:42,790 --> 00:05:46,750
the slashes has caused the screen to
actually print out in the same way.

107
00:05:46,750 --> 00:05:50,040
So in the first example what the pet
owner said was in double quotes but

108
00:05:50,040 --> 00:05:53,230
his statement includes apostrophes so
we had to escape one or the other.

109
00:05:53,230 --> 00:05:55,190
And that's actually what
we did in those examples.

110
00:05:55,190 --> 00:05:59,250
So in this case the first method, the
first part of this was actually the third.

111
00:05:59,250 --> 00:06:00,570
If there's print statement in other words.

112
00:06:00,570 --> 00:06:04,380
For no other reason other than the fact
that if you type in two consecutive quotes

113
00:06:04,380 --> 00:06:08,030
either a single one or a double one,
IntelliJ will type a third one for you.

114
00:06:08,030 --> 00:06:10,800
Now, you might be wondering why would
you actually want to actually use three

115
00:06:10,800 --> 00:06:15,540
consecutive quotes, well this is actually
a feature of Python, and what it does,

116
00:06:15,540 --> 00:06:20,000
it's actually provided by Python to make
strings that spanned lines more readable.

117
00:06:20,000 --> 00:06:22,900
So, in other words, the string that we
split over several lines earlier is not

118
00:06:22,900 --> 00:06:25,340
easy to read because
of the embedded slash.

119
00:06:25,340 --> 00:06:29,370
And as you saw Python allows a string to
be split over several lines without doing

120
00:06:29,370 --> 00:06:34,710
that by using triple quotes, and the way
to do that, we type something like this.

121
00:06:34,710 --> 00:06:39,760
AnotherSplitString = """ This

122
00:06:39,760 --> 00:06:46,840
string has been split
over several lines""".

123
00:06:47,980 --> 00:06:50,920
There's the three quotes, and my typing.

124
00:06:50,920 --> 00:06:55,060
Okay, so that's actually a way to
actually create the string, and

125
00:06:55,060 --> 00:06:57,230
without having to use any backslashes.

126
00:06:57,230 --> 00:06:59,680
So, in other words, you wanna lay
out a string with breaks, but

127
00:06:59,680 --> 00:07:02,040
you don't want every line to
start a new line with printed,

128
00:07:02,040 --> 00:07:05,100
you can escape the end of
the line with slash dot as well.

129
00:07:05,100 --> 00:07:08,590
Let's see what that looks like so
print another string, anotherSplitString.

130
00:07:09,670 --> 00:07:13,210
Now, if I run that, this string
has been split over several lines.

131
00:07:13,210 --> 00:07:16,010
I can see it has done the equivalent
function to the slash

132
00:07:16,010 --> 00:07:17,370
ns that we were using earlier.

133
00:07:17,370 --> 00:07:20,830
The other thing you can do is, if you
wanna lay out a string with lots of breaks

134
00:07:20,830 --> 00:07:23,550
but you don't want everyone to
start a new line when printed,

135
00:07:23,550 --> 00:07:26,420
what you can do is escape the end
of the line with a slash dot.

136
00:07:26,420 --> 00:07:29,510
So experiment adding a backslash to the
end of the lines to see what happens when

137
00:07:29,510 --> 00:07:30,540
you run the program.

138
00:07:30,540 --> 00:07:33,120
And getting back to
the Monty Python example again,

139
00:07:33,120 --> 00:07:35,800
we can actually also do
something like this.

140
00:07:35,800 --> 00:07:40,440
We can type using the triple quotes,

141
00:07:40,440 --> 00:07:46,420
we can do (' ' ' The pet
shop owner said "No,

142
00:07:46,420 --> 00:07:53,340
no, 'e's' uh,...he's resting" ' ' ').

143
00:07:53,340 --> 00:07:55,110
Like so, which is another way of doing it.

144
00:07:55,110 --> 00:07:59,230
So you can see that the triple quote
feature is actually quite handy.

145
00:07:59,230 --> 00:08:00,790
And conversely we could
do something like this.

146
00:08:00,790 --> 00:08:02,710
We can just copy that and paste it.

147
00:08:02,710 --> 00:08:06,100
I could change the single
quotes there to double quotes.

148
00:08:07,660 --> 00:08:12,010
Like so, and again if you run that,
we get the same result, so

149
00:08:12,010 --> 00:08:16,310
that can be quite handy to look
at using those triple quotes.

150
00:08:16,310 --> 00:08:20,390
The other thing I just wanna point out is
in the case where you've got a quote and

151
00:08:20,390 --> 00:08:22,360
you're gonna go to have
four quotes in a row.

152
00:08:22,360 --> 00:08:24,210
That doesn't work too well, and

153
00:08:24,210 --> 00:08:27,190
you actually find that Python gets itself
into a little bit of a knot with that.

154
00:08:27,190 --> 00:08:30,860
So if you are using the three quotes,
you need to actually have a space before

155
00:08:30,860 --> 00:08:32,600
to make sure you haven't
got four quotes in a row,

156
00:08:32,600 --> 00:08:35,500
which actually [LAUGH] again you saw
that Python doesn't handle too well.

157
00:08:35,500 --> 00:08:37,669
So that serves as a basic introduction.

158
00:08:37,669 --> 00:08:39,840
And look it's only a basic introduction.

159
00:08:39,840 --> 00:08:41,950
What we're gonna do next,
is we're going to the next video.

160
00:08:41,950 --> 00:08:43,450
We're gonna start talking about variables.

161
00:08:43,450 --> 00:08:46,560
Because we sorta glossed over that
a little bit when you talked about,

162
00:08:46,560 --> 00:08:47,920
very basically what they are.

163
00:08:47,920 --> 00:08:51,400
But we're gonna actually talk about that
more in detail, what variables are, and

164
00:08:51,400 --> 00:08:51,940
how to use them.

165
00:08:51,940 --> 00:08:53,450
So we'll start that in the next video.

