So far we've seen simple expressions like 100,
"Hello world" or 3.14.
These are all literal expressions where the value is the same as the expression itself.
But expressions are like building blocks that can be
combined to form larger and more complicated expressions.
Operators are one way to combine expressions.
The first operator that we'll learn is the addition operator.
In order to use it, we write something like print out 100 plus 200.
When I run this code note that I get the value 300.
So, when we execute a print statement then rather than printing out the expression
itself the expression here being 100 plus
200 then we print out the value of that expression.
So, this expression has
a value of 300.
Now, this expression uses the addition operator.
The addition operator which is done using the plus sign expects one expression to
the left and one expression to the right.
What it does is it computes the value of the expression to
the left and the value of the expression to the right.
It adds up these two values,
and then the value of this overall expression
is the value of this expression plus the value of this expression.
So, in the case of 100 plus 200,
we add the value 100 plus the value 200,
and the value of this overall expression ends up being 300.
Remember that whenever we call print,
we print out the value,
not the expression itself,
and so 300 gets printed in our output.
Beyond the addition operator,
python includes many other kinds of operators.
So, there's subtraction.
So, when I print out the value of five minus two then we get three.
If we reset our code here there's also multiplication.
Multiplication is done using the star symbol.
So, when we print out the value of the expression two times four then we get eight.
There's also division.
So, when I print out ten divided by three,
so division is done using the slash symbol.
When I print out the value of this expression then it's ten
divided by three which is 3.3 repeating.
In addition to addition, multiplication, division,
and subtraction, Python include some other kinds of operators.
So, you'll notice here that when we take ten divided by three,
we had two integers ten and three and we got a float as a result.
Sometimes we don't actually want that remainder.
So Python also includes truncated division which leaves out the remainder,
so that we get three back,
and it also includes the modulo operator.
So, if I say ten and then percent sign three,
this is the modulo operator,
so it gives me the remainder of ten divided by three, which is one.
Python also includes exponentiation.
So, if I print four star star two,
then this expression is like saying four to the power of two or four squared.
So, the value of this expression is four squared which is 16.
When executing code, python follows
the normal order of operations that you might be used to in math.
So, at highest precedence is whatever is in parentheses,
and then below that is exponentiation.
So, that would be something like four to the power of two.
Below that there is multiplication, and division.
So, that would be something like two times four,
and then below that there is addition and
subtraction, two plus three.
So, what this means is that suppose that we wanted to compute
the average of two numbers 10 and 20.
So, if we wanted to compute the average of these two,
which should be 15,
then we might intuitively write something like 10 plus 20 divided by two.
Now if I run this code,
you'll note that I get 20.0 rather than 15,
which is the actual average.
The reason is that Python because division is higher in precedence than addition,
when computing the value of this expression then Python first computes
the value of 20 divided by two and gets 10.0.
Then it adds 10 plus 10.0,
and gets the value 20.0.
If instead we wanted to first add 10 and 20,
and then divide by two,
then we could add parenthesis around 10 and 20.
So, I would say print out 10 plus 20 in parentheses divided by two.
Now what Python is going to do is it's first going to add 10 plus 20
to get 30 and divide by two to get 15.0.
You can see that now we're correctly getting the average of 10 and 20. See you next time.
