[MUSIC].
Well welcome to class today we're going to start you on your journey of
learning how to program the Python. Today's lesson is fairly straight
forward, you've probably seen some of this in middle school and high school
algebra. I'm going to tell you how numbers are
represented in Python and we will discuss how to do arithmetic expressons by
combining these numbers. I don't think its anything that's too
mysterious here. Kind of what you should, what you should
think about today's lecture is Python is, is an over grown 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 its going to, when its finished, give
us an answer. So that will kind of consume most of
today's lecture. And I'll go on about ways to build
variables and functions and things like that later on.
Okay let's go to class. Before we jump into the details of our
lecture on Arithmetic Expressions. Let me show you about how we're going to
handle examples that we're going to consider during lecture.
So here I have a code sculptor window setting, and it's got the example we're
going to consider here. This is actually a completed example.
I'm going to walk through it and fill in all the Python expressions dynamically so
you can see how I, 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 the video and the next, we're going to popup a URL that you can click
on. And it will take you to this file inside
code sculptor, and you can kind of see up here there's actually a naming convention
we have. It's always codesculptor.org.
We put a hash mark with examples, and then afterwards we'll have a lecture
called .py. And you don't have to remember that, the
link will be here, you can click on it to pull it up.
And you can play around with it either during the video or after the video is
over. All right, let's talk about numbers and
their arithmetic expressions. So to begin, let's just dive right in and
have Python print out some numbers. So let's print 3 and -1 and 3.14159 and
maybe -2.8. So if I hit Run here, sure enough out
come those numbers. Now the thing to note is that in Python
there are actually two types of numbers. There's kind of sign whole numbers,
things like 3 and -1. And then there are decimal numbers that
kind of have fractional parts and always have a decimal point.
And in Python those sign home numbers are called ints or integers and kind of the
decimal numbers are called floats or floating point numbers.
And if you are ever in any doubt about kind of what kind of number you are
working with. There is a function in python which can
actually tell you what type of number you are working with is 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 it's
kind of it's really a whole number, but somehow its kind of got this 0 fractional
part. So in Python this is going to be a float
in fact the way we are going to be working with float is we always have a
decimal point here, .0. 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 a function int and
float. So, I could convert something that is a
floating point number into an integer in the following manner.
[SOUND]. 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 going to get a, 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. we can also go through and convert, an
integer into a float. And 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. We're always going to 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. Now before we move on to talk about
arithmetic operations, let me give you a few words of wisdom about dealing with
floating point numnbers. floating point numbers are only an
approximation to a decimal number. For example, when you do 1 divided by 3,
you expect the decimal representation of something like 0.33333333 with 3 peats
forever. Computers are not really graded
representing that kind of information. In fact a number like pi doesn't even
have kind of,1 even a nice repeating representation.
There's a famous episode of Star Trek where Mister Spock calms 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 involute, desperately trying to compute the last digit of pi.
Now, if you'd, the computer people who have built the computer have taken this
class. They've no just 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 lots of digits.
What does Python do? So let my show you, I had two examples
real quick here to help you understand. So what I've gone out to 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 grabbed a 50 digit
approximation to the square root of 2. 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, they represent floating point numbers
with about 15 decimal digits of accuracy. So anything beyond that gets thrown away.
So, in particular, occasionally when you're doing arithmetic operations using
floating-point numbers. You're going to get answers of the form
four point and a bunch of zeroes and then maybe a three at the end.
What you're seeing here is called, something called floating point error.
Whereas Python is doing the computation. It can't do the exact, precise operation
which 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 inside that computation. Alright, we've talked about numbers in
Python lets take about the arithmatic operators you have available to do
computations in Python. here's a list of, kind of, basic
arithmetic operators, we have plus, minus, times, division, power, it's
fairly straight forward. You just take two numbers and apply the
operator to them, so we could say something like, 1 plus 2, or 3 minus 4
are 5 times 6 are 2 to the 5th power. And if we run that, out comes 3 minus 1,
30 and to the 5th is 32. one operator that you should pay
attention to in Python is division. So, the, the way the division operator
works in Python 2 is different in 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 divided by 3 or 5.0 divided by 2.0 or
minus 7 divided by 3.0. And what would come out when I run that
is what I'd expect kind of a decimal approximation of 1 3rd, 5 halves and kind
of minus 7 thirds. notice at 5, both the operators are
actually integers. Well, Python returns kind of the integer
version of the answer. That particular case is the kind of the
next lowest integer after you do the exact division.
So for example if I say print 1 divided by 3 or 5 divided by 2 or -7 divided by
3. That, the answer comes out to be, well, 1
integer divided by 3 is 0, we kind of round it down, 2.5, then let's see, 5
divided by 2. Integer answer would be 2 here, we kind
of rounded 2.5 down. We do -7 divided 3, well, integer
division is actually minus 1 3rd. And we'll actually talk about division
more later on, when we talk about remainders.
And we'll have the second operator called, slash, slash is explicitly
integer division. All right, we know about numbers, we know
about arithmetic operators. Now, we're ready to build arithmetic
expressions. So, the idea is fairly simple.
An arithmetic expression is either a number or it's an arithmetic operator
applied to two arithmetic expressions. This is kind of our first example of kind
of recursive definition. So for example, 2 it's an arithmetic
expression, 2 plus 3, it's an arithmetic expression.
Because we've applied the plus operator to two arithmetic expressions, 2 and 3.
Now in practice, you don't 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 plus 2 times 3 or 4.0
minus 5.0, divided by 6.0, or 7 times 8 plus 9 times 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. Is an arithmetic expression combined with
operators to this thing that I just typed in that's just kind of this flat
expression. I typed in 1 plus 2 times 3.
So somehow Python knew that this is really 1 plus, then 2 times 3.
Turns out there's this notion of precedence.
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 sallie. So the first letter of that phrase, of
each word of that phrase gives us the order.
In which we should do operations when we are taking kind this linear version of an
expression trying to think about in terms this kind of recursive definition.
So it says that we are value an expression, we should always do
parenthesis first and then after that we should always do exponentiation next.
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 em from left to right. And then finally we do addition and
subtraction last. So, for example, let's type in a
expression here say print, 1 times 2, 1 times 2 plus 3 times 4.
And if you look at that, the rules of Preston say we should do multiplication
before addition, so this is really the same thing as 2 plus 12.
So if I run that, sure enough I get 14 back for 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
parenthesis. For example, if I wanted to do that 2
plus 3 first, I can say 1 plus 1 times, 2 plus 3 times 4, that should be the same
thing as 1 times 5, times 4. And sure enough these both evaluate to
20. So in practice working with arithmetic in
expressions inside Python is very intuitive.
It's really what you, what you learn 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.