Welcome to Exploring Computing.
Today's video is Additional Python Features.
So in this video, we're going to take a look
at a few other Python language features
that you might find handy in certain situations.
So the first one I wanted to talk about
is how to do a multi-line statement.
So sometimes, you have a line of code
that's just too long to fit on a single line comfortably.
And in some languages, you can just go ahead
and split across the line.
You can just say, "Oh, this doesn't fit on the line.
I'm going to go ahead and put a carriage return in the middle.
The language interpreter should figure this out."
And it turns out this does not work in Python.
So if you do what I've done here, where I've taken
name equals and I put in a carriage return there
and then I follow it on the next line with input,
this is actually going to cause an error when you try
and load it.
So that's no good.
So there's several solutions to this.
The one that's universally going to be doable
is you can go ahead and put a backslash at the end
of your unfinished line.
So if I say name equals and I have that backslash--
don't use the forward slash, it's
got to be the backslash-- you've got that backslash, that
tells Python, "I'm not really done with the line.
Go to the next line.
That's actually a continuation of the current line."
In addition, you can use parentheses.
So if you've already got parentheses--
or you've got the input statement there,
I can go ahead and use parentheses
for the input statement.
You can also-- in many situations,
like if we've got a mathematical formula,
you can just put it in parentheses wherever you want.
But if you've got an open parenthesis, like here where
I've got that input and then open parenthesis,
Python immediately realizes that it doesn't see a closing
parenthesis on that current line,
so I must be spreading my statement
across multiple lines, and it will go ahead
and continue processing lines--
the subsequent lines as if they were part of the original line
until it sees that closing parenthesis.
You can do the same thing with the square brackets.
If you've got a list of items like I got here,
it sees that opening square bracket,
and it sees a bunch of people on our team member list.
It doesn't see a closing square bracket,
and it says, "Oh, you must want me
to continue processing the next line and the next line
until I see that closing angular bracket."
All right, another issue that sometimes comes up
is how to do what's called concatenating strings.
In computer science, concatenating strings
means to combine two strings into a single string.
And there's a variety of different ways
to do this, depending upon the programming language.
In Python, you just separate the strings by a space,
and it automatically puts them together.
So if I say, "hello" space "everyone,"
it's going to combine that together into a single string
"helloeveryone."
Notice that it does not actually add a space
into the "helloeveryone."
So I get "helloeveryone."
So if you want a space, either the "hello" string
needs to end with a space or the "everyone" string
needs to start with a space.
Or you need to put in a third in the middle
a quote with a space with a closing
quote in between the "hello" and the "everyone,"
and that will combine to get you "hello" space "everyone."
This can be useful when we have a really long string.
So here I'm using the Stanford motto.
I am not going to attempt to read it.
I don't know German at all.
"The Stanford motto," blah, blah, blah,
"translates as the wind of freedom blows."
And so maybe that's a little bit too long
to fit on a single line.
I want to put it across multiple lines.
And I'm going to do that by breaking it down
into individual strings.
Notice each of these strings, at least the first three strings,
ends with a space, so "translates as"
space, "The Stanford motto" space, dash, space.
Because, remember, if we don't put those spaces in there,
when the string gets combined back together,
we're not going to have spaces.
So don't forget your spaces.
You may also notice that you don't
have closing backslashes on the end of each of these lines.
And that's because I've got that print statement.
So I've got the print, parenthesis,
and so that immediately tells Python
that it should continue processing each
of the following lines as if they were on the same line
until it hits that closing parenthesis.
You can also use triple quotes for this.
So if you've got a long string and you
don't want to bother putting quotes around all of them
and you want it to go across multiple lines,
you can just put a triple quote at the start and then
a triple quote at the end.
And this will also work.
OK.
The last thing I wanted to mention
in our short little video on Additional Python Features
is how you write a comment in Python.
And so you recall that a comment is
used when we want to convey information to other humans who
are reading our code, but we want it
to be ignored by the computer.
So here, the number sign is used by Python and when--
for comments, and when Python sees a number sign,
it ignores everything following the number
sign until the end of the line.
That's considered a comment.
The Python thinks, "Oh, that's something for human beings.
I'm not a human being, so I'm going
to go ahead and ignore it."
Here, we've got a number sign "define variable."
So that whole line-- it starts with a number sign,
so that whole line is just going to get completely
ignored by the computer.
The computer's just going to be like, "Oh, this
must be something that Patrick wants for other people reading
the code to know, but I'm not a person.
I'm a computer.
I'm better than a person.
I'm a computer, so I'm going to just ignore that line.
So it completely ignores that first line.
And then, the next line, total equals zero, number sign,
"total number of students is initially zero."
It is going to process the total equals zero.
That's not behind a number sign.
But then, as soon as it sees that number sign, it says, "Oh,
the rest of this line, that's for humans.
I'm going to go ahead and ignore it."
So this whole thing, number sign "define variables,"
total equals zero, number sign "total number of students
is initially zero," is going to get treated by the computer
as just total equals zero.
All right.
That's it for today.
We've got one more lecture on Python programming.
I'll see you soon.