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.