Welcome to exploring computing.
Today's video is introduction to Python--
our first Python program.
So in the last video, we saw how we
could use the Python shell as basically a super calculator.
And we played around with a bunch of different formulas,
like, Celsius times 9 divided by 5 plus 32,
or kilometers times 0.621.
But the way we were doing it last time,
we had a bit of a problem because we
need to retype these formulas every time we want to use them.
And so as I suggested the last time,
you can use the Python shell as basically a super calculator,
but that's not really creating a program.
In order to turn these into programs,
we need to store them into a file
and be able to reuse them whenever we want.
So it turns out that IDLE has a built-in file editor
as I suggested last video.
And so if you're running IDLE, see last video
for instructions for how to run IDLE.
So if you go to the File menu, there's
an option there to create a new file.
So if you go ahead and do that, the editor will start up
and you will see what's being shown on the bottom right.
Now, our first attempt here could be just
to retype what we were previously
typing into the Python Shell.
So remember last time, we started out
with just basically adding a bunch of numbers together.
So say, I say 3 plus 2, and go ahead and put that
into our editor here, and when we try and run it
by going to the Run menu in the editor, and saying Run Module,
it's going to ask us if we wanted to save the file.
I'm going to go ahead and save the file as example.py
Python files are stored with the extension PY.
And then if I go ahead and run it,
by again, choosing Run Module, it does not do what I expect.
Here's what's actually going to show up.
So if you look at that line with a restart on there,
you see it is attempting to run my example.py file,
but there's no output of what's going on here.
Well, in our Python programs we need to explicitly tell Python
that we want to output to our Shell Interpreter.
So I can do that with what's called a print function.
And so here it is right here.
I'm going to say, Print, and I have a pair of parentheses
and between the parentheses, I say what I wanted to print.
In this case, I wanted to print 3 plus 2.
So I say, print, parentheses 3 plus 2.
And now if I run it, sure enough, if you look down there,
it's the second time I've run it.
You can see that it is printing a 5,
so this is doing what we want.
Now here, I'm printing the results of carrying out
a mathematical equation.
I can output other things.
Remember the last lecture we talked about the fact
that Python works with integers, it
works for floating-point numbers,
it also works with strings, which
allows to manipulate text.
And so what I've got down here at the bottom print,
quote "Go Stanford" quote.
Remember those quotes, indicate that I have a string
of characters G-O S-T-A-N-F-O-R-D,
rather than I've got some variable named, go,
and a variable named, Stanford.
And so if I go ahead and say, print,
and I tell it to the string, go Stanford,
in fact, that's exactly what it'll do.
So I had print 3 plus 2, and I had print go Stanford,
and if we looked down here at the bottom,
you can see it printed both of those.
OK, so last lecture, we converted Celsius
to Fahrenheit.
So let's create a little program that
does this conversion for us.
Here's my first attempt at doing this.
OK, go ahead create a variable called Celsius,
I store 30 in it, I carry out my little calculation there,
and store the result in the variable Fahrenheit.
And then I've got a fancier--
slightly fancier version of the print statement here.
Quote "temperature in Fahrenheit is", end quote, comma,
and then the name of the variable, Fahrenheit.
And that's actually going to print both of these items out.
It's going to print the string, temperature in Fahrenheit
is, and then it's going to go ahead and print Fahrenheit.
And if we go ahead and go to the Run menu in the editor
and we choose Run Module, sure enough this works.
It's says, temperature in Fahrenheit is 86 degrees.
So that works.
But there's a bit of a problem here.
And that is, my program here, includes setting the Celsius
variable to 30.
So if it's always 30 degrees out, this program works.
But if it is always 30 degrees out,
that's not super useful anyway, because I'll probably
figure out that's 86 degrees Fahrenheit soon enough, if it's
30 degrees every day.
So I want this to change, so that instead of always running
with Celsius equals 30, I run with different temperatures
in Celsius.
And in order to do that, I need to get input from the user.
And so that's our next step.
To get input from the user, I'm going to use this new input
function here.
I'm going to say, input--
I've got a pair of parentheses.
And inside that pair of parentheses,
I'm going to put a string there that is
going to prompt the user for the information we want.
So in our little example here, I'm
telling the user that I want information from them,
but I want them to know that the information I want from them
is their name.
And when I go ahead and execute this input function,
it's going to return a value to me that I
can store into a variable.
So now, I generally want to say, input enter your name.
I would actually do what I've got down here,
in the bottom here.
I would say, user, underscore, name-- remember,
you can use the underscore in variables.
User, underscore, name equals input enter your name.
And so, what this code here would do,
is it would prompt the user.
It would tell the user that we want their name.
They would enter their name, and the results
of whatever they entered would be
stored in the variable username, where I could use it
for other purposes.
OK, so, let's go ahead and take this new input function,
and use it in our Celsius to Fahrenheit program.
So I'm going to go ahead and ask the user
to input the temperature in Celsius.
And so, you'll notice I've got two lines here.
Why do I have two lines?
Well, the thing is that input function, is designed
to return string data types.
So if the user enters in 30, I'm not going to get the number 30.
I'm actually going to get the string consisting
of a 3 followed by a 0, and I can't use strings directly
to enter into mathematical formulas.
So I need to convert the string 3, 0, into the number 3, 0,
and it's like that's what the second line here does.
Float is going to take something inside the parentheses there,
it's what we refer to as a parameter.
It's going to take its parameter,
and convert it to a floating-point number.
So in this case, it will take whatever the user entered--
whatever string the user entered in as the input,
and it will convert it to an actual number,
and it will store that value into the Celsius variable.
And if we want, we can actually get
rid of the intermediate variable here temp.
I can put this on one line, I can say, Celsius equals--
request the user, enter the temperature in Celsius.
So the user will enter the temperature in Celsius,
and that will be returned.
That result that the user has returned,
for example, the string 3, 0, will immediately
be passed into float, and float will convert it
from the string, quote "3, 0, to the number
3, 0," what we can use in our mathematical formula.
And so here's our program.
So I'm going to go ahead and ask the user add
to the temperature in Celsius I'm
going to convert to floating-point number,
I'm going to store that in the variable or storage
location called Celsius, and then
I'm going to retrieve the storage location Celsius.
I'm going to multiply it by 9, divide it by 5
and add 32 to it.
I'm going to store that result in Fahrenheit,
and I'm going to go ahead and print,
temperature in Fahrenheit is, and then
whatever the result in the variable Fahrenheit is.
Now, if you look at the handout, I've
actually taken a further step out of this.
And so you could get rid of the variable here Fahrenheit,
and just put the Celsius times 9 divided by 5 plus 32 right
there into the print statement.
And we could even take it a step further,
and we could put this all in one line.
I could say, print temperature in Fahrenheit
is, and then, float input enter temperature
in Celsius times 9 divided by 5 plus 32.
I haven't done that, and actually one of the reasons
why this version is slightly longer
than the version in the handout, is because the font used
for these videos is much larger, and it didn't really
fit very nicely in one line, in the video.
So now, in general, I do recommend that you break it
down into smaller size steps.
It's easier to read, and it's harder to make mistakes.
If you cram this all into one line,
it is technically possible to write this entire program
in a single line code, but it's going to be hard for people
to figure out what you're doing, and it's
going to be super easy for you to accidentally make a mistake,
and it's going to be a little bit
hard for you to track down your error,
so I don't really recommend that.
So let's go ahead and run this program a bunch of times,
and you can see each of the times
it's asking us, please enter the temperature in Celsius.
So up at the top, I said it was 30 degrees,
and it said, Oh great.
That means the temperature in Fahrenheit is 86.
And I rerun it and said, enter the temperature in Celsius--
I said it's 20 degrees.
Said, OK great, that temperature is 68 in Fahrenheit.
If heaven forbid, the temperature in Celsius
is 0, that means the temperature in Fahrenheit is 32, and so on.
So we've created our first program,
and you can see that once we've written it,
it's super easy to use, and it's much easier
than retyping that form over and over and over again.
And of course, if the formula were much more extensive,
it would save us even more time.
So this is something that computers are really great at.
And it actually turns out, Python
is pretty extensively used across campus
in scientific labs.
And in fact, different departments, like for example,
the Chemistry Department and the Biology Department
have emphasized to the Computer Science Department
that they would really like us to teach Python,
and that's one of the reasons why we switched CS106A over
to Python.
All right, so that is our first Python program.
Next lecture we'll be extending our programming capabilities
to do more interesting things.
But I think that's enough for today,
and this gives you an idea of what programming looks like,
and how programming languages work.
I'll talk to you soon.