Hello, and welcome to Chapter Two. Hope you enjoyed Chapter One.
It was one of the longer lectures. Trying to motivate you a little bit.
And now we're going to kind of go back to the basics, to the, chapter
Chapter One covered sort of the first four to five chapters of the book.
So as always, this this video, these slides are
copyright Creative Common Attribution, as well as the audio.
And so,
now we're going to talk about sort of the really
low-level things that make up the Python language.
Constants. So I'm going to summarize this terminology just so I
can like say the word "constant" and you won't freak out.
A constant is as contrasted with something that changes, a variable.
We talk about variables in the next slide. But for now, constants.
Constants are in things that are sort
of natural and instinctive. Things like numbers.
A hundred and twenty-three.
98.6, or Hello world.
And so in, in, what, what I'm doing here is we're, we're using
a Python interpreter, and that, that's how you can tell, the chevron prompt.
And I'm saying print 123, and then Python responds with 123,
print 98.6, Python responds with 98.6, and print 'Hello world'.
So the constants are the 123, 98.6, and 'Hello world'.
So these are things.
We can use either single quotes or double quotes to make strings.
And so programs kind of work with numbers and work with
strings and we have these non-varying values that we call constants.
So the other side of the picture is a variable.
And the way I like to characterize a variable
is it's a place in the memory of the computer.
We give it a name as a programmer.
We pick the variable name.
In this, I'm saying x equals 12.2 and y equals 14.
I am choosing the name and I'm choosing what to put in there.
This is a statement called an assignment statement, and the way to
think of the assignment statement is that it sort of has a direction.
We're saying, dear Python, go find some memory.
I will use label x later to, to refer to that
memory, and take the number 12.2 and stick it into x.
Then this is sequential code.
Then the next thing I want you to do is I'd like you to go find some
more memory, call it y, I will call it y later, and stick 14 in there, okay?
And so that ends up sort of with two little areas
of memory.
You know, the one labeled x, and here's a
little cell in which we, like a drawer, or something.
And one labeled y. And we put 12.2. After these
lines run, we have 12.2 in one and 14 in the other.
Then, for example, if there's another line that's down here, so there's this
third line after this has happened, after this has happened, x equals 100.
Remember, this has kind of got an, a direction to it, see?
Oh, remember that x that I had, you know, I would like now to put 100 in that.
So as I'm thinking this through, I think of that as sort of
removing the 12.2 or overwriting the 12.2 and putting 100 in its place.
And so at the end here, x is left with 100 and y is left with 1 4 with 14.
So these variables can kind of have one value in them and
what we can look at them and we can
reuse them and put different values in if we want.
There are some rules for naming your variables.
Again, you get to pick the variable names.
Often we pick variables that make some sense.
We'll talk about that in a second.
In Python variables can start with an underscore.
We tend not to, as normal programmers, use those.
We let libraries use those.
It has to have letters, numbers, and underscores.
And, and start
with start with a letter or an underscore.
Case matters, so spam is good, eggs is
good, spam23 is good because the number is not
the first character, _speed, that's also perfectly fine
because it start with an underscore or a letter.
[COUGH] 23Spam starts with a letter, starts with a number, so that's bad.
This starts with something
other than a letter or an underscore.
And you can't use a dot in the variable name.
It turns out the dot has meaning to Python that would confuse it.
That would confuse it and wouldn't understand [COUGH] what we
really mean there, and so that would be a syntax error.
That would be a syntax error.
Because case is sensitive, that means that things like all lowercase
spam is different than a upper case S and all uppercase.
These are three distinct variables that are unique.
Most people don't use, choose variables that might be so confusing.
So that's to
you as you write it and as to anybody that
might read it would find three variables named this very confusing.
So it's a bad idea.
Don't do it, but I'm just showing you as
an example that case can make a variable name distinct.
And again, this variable is a place in memory
that we are going to store and retrieve information.
Whether that be numbers or strings or whatever.
These are things that we control.
Now Python also has a set of reserved words.
What it really means is you can't use these for variables.
These words have very special meaning. And, for, is, raise, if.
So you can't make a variable named i-f. It would be like, oh no, that is "if".
I know what "if" is.
So these are words that Python has as its core vocabulary.
And forbids you to use them
for other purposes, like variable names or later function names.
So that's kind of the vocabulary. Constants, variables, and reserved words.
Now, we take these and we start assembling them
into sort of sentences, statements, Python statements that do something.
So we've already talked about an assignment statement.
It has kind of an arrow here.
It says, hey Python, go find me a place called x.
Take the number 2 and stick it in there for later, then continue on.
Now, because there's an arrow, the right side of this is done first.
And so it said, so this right side, you can kind of ignore for the moment the
left-hand side and it calculates the right-hand
side by looking at the current value for x.
Which happens to be 2, and adds these two things together, and then gets 4.
And then, at the point where it knows 4, that this
number is 4, it will then store that back into X.
And so then, later, we print x and we will get the 4. And so again, this is
a sequence of steps and the, the variable x
changes as these steps continue. And when we're saying print x,
that really means print the current value for x.
So, we can do a number of different operators and assignment statements.
We calculate this right-hand side.
This is sort of all calculated, whatever this is, based on
the current value for x does this calculation, and then when
it knows what the answer is, it assigns that into the
variable that's on the left-hand side of the assignment statement.
Again, calculate the right-hand
side completely and then move it to the left-hand side.
Some early languages actually didn't use
the equals sign for the assignment operator.
This assignment operator in, in a way it kind of [INAUDIBLE]
Some languages
An early language actually used an arrow.
Arrows aren't really on people's keyboards.
Another language used colon equals as this assignment operator.
But we use equals.
Now, if you're familiar with math this can be a little confusing, like x equals 1
and then X equals 2. That as mathematics would be bad math
because in a proof or a problem, x can only have one value.
But in programming if this was two statements, that means
just x had a value, and then the value for x changed later.
Okay. So just kind of go through this because it's
working from the right-hand side to the left-hand side on assignment statements.
It is pulling out these x values, so x may have 0.6.
It pulls the values out before, sort of ignoring this part
right here, and it's just going to try to resolve this expression.
And it has multiplication and parentheses and things like that.
So it basically pulls the 0.6 into the calculation,
does the 1 minus x, which gives you 0.4.
Then it multiplies these three things together, giving 0.93.
And then when it is all done with all of that, it takes that.
Oops.
It takes that 0.93, and then puts it back into x.
And so this is just sort of emphasizing how the right-hand side is computed to
produce a value, then it is moved into the variable, and that is why you
can have sort of x on both sides.
Because this is like the old, and this is the new.
This is the old x participates in the calculation, and
then when the calculation is done, it becomes the new x.
Hope that makes sense.
So, this, on the right-hand side here is a numeric expression.
So we have a number of different operators.
Some of them are instinctive, intuitive.
The plus and the minus.
The reason some of these are so weird is in the really old days, we
didn't have too many things on the keyboard,
and a lot of programs were very mathematical.
And so they figured out what was on
the keyboard of the computer equipment of the day.
And then they had to fake certain things.
So, it turns out that plus and minus were on the keyboard,
and so plus and minus are addition and subtraction, respectively.
There was no kind of times
operator for multiplication, and dot was used for decimal points.
So they used asterisk for multiplication. So on computers' languages, nearly
all of them, they basically use a mult times for multiplication.
Slash is used for division.
So we say like, 8/2, which is 8 divided by 2.
Raising something to the power like 4 squared,
that is double asterisk. And then remainder is if you
do a division that gives you the remainder rather than divisor.
So 8 over 2 is 4 remainder 0. So
the remainder is what you get with this particular operator.
There's a few cool things that we can do
with remainder that we won't talk about right away.
But it's there.
And so here's just a couple of sample expressions.
That's giving me green.
Okay.
So, so again, I'm using a Python Interpreter.
So you can kind of, this is just the prompt.
These chevrons are the prompt.
Create the variable xx, and assign it to 2.
Retrieve the old value and an addition.
Then print it out and put it back into xx so xx
has 4. yy, this is a multiplication, 440 times 12.
It is 5,280. yy over 1,000. Now this is a little counter-intuitive
Because yy is an integer, it then does it in a truncated division.
And so, 5,280 divided by 1000 is 5. Now if, and,
and so that's an integer division. We'll see in a second
about floating point division.
Now we take the variable jj and we set it to 23.
And now we're going to use the modular or modulo or remainder operator.
Say what is jj, what is the remainder when divide this jj by 5.
And so if you think about this, we take old long division,
23 divided by 5, you end up with 4 and then remainder 3.
The modulo operator, or the percent of
the remainder operator, gives us back this number.
And so that's why kk is 3.
It is the remainder of 23 when divided by 5,
or the remainder of the division of 5 into 23.
And the raising to the power, 4 cubed. That's not so nice.
4 cubed is 4 star, star 3.
And so that ends up being 64.
So that's just operations. Now, just like in algebra and mathematics
we have rules about when to which, which operations happen first.
In general, things like the power happens before the
multiplication and division, and then the addition and subtraction happen.
And so there are some rules that, when
you're looking at an expression and trying to calculate
what its value is, if you don't have parentheses, it follows these rules.
And so the, the most, the rule that sort of
trumps all the rules is that parentheses are always respected.
So a lot of us just write these with
parentheses in place, even sometimes though you don't need it.
Then after parentheses have been handled, then it does exponentiation.
Then it does multiplication,
division, and remainder. And then it does addition and subtraction.
And then, when all else being equal, it just works left to right.
So let's, let's look through an example. So here is a
calculation that is, you know, 1, 1 plus 2 times 3 divided 4 over 5.
And the question is, what order does this happen, okay?
And so let's sort of take a look at this.
And so, we start with are there any parentheses?
And the answer is no, there are no parentheses.
So let's go next. Power.
And so the, the power says okay, let's look across and find those things that
are raised to a power. And 2 cubed or 2 to the third power is the, the power.
So we're going to do that one. Okay.
And then we can, the way I do it when
I'm sort of doing these slowly is I rewrite it.
So the 2 to the third power becomes 8, so it's 1 plus 8 over 4 times 5.
And then now we can say oh power, that's taken care of.
Now we're going to do multiplication and division and we go across.
Now we have both a division and multiplication.
Okay? Multiplication and division are done at the same
time, so that means we do left to right,
which means we do the first one we encounter first.
And so that will be 8 over 4 because of the left-to-right rule.
And so we find that one, and that's the
one that gets computed next, and that turns into 2.
And again, I like to rewrite these expressions
just to keep my brain really, really clear.
After a while you just do it in your head, but I rewrite them.
When I was first learning it,
at least, I rewrote it all the time.
And and so next looking at this, there's a multiplication.
We're not done with multiplication yet. So the 2 over 5 is the next thing.
And then we do that calculation, and that becomes 10, and again we rewrite it.
And now we've done the multiplication, and we're going to do addition next.
And that's just 1 over 10, and that becomes 11.
And so basically, this big long thing,
through a series of successive steps, becomes 11.
And indeed, when we print it out, that's what we get.
Okay?
So, there's the rules that are parentheses,
power, multiplication, addition, and then, left to right.
But smart people usually just put parentheses in, you know?
So here's this, here's an exam. Oop, go back, go back.
Here's an exam question.
Now, I wouldn't write this code, right, I wouldn't write this code this way.
I would put a parentheses here.
And a parentheses there.
It's the same thing because that's exactly the 2 times 3 is going to happen and
4 over 5 is going to happen and then the
plus and the minus will happen left to right.
But why not make it easier on your readers
and just put the parentheses in. Because they're redundant.
They're not necessary, but away you go.
Now, if you don't want it to happen in that order, of
course then you have to put parentheses if you want the addition
to happen before the multiplication, then you
have to put parentheses in, which you can.
But we tend to recommend that you use more parentheses rather than less parentheses.
Now, Python integer division in Python 2,
which we're using Python 2 for this class.
There's a new Python 3 that the world is slowly transitioning
to and a lot of people are using it in teaching.
But it's not as common, sort of, in the real world with libraries and utilities.
And so we'll stick with Python 2 for a few
more years until Python 3 really kind of turns the corner.
It's nice to have it there, but there's so much Python and it's so
popular, Python 2, that it's just kind of hard to get everybody up to Python 3.
So in Python 2, integer division truncates and you saw that before where
I did the 5280 by 1000 and I got 5 as and, and, but we
can look at a couple of examples that make this really very quite, quite clear.
So, 10 divided by 2 is 5 as you would expect.
9 Divided by 2 is 4.
Not exactly what you'd expect.
You kind of expect that to be 4.5, instead of 4.
But in Python 3, it will be 4.5, but for now, in Python 2,
9 over, 9 over 2 is 4. And 99 over 100 is 0.
Now that seems rather counter-intuitive, but it is a truncating
division, it's not a rounding division, it's a truncating division.
Now, interestingly, if you make either of these numbers have a decimal, make them
what we call floating point numbers, then the division is done in floating point.
So, 10.0 over 2.0
is 5.0. Now, these are different.
This is an integer number, and this is a floating point number.
It's 5.0.
And then 99.0 over 100.0 is exactly as you
would expect, and it's a floating point number, so.
Now you can also mix integers and floating point numbers as you go.
So here we have 99 over 100.
Those are both integers. Integer, integer.
And, or, and that comes out with 0 because it's truncating.
Now if we have an integer and a floating point
number, 99 over 100.0, then that comes out as 0.99.
And either one, if we have 99 over 100, that's a floating point, and
that's an integer.
We still end up with a floating point, so this is a floating point, floating point.
And even in complex expressions, as it evaluates when
it sees an integer, so the first thing when
you evaluate is this would become a 6, so it would be 1 plus 6 over 4.0 minus 5.
Then it would be doing the 6 over 4.0 and that would be 1.5, 1 plus 1.5
minus 5. And so this is an integer and that's
a floating point and the result becomes a floating point.
And then the rest of the calculation is done floating point
to the point where the ultimate is a floating point negative 2.5.
So you can throw a floating point into a calculation and as soon as the
calculation touches the floating point, the remainder
of the calculation is done in floating point.
It kind of converts at the floating point but it doesn't
want to convert it back because it considers floating
point sort of the more general of the representations.