In this video we're going to present expressions.
Expressions are a fundamental building block of Python code.
So it's important that we understand what is and what ...
is not an expression and how expressions differ from other constructs.
What is an expression?
An expression is a syntactic entity---
now, that just means that it's ...
constructed of syntactically valid elements in a valid order---
and an expression is something that can be ...
evaluated---it resolves to some value.
We're used to thinking one plus one equals two.
One plus one is an expression
and it evaluates to two.
That's the basic idea.
Now an expression may be a combination of a number of ...
different things.
This quote here indicates that
an expression can consist of constants (which I will call "literals"),
variables, functions, ...
and operators.
In this video we're just going to look at constants or what ...
we call "literals" ...
and a few operators. And then we'll expand on this ...
definition of an expression as we learn more programming ...
constructs in Python.
Literals
are expressions. So the string 'Hello World' ...
is an expression.
That's perfectly fine. 42 is an integer and that's an expression.
This thing here that follows ...
0.3 0 0 0 0 0 ending with 4,
that's also an expression. It's a literal value that ...
we call a "floating-point number."
That means it has some
value to the right of the decimal place.
And then True and False are literals in the Python language.
And they have specific meanings that we'll get to a ...
little later, but True and False are also ...
literals in Python. (Notice these have no quotation marks.)
All literals are themselves expressions.
They evaluate to themselves.
We can construct compound expressions by using ...
arithmetic operators. There more operators than ...
I'm showing here, but this will suffice for our ...
purposes right now. So 3 + 5 is a perfectly valid expression.
7 - 2 is a valid expression.
3.5 * 4 is a valid expression.
21 / 3 is a valid expression.
Notice that each one of these expressions ...
if we were to evaluate it, it has some value.
So 3 + 5 evaluates to 8. 7 - 2 evaluates to 5.
3.5 * 4 evaluates to 14.0.
21 / 3 evaluates to 7.0.
So just like we can in mathematics,
we can use parentheses to group certain operations.
So here we're using parentheses:
(3 + 5) * 4,
7 - (2 * 2).
1 + (3.5 * 4), and so on.
Notice that the parentheses have to be balanced.
So there has to be an open and closing parenthesis that are matched.
We can have nested parentheses,
but for every open parenthesis we need a closing ...
parenthesis to match.
And we can use redundant parentheses---they're ...
syntactically valid but generally they are considered ...
poor style in most contexts. So I would avoid redundant ...
parentheses except in places where it makes reading ...
more clear.
An interesting thing about Python:
we can construct compound expressions with strings using ...
the plus and multiplication symbols.
Now, they mean something different ...
when they're used with strings than when they're used with numbers.
When they're used with numbers they perform ...
multiplication and addition just like you'd expect.
In the case of strings, however, ...
they perform what's called "concatenation."
Here we have the concatenation of three separate strings:
'Hello'
and an empty space ' '
and then 'World'.
And concatenation is just like coupling railroad cars:
One butts up against the next and they form a longer chain.
So, 'Hello' + ' '
+ 'World'
becomes 'Hello World' when we evaluate it.
Now, you might wonder what is ...
'Fun' times three.
Doesn't mean three times the fun (although maybe you can ...
read it that way).
It means that we're going to take that string 'Fun'
and concatenate it three times together. So the result of the ...
evaluation of 'Fun' times 3 would be 'FunFunFun'.
It's important to note that we cannot ...
concatenate anything but strings.
So for example if we were to type 3 + 'Hello' ...
that would not be a valid expression,
and that would result in an error.
Also we can't use the minus sign or ...
the division operator
with strings.
So in summary: Literals are expressions.
We can construct compound expressions with numeric ...
literals, arithmetic operators and parentheses.
With some restrictions, we can construct expressions ...
which concatenate strings---that's using the ...
multiplication and addition operators.
And later on we'll see that functions can be used in ...
expressions, but let's save that thought for now.
Now a little comprehension check.
Which of the following are valid Python expressions?
Let's look at the first one. The first one is a constant---a ...
literal. It's an integer literal and that's ...
perfectly fine.
(We'll give that one a little green check mark there.)
What about the next one? Is this a valid Python expression?
We're just rewriting the number above---1729---
as 1000 + 700 + 20 + 9. And of course,
that would evaluate to 1729, and that looks okay.
That's a valid Python expression.
What about the next one?
There's a problem here! What's the problem here?
Well, we have an unbalanced ...
parenthesis. We have an opening ...
parenthesis ...
before 20, but we have no matching ...
closing parenthesis. So this is *not*
a valid python expression.
The next one is just a string literal:
'Spam'. That's fine.
And we've already seen we can perform multiplication with ...
strings---not really multiplication,
it's repeated concatenation---so 'Spam'
* 3 would be 'SpamSpamSpam' ...
and that's a perfectly valid ...
expression.
Now we can't use subtraction with strings.
There are approaches that we can use to manipulate strings,
but this is not a valid one, so this is not a valid Python expression
We can't say 'Spam' - 'S'.
That's no good.
Now what about this one?
'Spam' * 2 ...
+ 'Spam'.
The order of operations for string concatenation works ...
the same way as the order of operations for multiplication ...
and addition of numbers, so we multiply first and then we add.
So this would take 'Spam' concatenated twice to get ...
'SpamSpam' and then we'd add another ...
'Spam' at the end to get ...
'SpamSpamSpam'.
So that is a valid Python expression.