0:13

Today's lesson is fairly straight forward,

Â you've probably seen some of this in middle school, in high school algebra.

Â I'm going to tell you about how numbers are represented in Python, and

Â then we'll discuss how to do arithmetic expressions by combining these numbers.

Â I don't think it's anything that's too mysterious here.

Â Kind of what you should think about today's lecture is that Python is course,

Â just an over ground calculator.

Â We're going to give it some data, in this case, numbers, and

Â we're going to ask it to do some operations.

Â And it's going to, when it's finished, give us an answer.

Â And so that will kind of consume us through today's lecture.

Â Then I'll go on and we'll talk about ways to build variables and

Â functions of things that we'll act on later on.

Â Okay, let's go to class.

Â 1:09

So here, I have a CodeSkulptor window sitting, and

Â it's got the example we're going to consider here.

Â This is actually the completed example, I'm going to walk through it and

Â fill in all the Python expressions dynamically, so you can see how I do it.

Â But the critical thing is, we're going to make that file available to you.

Â It's going to, in the break between this segment of video and

Â the next, we're going to pop up a URL that you can click on, and

Â it will take you to this file inside CodeSkulptor.

Â And you can kind of see up here, there's actually a naming convention we have.

Â It's always codeskulptor.org, and we put a hash mark with examples.

Â And then afterwards, we'll have the lecture name.py.

Â Now you don't have to remember that, but then go up here,

Â you can click on it, pull it up.

Â And you can play around with it either during the video or

Â after the video is over.

Â 1:58

So to begin, let's just dive right in and have Python pronounce some numbers.

Â So let's print 3, and -1, and

Â 3.14159, maybe -2.8.

Â So if I hit run here, sure enough, out come those numbers.

Â Now the thing to notice that in Python, there are actually two types of numbers,

Â they're kind of signed whole numbers, things like 3 and -1.

Â And then there are decimal numbers that are kind of have fractional parts and

Â always have kind of a decimal point here.

Â And in Python, those signed whole numbers are called ints, for integers, and

Â kind of the decimal numbers are called floats for floating point numbers.

Â And if you are ever in any doubt about kind of, what kind of number you're

Â working with, there is a function in Python that can actually tell you kind of

Â what type of number you're working with, it's called type.

Â So I could say type(3) or

Â maybe type(3.14159), if I run that,

Â well, I'm going to guess this is an int, and this is a float.

Â Let's run it, sure enough, it's an int, this is a float.

Â Now here's a little trickier test, what happens if we ask for the type of 3.0?

Â Now if you look at it, really, this is really a whole number, but

Â somehow it's kind of got this 0 fractional part.

Â So in Python, this is going to be a float.

Â In fact, the way we're going to know we're working with a float is we always have

Â a decimal point here, a .0.

Â 3:29

Now there's some functions that we can use to move back and forth between ints and

Â floats, and they're not too complicated.

Â In fact, they're just the function int and float.

Â So I can convert something that is a floating point

Â number into an integer in the following manner.

Â 3:51

And so, these are going to return back some integers and

Â kind of what integer are we always going to get?

Â Well, the rule is,

Â we're always going to get the whole part of this decimal number here.

Â So we're just going to throw away the part to the right of the decimal point.

Â 4:11

That, again, seems kind of crazy because,

Â well, an integer kind of is a decimal number.

Â But what you're going to see is the way Python represents this is, well,

Â it's going to simply put .0 here.

Â Okay, so when you see 3.0, that's really telling you this is a float,

Â -1.0, that's a float.

Â 4:43

Floating point numbers are only an approximation to a decimal number.

Â For example, when you do 1 divided by 3, you expect a decimal representation of

Â something like 0.33333333, where the 3 repeats forever.

Â 5:03

In fact,

Â a number like pi doesn't even have kind of even a nice repeating representation.

Â There's a famous episode of Star Trek where Mr. Spock calms a rebellious

Â computer by asking the computer to compute the value of pi to the last decimal digit.

Â And the computer whirs off and goes into an infinite loop,

Â desperately trying to compute the last digit of pi.

Â People have built a computer taking this class, they have a time out here or there.

Â But the critical question is kind of what goes on inside Python,

Â whenever you give it a number that has lot of digits.

Â What does Python do?

Â So let me show you kind of two examples real quick here to help you understand.

Â So what I've gone out to have done is I've actually grabbed two really good

Â approximations for pi and the square root of 2, so here I have,

Â this is kind of a 50 digit approximation of pi.

Â And then I've also gone and

Â I've grabbed a 50 digit approximation to the square root of 2.

Â 6:09

So I'm going to actually ask Python now to print these out, and when you do that,

Â notice that I lost a lot of digits here.

Â All these digits here kind of got trimmed down.

Â All these digits here got trimmed down.

Â So what happens inside Python is they represent floating point numbers of about

Â 15 decimal digits of accuracy.

Â So anything beyond that gets thrown away.

Â 6:31

