0:06
Now that we know how to express numbers in Python,
we want to actually be able to do something with them.
So let's go back to elementary school and learn how to add and subtract.
All right. Let's get started.
Let's start out with the most basic math you can do in
Python and that is the Unary plus and minus operators.
Okay? Effectively, we can put a plus sign or a minus sign
in front of any number and that tells Python hey,
I want this number to be positive or negative.
Now you'll note that I do not have to use the plus sign; I can,
but if I use no sign in front of a number,
Python assumes that it's positive, right?
So let's try this.
Okay? So we say print plus three.
Python prints out three and just ignores the plus sign there,
it doesn't need to print it for you.
I print out minus 3,348.63 and you can see I get minus 3,348.63.
Okay?
So I can put these plus or minus signs in front of any number - an integer or
a floating point number - and basically it tells
Python if I want that number to be positive or negative, like I said.
All right. Let's look at something more interesting.
Let's start out with some simple arithmetic here.
Addition and subtraction.
Okay. And here we have one plus two.
I'm sure you know the answer to that,
but let's see if Python does.
Let's see what we get here. All right.
We do, in fact, get three,
as we might expect.
Note that I'm adding one integer to another and the result here is an integer.
I can also subtract 48 minus 89 and I get minus 41.
Again, as you would expect.
I can also add and subtract floating point numbers, right?
3.45 plus 2.7 is 6.15, right?
And then subtract floating point numbers; things work out.
A little bit more interestingly here,
I have an integer added to a floating point number.
Now, what would we expect to happen?
Well, you can already see in the output pane,
we're going to get 9.7.
And that's what we think should happen, right?
Three plus 6.7 is obviously 9.7,
but it's a little bit more complicated than that, right?
I've got an integer and a floating point number
and the output here is a floating point number.
So what's actually happening is it takes that
three and it turns it into a floating point number;
turns it into 3.0 and then it adds up to 6.7 and says okay,
the result is 9.7.
Okay? Same thing happens when we subtract here.
We got 9.8 minus four and it takes that four,
turns it into 4.0 and gives us back 5.8.
All right. So hopefully,
addition and subtraction behave pretty much the way that you would expect, okay?
Let's move on to multiplication.
Now, to express multiplication,
we use the asterisk here, okay?
Asterisk tells Python that you mean multiplication.
So three asterisk two here is three times two.
And again, let's make sure that Python knows how to multiply.
Ah, yes, it does. I get a six, right?
And again, I have an integer times an integer and I get back and integer - six.
Okay. I can also multiply floating point numbers,
7.8 times 27.54, right?
And we get a floating point number out there.
And as with addition and subtraction,
I can multiply an integer times a floating point number.
And again, what happens is Python first converts that seven to be
7.0 then it multiplies it by 8.2 to get the result of 57.4.
Okay? So hopefully, multiplication also makes sense - relatively straightforward.
Let's continue our elementary school math lesson here and look at division.
Now, division is a little bit more tricky.
Let's run division.
Okay. I have eight divided by two.
Well, what happened here?
I got 4.0 - that's a little bit different, right?
With addition and subtraction and multiplication,
if I had two integers,
I would get an integer as a result.
So here, I have an integer - eight - divided by the integer - two - and
somehow I got the floating point number 4.0.
Why? Well, the clue is right here on the screen, right?
It's because of what happens here.
I have three divided by two.
Well, there is no integer that I can use here as the - the answer.
Okay? We know that three divided by two is 1.5, right?
Now, Python is doing the right thing here,
it says three divided by two, I get 1.5.
So, division always gives me back a floating point number, right?
Even if it could have been expressed as an integer.
So eight divided by two could have been expressed as the integer four,
but Python still gives me back the floating point number 4.0.
All right? And that way everything is consistent.
So when I divide three by two,
I get 1.5, right?
So I get a floating point number always, right?
And I can divide floating point numbers, right?
So If I divide two floating point numbers,
I also get a floating point number.
Everything makes sense here, right?
Now, sometimes I might actually want an integer as the result of division.
So the single slash in Python is divide, okay?
So if I see this,
that means divide, right?
Double slash here is integer divide.
So it changes the way division actually works.
Okay? So eight slash slash two says divide these numbers and give me back an integer.
So eight slash slash two gives me back four.
Okay? So now I'm back to I've got
one integer divided by another and I get back an integer.
Three slash slash two;
well, hmm - what is that going to give me?
Okay, there's a couple of options here.
You might think hey, that will round;
no, it doesn't, right?
You can see that it gives me one.
It actually throws away the fraction, right?
So three slash slash two - divides that, gets 1.5,
throws away the 0.5 and gives me back one, right?
Now interestingly, what happens when I have
a double slash and I have floating point numbers?
Well, seems like that's integer division,
so it should give me back an integer; ah, not quite.
So what happens is,
if you take two floating point numbers and use double slash,
it divides them and gives you
back the integer value converted to a floating point number.
So 7.538 divided by 14.3 is,
you know, something around a half;
that gets truncated now to zero because we're throwing away the fractional part.
And then we convert it back to a floating point number,
so you have 0.0.
Okay?
One final piece of advanced math I want to show you here is exponentiation.
So I can also raise numbers to powers, right?
And the way to do that in Python is to use the double asterisk here.
So three double asterisks two means three raised to the second power.
Let's check that Python knows how to do that.
Yes, in fact, it does; it gives me nine.
Okay. Five double asterisk 4;
five raised to the fourth power should give me 625 - and indeed, it does.
I can also take floating point numbers and raise them to powers;
so 32.6 to the seventh power is that number over there.
And I can also use fractional powers, right?
Nine raised to the 0.5 power is actually the square root of nine, right?
Nine raised to the one-half power.
And we get three over here.
Right? And again, notice the types.
Right? If both numbers are integers,
you can see that I get back integers;
if either number is a floating point number,
you see that I get back floating point numbers.
And there you have it. We've gone through
some basic arithmetic expressions in Python and you hopefully
now see how you can do some math and actually manipulate your numbers to
get some more complicated results.
We still really can't write anything too interesting,
but at least you can do basic math.
We now have a little bit more knowledge about Python;
we don't just have numbers,
we also have arithmetic.
Yes, we can do some elementary school math here;
we can add, subtract,
multiply, divide and exponentiate.
And the one way we actually confirmed that
Python knows how to do these things too, all right?
Hopefully, you feel like you're moving along a little bit here
and we can write a little bit more complicated things.