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.
So, here we are, talking about integers and floating points.
These are a concept in programming languages and in Python called type.
Variables and constants have a type.
We can see that if you say 1, versus 1.0,
they have different, they, it works different, it functions differently.
And so Python keeps track of both variables and literals/constants, and
having them have a type. And we've seen this, right?
Now, the interesting thing is, is Python is very aware of
the type and can use the same syntax to accomplish different things.
So if we look at this line here, where we say dd equals 1 plus 4.
Well it looks at the 1 and looks at the 4 and it says,
oh those are two integers. I will add those together and give you a 5.
So it gives you an integer, an integer, and an integer comes back,
Okay?
And then ee equals 'hello ' plus 'there'. Well these are two strings,
'hello ' and 'there'. And it says hmm, this must be a concatenation.
Alright? So I'm going to concatenate those together because
those are strings and I know how to concatenate strings.
And that's kind of like string addition, right?
And so we see a "hello there" as a result. Now the interesting thing is, where
did this space come from? Let me change colors here.
Oops.
Where did that space come from? Well, the plus does not add the space.
Here's a space right there, and that's the space.
So I can concatenate it, hello space plus there, and that's how I got hello there.
But, the key thing is, is this plus
operator, clear, this plus operator looks to either side
and says oh,
they're strings. I think you mean concatenation.
Here it looks either side and says oh,
those are integers, I think you mean addition.
So Python is very aware of type and type informs Python what you really mean.
So, it looks like those are kind
of the same, but they're quite different operations.
So the type can get you into trouble. Remember Python is looking at the type.
So here we have a little problem, our
first traceback, first of many tracebacks.
So here we have ee which is hello there which is
exactly what we did. This is a string and this is a string.
So ee should be a string. And then we try to add 1 to it.
And again, Python is saying oh, I see
a plus sign here, so I'm going to look over here, yeah,
that's a string, and look over here, and that's an integer.
And it's like, aaah! And this is a traceback.
Now, here's a good time to talk about tracebacks.
Tracebacks, I color them red.
Because you might think that Python dislikes you or
thinks that you're, you know, unworthy of its brilliance.
And certainly the way these things are worded it sounds like,
you know, the, you're being scolded. It's like, hey, type error.
You can, cannot concatenate str and int objects, right?
That's, I'm, I'm scolding you, you bad, bad programmer.
And it does feel a bit like you're scolded.
But, if you go back to lecture one, this is also
the moment where, really, we shouldn't think of this as like scolding.
We should think of this as Python sort of asking for help.
It's like,
wow, you gave me this line, and I, Python, have no idea.
In all your greatness, could you give me some possible
clue as to what you really mean for me to do?
Because I'm so lost.
And given that I'm Python and I'm lost and you are the only
purpose for my existence, I must stop until you give me better guidance.
So, don't look at tracebacks as scolding.
They sound like scolding. I'll stop coloring them red after a while.
So, if Python is so obsessed with the type of things, you
should be able to ask Python what the type of something is.
So there's a built-in function called type.
This is part of the Python language.
Type (), and you can put a variable in here.
What's the type of the variable ee?
And it says, oh yeah, I know what that is, that would be a string.
And then you can also put a constant in here.
And say what's the type of quote, hello, quote, and that's a string too.
And what's the type of the number 1?
Well that would be an integer.
So it's picky about the type, but it will
also share with you what it believes the type is.
And there's several types of numbers.
As I've already mentioned, there are integers, which are the whole numbers.
They can be positive and negative and zero.
And then there are the decimal numbers,
the floating point numbers, like 98.6 or negative 2.5 or 14.0.
Python knows these as well because it does division different if it's presented with
two integers, or an integer and a float, or a float and a float.
And so here we have x is 1, and we'll say what is it?
It's an integer.
And we say it's 98.6, and we'll say, well, what's that?
It's a float.
And you can ask for both variables and constants.
So what's the type of 1? It's an integer.
And what's type of up 1.0? And it's a float.
You can also convert types.
It has a bunch of type conversion functions built into it.
So, there's implicit conversion going on
when you're sort of saying, you know, divide an integer by a floating point.
It says okay I see, I look to the sides and
I will make the, I will make the conversion for you.
But you can also be explicit.
So in this case we're going to say, take this
99 and convert to a floating point version of itself.
Which is 99.0. And then do the division.
So Python looks out here and goes oh, after that, that's
a float, and that's an integer if I look over here.
And then that means that the result is a float.
And the division is done as a float.
So we are force converting the 99 integer into a 99.0 float.
And we can even do this like and just stick it in the variable.
So we can just put 42 in i and that is an integer.
Then we can say, hey, convert float that i
into a float and stick it into the variable f.
And so we can print it. And now it's 42.0 instead of 42.
Right? They're not the same.
They're both kind of 42, but one is a
floating point 42 and the other is an integer 42.
And we can ask, and that is a float.
And you can also do the same thing in the middle of
a calculation, where you have 1 plus 2 times a float of 3.
This float is done quickly. So the first thing that happens
this is 1 plus 2 times 3.0 over 4 minus 5.
So
the first thing that happens is these floats
are done because they are parentheses so they matter.
So this is a built-in function called float that takes, as its
argument, a non-floating point number and gives you back a floating point number.
We'll talk more about functions in Chapter Four.
You can also convert between strings and numbers, and if you
recall, I, we did the example where we took a string.
In this case, I'm being a little confusing, because
I'm making a string with the characters 1, 2, 3.
Now, this is not the same as 123.
This is a three-character string with 1, 2, 3 in it.
And I can ask what kind of thing is in there, and it says,
oh, there's a string in there. I know about that.
And then I can try to add 1 to it, and
it seems intuitive that quote 123 plus 1 would be somehow 124.
But it's not.
Python takes a look at the plus and says, oh there's
a string on that side, and an integer on that side.
I am going to freak out and tell you
that you cannot concatenate a string and an integer.
Okay? But there is an int function
that converts various things, including strings, to an integer.
So we can give as its parameter, its input, the string value, then it
converts it to an integer, and then we'll put the result in the variable ival.
We can ask what the type of that is, it's an i, it's a integer.
And now we can use it in an expression, print ival plus 1, and
so now Python looks to both sides, sees an integer, sees an integer, and
gets 124. Voila.
Now, if I make a new variable and I stick hello Bob in it, and I
say hey, let's convert hello Bob to an integer, as you might expect, it blows up.
And it says, invalid literal for int.
These, these tracebacks again, once you kind of get used to the kind of harsh
wording of them, because they're not saying, sorry, comma,
they're trying to tell you what's going on.
So, cannot concatenate string and integer, and invalid literal for int.
It's trying to be as helpful as it possibly can
be to give you a clue as to what to fix.
So, again, not scolded.
Okay, so that's variables and types and type conversion.
Now we'll talk a little bit about user input.
And there's a function that's built into Python called raw_input.
And what happens when raw_input runs is it, it has as one of
its parameters, a prompt, which is something that shows up on the screen.
Who are you?
And then,
it waits, tik, tik, tik, tik, tik. Sits and waits, says, what next?
And then, you type a string, dot, dot, dot, dot, dot,
and then you hit the Enter key.
The Enter key.
And then whatever you typed here goes into a variable.
And it is a string. And, then you,
then you can use it.
So I'm going to print the string Welcome,
comma. So that means I'm printing two things now.
The comma adds a space between Welcome and then nam, and so
Welcome is a literal, and then Chuck is coming from this nam variable.
So this is a two-line program.
I think this is one of your assignments, actually,
to well, it's one of the exercises in the book.
To a prompt for a user's name, and
then welcome them, okay?
So raw_input is a function that issues a prompt, waits, and then takes whatever
string that's entered, and then returns it and then puts it into that variable.
So, now we're going to create kind of the first useful program.
It's not a powerful program.
It is a, an interesting problem of the fact that for some reason there
is a difference in the numbering scheme
of United States elevators and European elevators.
European elevators, the floor that you walk out on is the
zero floor.
The floor above that is the one floor and the
floor below that, the basement, is the minus one floor.
And so you walk in and you can go either up the elevator or down the elevator.
Of course, in the United States, the floor that you walk in is the
one and then there's the two floor above that and then there's like, the basement.
So this is the imagination that the Americans
have as to how to number floors, right?
The Europeans go zero, one, minus one.
So, children who go to hotels learn instantly the notion of zero and the
notion of positive and negative numbers and
the symmetry between positive and negative numbers.
I mean, I just wish the United States hotels would switch to this
to teach young people zero immediately and
negative numbers.
So we somehow think that numbers all in the United States start at 1
and then there are no negative numbers, there's the
basement.
I wonder why that is, but whatever.
For people who travel a lot, they may be confused by this.
They need a way to convert back and
forth between the US and European numbering system.
So this is a simple program that demonstrates
a real classic pattern of input processing and output.
It's just three lines, but it has the
essential things that all programs that are useful.
They generally read some data, do some work with
the data, and then produce some kind of results.
And so, so the first line is a raw_input
that effectively, that puts out a prompt and then it waits.
It says, please enter your Europe floor. It sits there.
We type a zero,
then zero goes into inp, but it is a string.
It's not a number.
It's a string.
So we can't add to it. But we can take
and convert it to an integer with the int function.
Int of inp, thats a string being converted to an integer
so now its a real numeric zero.
And we can add 1 to that and we sum that together.
And we put it into the
variable usf and then we print US floor, comma, and then
whatever the variable for usf is. And out comes US floor 1.
So we've written a very simple elevator floor conversion
from a European floor to a United States floor.
Don't ask about negative numbers, it's not really good at that.
But from zero and positive numbers it works great.
So another thing to think about in any programming language is comments.
Comments are like commentary, you know, and basically it's a way for us to
add notations for ourselves and for other humans interspersed in the code.
And so in Python anything after a pound sign is ignored.
You can have a pound sign at the beginning
of a line and then the whole line is ignored.
There are two or three reasons why you could do this.
One is sort of like paragraph headings, where you can
say what's going to happen in English or, or your language.
And you can write documentation says this
code was written by Charles Severence, December 2010.
And you can also just hide a line of code to
test and, and turn it on and off without actually deleting
the line of code. It's a real common thing in debugging.
So for example, here is a, here is a, the program that we've been playing with.
This is our word counting program that
we've been talking about from the beginning.
And here is an example of four comments, one, two, three, four.
Four comments that basically tell us what these paragraphs are going to do.
Now, they don't have any effect on the program whatsoever.
But this one says get the name of the file and open it.
Kind of helpful, right?
Count the word frequency.
That's what this little bit does. Find the most common word.
That's what this little bit does.
And all done, print this out.
So it's really can be very helpful just to add a little tiny bit of stuff.
And you don't want to overuse comments.
You don't want to say like x equals 12, take 12 and put it into x.
Sometimes people teach
you and try to say, oh you need a one comment every three lines.
I don't believe in any of those rules.
I basically say if its useful to describe it, then describe it.
So that's comments. So some operators apply to strings.
We've already talked about plus.
It's kind of silly, although useful in places.
You can actually multiply strings. The asterisk looks and
says I've got a string and an integer, and it prints out the string five times.
Not a lot of use for that.
Now, let's talk a little bit about choosing variable names.
This is something that is really confusing.
So I said like, x equals 1, x equals x plus 1, what does x mean?
And the answer is, it doesn't mean anything.
I chose it. I wanted to make a variable,
and so I picked x.
We pick x a lot, probably because we learned
in algebra in sixth grade that x was a variable.
So, and it's short, and so, why not call it x?
But as your programs get larger this gets kind of frustrating
to have all your variables like x and y and z.
And so the notion of mnemonic, it means memory aid.
We choose our variable names wisely, so they remind us of what the variable's
going to do internally. And so, as I go through this lecture
in the beginning if I choose a variable that's too clever
you're going to think that it's part of the language.
And so I sort of switch back and forth between well-chosen variable names
and stupid variable names to kind of reemphasize the notion that I can choose.
Mnemonic is a good practice, okay?
So here we go. Let's take a look at a bit of code.
So the question is, what is this code doing?
What will it even print out?
Is it syntactically correct?
Now you could probably cut and paste this into your brow, into Python and figure
out that it is syntactically correct. There are three variables.
This one here and this one here match.
This one here and that one there match. And these two match.
So it's taking these two numbers and
multiplying together, and then printing out the product
of the two numbers, if you're real careful and like look at every, every character.
Now, this would be called non-mnemonic variables.
They're really messy.
Now Python, it's happy, because all it wants is to say, oh.
Here is then name that
I, the programmer, decided I wanted to call this
piece of memory and I'll refer to it down here, okay?
And so Python is happy.
Now if you hand this to another human being they are going to be really unhappy.
Because they are going to be like, what are you doing?
So one better way to write it would be to make the variables very simple.
And then cognitively we humans can figure out which is which,
because again it's still only about matching.
The a has to match the a, the b matches the b, and the c's match.
It's actually the exact same program.
A equals 35. B equals 12.5.
C equals A times B.
And print C. It is these.
Python sees these as the same program.
It doesn't care what we name them. Now, a human will
be much appreciative if you say, here you can either have this one or this one.
This one will make them a lot happier.
Okay?
So that is certainly cognitively easier, but it's not really
giving you any sense of what's going on here, right?
So an even better way to write this exact same program to do the exact same thing
would be to choose variables named hours, rate, and pay,
if indeed that is what you're doing.
Now you can look at this and you go, oh well, shoot,
35 is the number of hours, and 12.5 is the rate, and the pay is
the number of hours times the rate, and then we are going to print out the pay.
And that makes a lot of sense.
So this is really a awesome and wonderful characterization.
And if that's what you're doing and those are hours,
rate, and pay, it's a great thing to name the variables.
But, this is where beginning students get confused.
And so sometimes I'll write it this way and sometimes I'll write it this way.
Because you'll look at this, until you get a little
more sophisticated, a little more skilled, and you'll say like
does Python know something about payroll? Is hours a reserved word?
Is rate a reserved word and pay a reserved word?
Are these things that Python knows about? And the answer is, no.
Python sees these three programs as exactly the same name.
It's just this person really made a very bad choice of variable name.
This person made a less bad choice of variable name,
and this person made a really awesome choice of variable name.
So the only difference between those two things is style.
They are the exact same program.
And Python is equivalently happy with these, but humans
are most happy when the variables are easy to remember
and they are somewhat descriptive of what their expected contents will be.
That's mnemonic.
To help you remember what you were meaning to do when you write the program.
This is still a bit cryptic, having these
really short, one-character variable names is still
a bit cryptic. So,
You have a couple of assignments at the end of the chapter.
One of the assignments is to write a program to prompt
the user for hours and rate per hour and compute pay.
So, I won't do this here, but just a couple of sort of odd things.
You're going to be using raw_input.
But remember that hands a string in so you're going
to have to use float.
The function to convert it to a floating
point number so you can actually do a calculation.
And then you're going to have to use multiplication and print.
So then multiplication, and then print.
So some combination of raw input, float, multiplication, and print,
constructed to make your program do exactly this.
So, this is the end of Chapter Two.
We talked about types,
reserved words, variables, the mnemonic, how you choose variable names.
We'll hit this a couple more times
because choosing variable names is always problematic.
Operators, operator precedence, which just means like does multiplication happen
before plus, parentheses. Integer division is a little weird because
it truncates, oops, right, 9 over 10.
Oops, 9 over 10 equals 0. That's the integer division truncating.
Conversion, this is like the int() float().
And then user input, which is raw_input.
And then comments, which are ways for you
to add human-readable text to your program.
Okay? See you next lecture.