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.