So in particular, occasionally, when you're doing arithmetic operations using

Â floating point numbers, you're going to get answers of the form,

Â 4 point and a bunch of 0s and then maybe a 3 at the end.

Â What you're seeing here is something called floating point error.

Â Whereas Python is doing the computation,

Â it can't do the exact precise operation that you're specifying.

Â It can't compute pi to the last decimal point or

Â one-third to the last decimal point.

Â So it has to do some approximation.

Â So you're seeing that approximation error there inside that computation.

Â 7:09

All right, we talked about numbers in Python.

Â Let's talk about the arithmetic operators that you have available to do

Â computations in Python.

Â Here's a list of kind of some of the bare basic arithmetic operators.

Â We have plus, minus, times, division, power.

Â It's fairly straightforward.

Â We just take two numbers and apply the operator to them,

Â so we can say something like 1+2, or

Â 3-4, or 5*6, or 2 to the 5th power.

Â 7:46

And if we run that, out comes 3-1,

Â 30, and to the 5th is 32.

Â One operator that you should pay attention to in Python is division.

Â So, the way the division operator works in Python 2 is different

Â than the way it works in Python 3.

Â In Python 2, if one of the operators is a floating point number, then the result of

Â division is also a floating point number, and it approximates the actual division.

Â So, for example,

Â I could say print 1.0/3,

Â or 5.0/2.0, or

Â -7/3.0.

Â And what would come out when I run that is what I'd expect,

Â kind of a decimal approximation of 1/3, 5/2, and kind of -7/3.

Â 8:46

Notice if both the operators are actually integers, well,

Â Python returns kind of the integer version of the answer.

Â And that particular case is kind of the next lowest integer after you do

Â the exact division.

Â So for example, if I say print 1/3,

Â 5/2, -7/3,

Â we run that, the answer comes out to be,

Â well, one integer divided by 3 is 0.

Â We kind of round it down.

Â 2.5, well, let's see, 5/2, integer answer would be 2 here.

Â We kind of rounded 2.5 down.

Â We do -7/3, well, integer division is actually -1/3.

Â I'm actually talk about division more later on when we talk about remainders,

Â and we'll have a second operator called //, is explicitly integer division.

Â 9:56

So, the idea is fairly simple.

Â An arithmetic expression is either a number, or

Â it's an arithmetic operator applied to two arithmetic expressions.

Â Just kind of our first example of kind of recursive definition.

Â So, for example, 2 is an arithmetic expression, 2+3,

Â it's an arithmetic expression because we've applied the plus

Â operator to two arithmetic expressions, 2 and 3.

Â 10:24

Now in practice, you don't even really need to understand that definition.

Â You can just simply go through and

Â type in expressions that you're used to using from, say, middle school algebra.

Â So I could say 1 + 2 * 3,

Â or 4.0- 5.0 / 6.0,

Â or 7 * 8 + 9 * 10.

Â And if I hit run, what comes out is exactly what I'd expect.

Â Now you might kind of say, well,

Â how did I go from this definition kind of involving an arithmetic expression.

Â It's an arithmetic expression combined with operators,

Â to this thing that I just typed in that's just kind of this flat expression mean.

Â I typed in 1 + 2 * 3, so somehow,

Â Python knew that this was really 1 +, then 2 * 3.

Â It turns out there's this notion of precedence, and

Â you probably studied this in middle school algebra.

Â And in fact, you probably have a mnemonic you learned at one point, maybe remember,

Â which was, please excuse my dear aunt Sally.

Â So, the first letter of that phrase, of each word in that phrase,

Â gives us the order in which we should do operations,

Â when we're taking kind of this linear version of an expression trying to think

Â about it in terms of this kind of recursive definition.

Â So it says that when we're evaluating expressions,

Â we should alway do parentheses first.

Â And then after that, we should always do exponentiation next.

Â 11:52

Then we should go through and do multiplication and division, m and d.

Â And in fact, those have equal precedence.

Â So if we have a sequence of those, we just do them from left to right.

Â And then finally, we do addition and subtraction last.

Â 12:04

So for example, let's type in an expression here,

Â say, print 1 * 2, 1 * 2 + 3 * 4.

Â And if you look at that,

Â the rules of precedence say that we should do multiplication before addition.

Â So this is really the same thing as 2 + 12.

Â So if I run that, sure enough, I get 14 back from both of them.

Â And notice, here's kind of the real rule,

Â which is if you're in doubt about the order in which operations take place,

Â you can always just go through and use parentheses.

Â So for example,

Â if I wanted to do that 2 + 3 first,

Â I can say 1 + 1 * (2 + 3) * 4.

Â And that should be the same thing as 1 * 5 * 4.

Â And sure enough, these both evaluate to 20.

Â 13:08

So in practice,

Â working with arithmetic expressions inside Python is very intuitive.

Â It's really what you learned in middle school algebra.

Â So I don't think you'll find anything real too tricky here.

Â So go ahead and take a shot at it.

Â