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.

Â