字幕表 動画を再生する
[MUSIC PLAYING]
DAVID MALAN: All right.
This is CS50, and this is the day before our test, of course.
But this is lecture 8, in which we're actually
going to finally transition from C, this lower-level language
that we've been spending quite some time to.
And the goal today isn't so much to focus on Python per se,
but honestly to do what we hope will be one of the most empowering aspects
of the class, which is to emphasize that this has not been a semester learning
C. This has been a semester learning programming,
a certain type of programming called procedural or imperative programming.
But more on that in another higher-level class perhaps.
But really, that this class is about ultimately teaching
yourself to learn new languages.
And indeed, what you'll find is that as we explore some of the features
and the syntax of Python, odds are today,
it might look as cryptic as C did just a few weeks ago.
But you'll find that once you start recognizing patterns,
as you have with C, it will be all the more accessible and all the more useful
when solving some problems.
So unrelatedly, just earlier this week, I happened to be in Mountain View
with some of the team.
And you might recall from last lecture at Harvard
we offered this glimpse of one of the earliest racks of servers
that Google itself had.
Well, turns out they changed buildings.
But we happened to stumble upon the actual display.
So pictured here is a photo from my own phone,
which was actually really cool to see.
So inside of this, you'll see all of the old hard drives they've used.
We actually looked at some of the labels.
And indeed, hard drives manufactured in 1999,
which is when Google started getting some of its momentum.
You can see the green circuit boards here,
on which would be CPUs and other things, potentially.
So if you'd like a stroll down memory lane,
feel free to read up on this on Wikipedia or even on the excerpts here.
And then strangely enough, at the conference some of us were at
did we discover this-- perhaps the biggest duck debugger made up
of smaller duck debuggers, one of whom was our own.
So that, too, was how we spent this past week.
All right.
So how are we going to spend this week and the weeks to come?
So you'll recall that when we transitioned from Scratch to C,
we drew a couple of comparisons between syntax and features.
And I thought it'd be useful to take that same approach here,
really to emphasize that most of the ideas we're going to explore today
are themselves not new.
It's just how you express them and how you
write the syntax in the language known as Python that's
indeed going to be different from Scratch, from C,
and now here we are with Python.
So back in the day, in week 0, when you wanted to say something in Scratch,
you would literally use this blue purple puzzle piece, say hello.
And we called that a function or a statement.
It was some kind of verb action.
And in C, of course, it looked a little something like this.
Henceforth, starting today in Python, it's going to look like this.
So before, after.
Before, after.
So it's pretty easy to visually diff these two things.
But what are just a couple of the differences
that jump out at you immediately?
C, Python.
So there's no more backslash n, it would seem, in this context.
So that's kind of a nice relief to not have to type anymore.
What else seems to be different?
No semicolon, thank god.
Right?
Perhaps the stupidest source of frustration
that you might have experienced by just omitting one of those.
And someone over here?
Yeah, so printf is now just print, which is pretty reasonable unto itself.
So these are terribly minor differences.
But it's sort of testament to the kinds of mental adjustments
you're going to have to start to make.
Fortunately, thus far we've seen that you can start leaving things off,
which is actually a guiding principle of Python in that one of its goals is it's
meant to be easier to write than some of its predecessors, among them C.
So in C we might have implemented this hello, world program that
actually ran when you clicked the green flag using code like that at the right.
And this was, if those of you who had no programming experience coming
in to CS50, what probably looked like the proverbial Greek to you
just a few weeks ago.
And we teased apart what those various lines meant.
But in Python, guess what?
If you want to write a program whose purpose in life is to say, hello, well,
just write def main.
Print hello, world.
So it's a little similarly structured.
And in fact, it does not lack for some of the more arcane syntax here.
But we'll see soon what this actually means.
But it's a little simpler than the one before.
And let's tease this apart.
So def here simply means define me, a function.
So whereas in C we've historically seen that you specify
the type that the function should return,
we're not going to do that in Python anymore.
Python still has data types, but we're not
going to explicitly mention what data types we're using.
Meanwhile, here is the name of the function.
And main would be a convention, but it's not
built into the language in the same way as it is in C, as we shall see.
Meanwhile, this silly incantation is just
a way of ensuring that the default function
to be executed in a Python program is indeed going to be called main.
But more on that when we actually start creating.
But this is perhaps the most subtle but most important
difference, at least early on.
And it's even hard to see at this scale.
But notice the colons both here and here that I've highlighted now in yellow,
and these dots, which are not to be typed,
but are just meant to draw your attention to the fact
that I hit the space bar four times in those locations.
So if you have ever sort of gotten some feedback from your TA or TF
that your style could be better, closer to 5 out of 5, because of lack
of indentation or pretty formatting, Python's
actually gonna help us out with this.
So Python code will not run if you have not invented things properly.
So gone are the curly braces that encapsulate related lines of code
within some block of functionality.
And instead, they're replaced generally with this general structure.
You have a colon, and then below that and indented
are all of the lines that are somehow related to that earlier line of code.
And the indentation must be consistent.
So even though your own eye might not quite
distinguish four spaces from three, the Python environment will.
And so this will actually help implicitly
enforce better style, perhaps, than might
have been easily done from the get-go.
So then, of course, in Scratch, we had a forever block,
which says, hello, world forever, much like in C, we
could implement it like this.
Now there's actually a pretty clean mapping in Python.
We already know we can get rid of the semicolon.
We already know we can get rid of the curly braces.
We're going to have to add in a colon.
But it turns out we can get rid of a little more, too.
So what more is absent from this translation of hello, world to Python?
This one's more subtle.
So we definitely got rid of the curly braces,
relying now just on indentation.
OK, so there's no parentheses around while.
And so this, too, is actually meant to be a feature of Python.
If you don't logically need parentheses to enforce order of operations,
like in arithmetic or the like, then don't use them
because they're just a distraction.
They're just more to type.
And the code now is just visually cleaner and easier to read.
There's a minor difference, too--
True and False are going to be capitalized in Python.
But that's a fairly incidental detail.
But notice this kind of captures already the spirit of Python.
It's not a huge leap to go from one to the other.
But we've just kind of started to get rid of some
of the clutter and the stuff that never really intellectually added much,
and if anything was annoying to have to remember early on.
So True here is our Boolean.
And now we have a finite number of iterations.
We might want to say hello, world exactly 50 times.
In C, this was a crazy mess if you wanted to do this.
You'd have to initialize a variable with which to count up to,
but not including 50, plus plussing along the way and so forth.
In Python, it's going to be a little cleaner.
And we'll come back to what this means exactly.
But if you kind of read it from left to right, it kind of says what you mean.
Right?
For i in the range of 50.
So i is probably going to be a variable.