Hello.
And welcome to Python for Informatics.
Right now we're going to cover Chapter One.
I'm Charles Severance from the University of Michigan.
And I'm the author.
And I'll be your lecturer, for this online lecture of the first chapter of the book.
This lecture and my slides, and the book, as a matter of fact, are all open.
Open content, open materials.
They're copyright Creative Commons Attribution.
And this video recording is also copyright Creative Commons Attribution.
It's important to be explicit about copyright, and
so I say it right at the beginning.
So, computers basically want to be helpful.
They are programmed.
[SOUND] Matter of fact, this is a microprocessor.
This is really just an electrical part.
It's got wires and circuits inside of it.
And somebody spent a lot of engineering time, to make it so that these pins in the
back take instructions from us, from operating systems,
from the hard drive, from the memory.
Instructions come into here and then results come out.
It's really sort of a very programmable hand
calculator and it's our job to put instructions in.
This thing, in a sense, is wired to
be curious about what's next, right? It's like
It, it's, like, tell me what you want to do next.
What do you want to do next?
What do you want to do next, and after that, what do you want to do next,
and it just happens to do that a billion or so times a second.
And, so that's sort of the, the low-level piece, and, but you can also think, if
you have, like, a PDA, something like this,
all the buttons on here are some kind of,
you know, "what's next?"
Right?
Each of those is sort of something begging
for my attention, some application developer who's built a
really cool application and says please use me, please
click me, I am sort of nothing without you.
We humans are the things that sort of cause computers to start doing
something and this will sit here happily until I caused it to do something.
Now, [SOUND] whoa!
Whoa.
Hope it's still okay.
Yeah, it seems to be fine.
Seems to be fine.
Takes a lickin', and keeps on tickin'.
So, these, anyone can use, right?
They say even animals can use a Macintosh, smartphone.
And so you don't have to be a programmer.
But to get this to do what you want
you need to learn a different language, and we need to
learn the language of the instructions to tell it what to do.
So, that's what we're going to do.
We're going to learn how to talk to this.
Yo!
because it's asking us a question, we have to give the answer.
So, what's a programmer?
A programmer is somebody who
writes a program, which is a script or a set of instructions that tell
one of these kinds of things what it is that they're supposed to do.
And sometimes you're running a program like Moodle, an open source
learning management system, or Sakai,
another open source learning management system.
And sometimes you'll even get paid to do that, right?
Sometimes you do it for free, sometimes you
get paid, sometimes you write things for yourself.
And and but, if you think about it, all these
applications on my iPhone, somebody's making some money off of these.
They may not be able to quit their job, but a surprising number
have been able to quit their job or start
small companies, maybe not gigantic companies, but small companies.
So these people that can put applications inside of our computers
are programmers, because they understand the way that we talk to these computers.
And part of what I'm going to try to do is to get you to move from
the mindset of the end user who thinks of this as something just to click on
to the mindset of the programmer, who's kind of
on the inside trying to get out to you.
So that's, as we sort of move from user to
programmer, we move from outside to inside and we think
of the world out there and it's like what are
they going to put, push, what button are they going to push.
So here's kind of a picture of that.
So on the outside, we're users, we click on buttons, we click
on websites, we click on buttons on our phones, et cetera, et cetera.
But what's really going on inside all of that is there's a computer with
a bunch of hardware inside of that, and it has inside of it data,
networks, other information. And software is what makes all that make sense.
And so, part of what I want you to do is I want you to stop
thinking about how to use these things from
the outside, and we move to becoming a programmer.
We're someone on the inside.
We're with the CPU, we are with the memory.
We are with the network connection.
We are doing things on behalf of the user, and presenting them back up to the user.
So, why be a programmer?
Now, this class is specifically not trying to turn you
into a professional programmer, even though I'd be very proud
if after five, ten more classes you were a
professional programmer. But that's not the purpose of this class.
Sometimes you just want to get something done.
You got an Excel spreadsheet at work and the data's not right.
You got the data from somebody else and it's
got like extra spaces where it shouldn't have it or
does missing fields or something, you gotta do something to it that Excel can't do.
And you're, you're stuck, like, saying, Aw, I
want to, I want to mess with this data and
put it in Excel so I can do my job, but it's a pain in the neck.
And I have to sit and bring it into a text editor like Microsoft Word
and go line by line and make all kinds of mistakes and clean the data up.
You can write a program to do that,
and that's the kind of programs we're going to do.
Programs that serve our needs inside the computer, but to serve our needs.
Professional programmers tend to build things for other people to use, right?
They, they tend to build things that everyone else does.
But we're going to build stuff primarily for ourselves.
So.
What is code?
What is software?
We use these words pretty much independently, a program.
It's really a sequence of stored instructions.
We learn the language that this talks, and then
we will feed the instructions in, one at a time.
It takes them one at a time, it gives us back a result, we give it the next one.
It gives it back, in, out, in, out.
So it's really a sequence of stored instructions.
But it's kind of more than that.
It's, it's sort of like our creativity, and if you've been using some of
my software, like my MOOC software, I
spent about a month writing all that stuff.
And it's like, it's me.
I mean I'm, it's my vision of how cool stuff ought to work, right?
And so it's more than just getting something done.
It's also a sense of pride and a sense of accomplishment.
Especially if you're giving something that other people can make use of.
It's really, I think it's very creative.
And it's what attracted me to being a programmer in the first place.
It's that I could, I could leverage the abilities inside of here,
and I could do things, cool things, on behalf of the user.
So, code, software, a program.
So, let's get a non-technical example of this.
So, I'll have you link out to the YouTube [COUGH] for this.
This is the Macarena.
The Macarena is a song that
has with it a well-known dance that everyone
seems to know, or either get taught very quickly.
So I'll, I'll stop and let you watch the Macarena, and then come back.
[BLANK_AUDIO]
So welcome back.
I hope you enjoyed that.
In a sense what we've got there is
a program.
A program for human beings.
And, maybe you learned that at a club or
something, and they told you what to do next.
Well, I can teach you how to do
the Macarena by writing a simple program right now.
So here's my Macarena.
While the music plays, means you do it over and over
and over again, to the beat, that's kind of like computers.
They do things in a beat.
They happen to have three billion beats a second.
But as it were, so we're going to do this multiple times so
we have this whole group of instructions that we're going to do, right?
Left hand out and up, right hand out and up, flip left hand, flip right hand, left
hand to right shoulder, right hand to left shoulder, et cetera, et cetera.
Now this particular little program has a mistake in it.
Actually several.
I want you to look and see if you can find the mistakes in the program.
[BLANK_AUDIO]
Okay.
So here are the places that have the mistake.
Right?
The mistake is
right ham to the back of the head, and left hand to right hit, not hip.
Now if you're in a bar and you take a ham and you hit somebody
in the back of the head, that's not very nice when you're dancing to this song.
These are what's called bugs.
Now a human reading this
would say, oh, I think they meant to say "hand" here.
But a computer is much more literal than people.
We'll see a couple of exercises where we'll see that people
can correct little mistakes like this, but computers they cannot, right?
So we have to fix these bugs and we have to say
right hand and we have to say hip when we mean hip.
So we have to be explicit.
Computers do exactly what we say.
They don't do what we mean to do.
So, let's clear that.
Here is another example, okay?
Let's see how this comes out.
[SOUND]
[MUSIC]
You're supposed to count the number of
times the word "the" appears in this sentence.
Count it.
And the word "the", how many times?
[MUSIC]
Okay, it's your turn.
[BLANK_AUDIO]
Now, here.
[BLANK_AUDIO]
This is not something humans are good at.
I moved it around, I played a little music, I confused you,
I put a picture of a clown car in the upper left-hand corner.
Et cetera, et cetera, et cetera.
Now it turns out that computers, once we tell
them what to do, are very good at concentration.
It can easily go through 30 words and find the most common word.
Or three million words and find the most common word.
And it'll never make a mistake.
We first have to give it a set of instructions.
So, I don't want you to learn this right now, but this is a Python program.
Let's say that I want to teach, let you count words in files, okay?
I say hey, I know how to program Python, I'll
send you an email, and I'll send you this program.
Just stick it into Python, and it will count words for you.
Right?
You've got a million words, million lines in a
file, you want to find the most common word.
And so, so here we go.
So I will send you this file called words.py
I spent a little time, it's a friendly gift to you.
And this is what I type in. Now I'll give
you kind of an outline on what this is going to do.
First thing it's going to do is open a
file and read it, then it's going to split
the lines and files into words based on the spaces.
Then it's going to run through and accumulate
numbers like, you know, this word is one,
this word is one, oh I saw that again so I turn that to two.
That's what this does, it's a loop, it goes
round and round and round, one for each word.
Then what we're going to do is we're
going to write another loop that's going to figure out
which is the most common word by looking
at all those little histograms that we built up.
And then it's going to print those things out at the very end.
And this can certainly do Python words.py and read clown.txt
and tell us that the word "the" occurs seven times.
But, you know, it can go, it can find out that a different thing
has the word "to" and that it occurs sixteen times and it's just as fast.
So yeah, you have to learn a language and you have to tell it what to
do, but once you do it'll do it for a million or a billion words and happily.
So you don't have to do menial work
once you understand the way to instruct the computer to do menial work.
[NOISE]
So, we always start all programming classes with hardware architecture.
I, I don't think it's essential, so don't get too excited about it.
It's a good
use of terminology so we can have some words
like, say, like CPU and you don't freak out.
Or memory, or RAM, or disk drive and you don't freak out.
I don't want to turn you into a hardware nut. I just want you
to kind of have a few words so we can talk about what's going on inside.
Because, in a sense, we're going to be writing
programs to do stuff, both data, instructions, et cetera.
So, here's some hardware that I just bought
a couple weeks ago and I'm really in love with,
and that is the Raspberry Pi.
This is a single-board computer.
It's got storage on an SD card right there.
That's the operating system and the data.
And it's got the both a microprocessor and the memory is in
here, as well, and it hooks up with USB and HDMI and
various things, and if you want, in this course, you probably can
do all of the homework using a Raspberry Pi if you so desire.
So, this is what hardware really looks like.
It's kind of the inside of something.
Normally, it's in some kind of case and you don't get to see it.
And that's what it looks like.
It's kind of got this green and little silver and gold.
I think they're very beautiful.
They're very pretty.
A lot of engineering goes into making these things.
And and so we kind of have a block diagram of
what's going on in here, and there's some, just some terminology.
The, the brains of it all, well.
We draw this black diagram partly because, and this is
a, from a, oh, parts are coming off of this.
Eh.
I don't know what that was.
It's okay, it's broken anyways.
And if you have a desktop computer, this is more like what it looks like inside.
This part is called a motherboard, and it's kind
of like the thing that connects and brings everything together.
It's got a bunch of wires.
Each one of those little lines here is a wire.
It's covered with sort of a lacquer.
And then there are things that penetrate
the board, and then connect to various chips.
And this whole thing is what this picture is.
But it really is connecting a number of different components.
The central processing unit that I've spoken of before.
Put that back down.
[SOUND] Central processing unit is the closest thing a
computer has to a brain, but it's barely a brain.
It's really just a super fast programmable calculator.
It, we make it flexible by our creativity when we write programs.
We make it seem intelligent.
It's people that make it intelligent by taking our own knowledge
and putting it in.
This is not itself intelligent, there's nothing to fear from this.
It's just not that smart.
So, this is the thing that's programmed to ask the question "what's next?"
And then we have to have a set of instructions
that feed this thing really fast, billions of times a second.
And that's what this is, this is the random access memory.
And we have memory chips and
they're connected together through the motherboard.
So, we have the main memory and we have the central processing unit.
And this is where our high-speed instructions come
from, this is where our high-speed data is stored.
And this is the thing that asks "what next?" and it reads its instructions from here.
And you'll see that kind of like, oop, they're not
quite connected together but eventually they're kind of connecting together.
Don't feel too bad about this hardware it's all
old and it's all broken and it can't be hurt.
So, the next thing we got is input/output device.
So I'll go back to my Raspberry Pi.
So the Raspberry Pi has a USB
that you can connect a mouse or a keyboard.
It has a HDMI that you can connect a monitor to.
I has an Ethernet connector.
So these are all examples of
input/output devices.
And then the last thing on the screen is the secondary memory.
So, this RAM [SOUND] on the Raspberry Pi, the CPU, the central processing
unit and the RAM are all on this one chip in the middle.
It's called SOC or system on a chip where they put more parts there.
So in a sense it collapsed
this, and this, and a lot of this all down in the Raspberry Pi
to one little guy.
But it's still architecturally the same thing.
There's a central processing unit.
There's main memory.
There's graphics cards, etc.
[SOUND] So, input/output devices.
Oh, this big
this guy has input/output devices too, like USB and keyboard, monitor, etc.
So they're, they're very similar.
It's just, this is new, and this is old.
Everything gets smaller when it gets newer.
Okay.
[SOUND] Okay.
So the last thing we gotta talk about is the secondary memory.
Well,
when the power goes off, these things sort of go away.
The data in this RAM goes away.
It's just designed to be really fast.
But not permanent.
So we need a place that's permanent, that's what secondary storage is.
That's what, that's what this secondary storage is for, this is permanent.
This is fast, and it cha cha cha cha cha really fast.
And but this is permanent, and this is slower.
Okay?
So the secondary memory.
[SOUND] I've got two kind of secondary memory.
Oh, dropped it on the floor.
[SOUND] Two kinds of secondary memory.
I'll start with the Raspberry Pi.
The secondary memory of the Raspberry Pi is this SD card.
It's like a disk drive.
It still is permanent.
Does not require power to maintain its data.
The data stays permanent.
So, in the future we will see more
of these flash-style drives and SD-style drives.
So the Raspberry Pi is kind of alluding to the future.
[SOUND] There's a disk drive in here.
It's not really a disk.
It's also flash memory.
But in the old days, in the good old days, back when I was a kid,
we, our secondary memory was [SOUND] a disk drive,
and it had [SOUND] platters, and it spun, and it made a satisfying noise.
And it would move in and out to read data, and
I'll show you a video of this just in a bit.
[SOUND] And so these would record the data on the magnetic platters
and then when the power's taken off, the data would still be magnetized.
And then it would go and move to the
right spot, [SOUND] spin it around, and read the data.
And again, this is kind of messed up in a pretty bad way.
So, there we go.
Central processing unit, brains of the operation, main memory, fast,
but goes away when we power on, input output devices, keyboards, et cetera.
And then storage, that has, maintains its data across power cycles.
Okay, and I just said all that.
Okay?
So then the question is, where do you belong in this?
Where do programs live?
Where do we write?
And the answer is, we kind of live in the memory, right.
What we do is we put our programs into the
memory and then the CPU pulls the programs out of the memory.
So we have to write our programs and put them into the memory.
When we start them and run them we're really loading them
into the memory so they can be fed rapidly to the CPU.
Now the computers don't really execute Python, like "if x less
than three print", but that's what we tend to want to write.
Because what the computers really execute is a thing called
machine language, which is a series of zeroes and ones
that pretty much translate directly to what's on these pins.
There's voltages that go up and down.
That's called machine language.
Source code, like Python, is written in a
way that's most convenient, well, at least more convenient.
Machine language is what's most convenient
for the hardware.
So we either, we have to translate from source code to
machine language, and that's what the Python program does for us.
We write in Python, and Python translates to machine language for us.
So, I got a couple of videos that give you a sense of how this all works.
We'll start with CPU.
And what this is going to do is, this is going to show you the intensity of
how much electricity, the thing that gets hot
inside your computer is this little guy right here.
And we're going to see in this video just how hot it can get.
[SOUND].
Okay, so welcome back.
So the next thing I'm going to show you
I showed you a hard disk that sort of didn't work.
But we're actually going to show you a real short video on how a hard
disk works [SOUND] that someone took the
cover off and actually applied power to it.
You don't want to do this yourself if you have a
hard drive, I've read, and some people say that you can do
it for a, for a few minutes and then the drive
kind of destroys itself if you run it with the cover off.
So let's take a look at this.
Okay, so.
Now, we're going to talk a little bit about Python.
Python is a programming language.
Python is a way that we communicate.
Now Python wasn't invented by computers.
We invented Python as humans as a way to encapsulate our instructions.
And there's lots of different programming languages.
Python, Javascript, C++, tons of them.
Python's just one of them that we happen to teach in this class.
Now, I'll start with a little Harry Potter reference.
Parseltongue is the language of serpents and those who converse with them.
An individual who can speak Parseltongue is known as a Parselmouth.
And it's a very uncommon skill, and it may be even hereditary.
Nearly all known Parselmouths are descended from Salazar Slytherin.
There's our Harry Potter reference.
Python is the language of the Python
interpreter, and those who can converse with it.
We're going to converse with the inside of a computer pretty soon.
An individual who can speak Python is known as a Pythonista.
It is an uncommon skill and may be hereditary.
It may not be hereditary too.
Nearly all known Pythonistas use software initially developed by Guido van Rossum.
Guido van Rossum, this guy right here.
Yo Guido, what's up?
Let's put a mustache on, yo Guido.
Sorry.
I should be nice to him.
He is the inventor of Python.
Python's over 20 years old.
He invented it to make it an easy language but was both easy and powerful.
And that's why it's a great language to start your learning with.
It's a powerful language but it's also designed to be easy to use.
Can anyone guess what the reason for the Python language name is?
[BLANK_AUDIO]
So let's see.
[MUSIC]
Python was named after a famous British comedy show that was in the 60s and 70s
and 80s, I think, named Monty Python, Monty Python's Flying Circus.
And so
And I, I think he was trying to capture a
playfulness, a certain kind of silly fun aspect of Python.
And and so there we go, enough of that.
We done?
Yeah, okay, the music's done now, thank heaven for that.
Okay, so again this is a language and this Guido, he made it for us, he made choices.
He said we're going to put a colon here.
I think we should like indent this and do these things.
And he, he's made choices.
And [COUGH] and some languages have people like some different better than others.
It's kind of an artistic choice.
And, and I like to kind of equate this to
learning a language to speak with people, with humans, you know.
You know, when we're a baby we don't know how to talk and
we start babbling, blah, blah, blah, blah, blah, blah. Maybe we just start crying.
That's the first thing that we do.
[SOUND] But we're like, we're like on this strange
planet. We got to talk to this thing.
So we have to learn its language.
And we're not going to learn it right away.
You don't go like, hey study all night and you know this language.
There's no way you can do that.
Although Rosetta Stone might be a good way to
teach programming, maybe I should take some of their ideas.
So, the thing that's different about learning a
human language versus learning a programming language is that
when we're learning a human language we're talking to
a human, and they're going to do correction for us.
So if
if I say a word incorrectly, like," Mama-alah."
Right?
I don't know.
That was pretty dumb.
Someone listening says, oh, I know what he said, I know what he said.
But Python and computers aren't really listening.
They're kind of dumb.
They can't really correct our mistakes for us
because they don't know what we're trying to say, they really don't.
They're very literal.
And, so it is really common
in the beginning to get upset.
Because we say something we think is cute, and it says, syntax error.
And we'd be like, okay let me try this.
And it says, syntax error.
And so we get this notion, and I had this problem when I was
first programming, I was like, I would like, here's my program, do you like it?
It would say, syntax error.
Now, the problem is, they could reword the messages to be a little nicer
perhaps, but the syntax error isn't really a
judgment on you that says you're a failure.
The syntax error is really saying, I, I don't know what you're saying.
I'm confused.
I only know a few things, and what you just said is not something I understand.
So instead of thinking of the program, the Python as some kind of
evil, demonic monster that just hates you and just keeps saying syntax error,
think of Python more like a dog.
Right, the dog's, what can you talk to a dog?
Can you say "lovely sunset we're having" to a dog?
Because the dog's not going to understand that.
The dog does understand some things like food,
back, walk.
But it doesn't understand the accumulated works of Shakespeare.
So when you talk to a dog, you've gotta be careful
to talk the subset of the vocabulary that the dog knows.
And so this is a key thing when you're first learning.
There's only a certain set of things that Python understands.
It turns out it's easier to teach you Python
than to teach Python to listen to whatever you have to say.
Things like Google make it seem intelligent.
So you can kind of type anything into Google, right?
Well, yeah, billions of dollars later
Google, for at least, like, short things, can
seem like it knows what you're talking about.
In terms of programming, it's a lot easier for you to
figure out the exact precise way to say it, rather than make
it so that we have to spend a billion dollars on
something like Python to figure out what you mean in your programs.
So, let's start talking to Python.
We're talking to Python.
So.
If you've installed Python properly, whether it's on a
Mac or a Windows or on a Raspberry Pi, at
some point you'll be in a terminal program and
you'll type Python to make Python run in interactive mode.
You might have to type c:\ something something Python in Windows.
But at some point you're running Python.
Now Python itself is a program, it's a program
that is asking you to type the Python language.
Now the interesting thing is you've got this chevron prompt
here, and it's kind of another version of "what's next?"
I told you that this hardware was designed
to always want the next instruction to come in.
Well.
Python, once we start it, it really has no idea what to do.
It is, is waiting for you to tell it what to do.
Okay?
So, let me see if I can pop something up here.
So here we go.
Clear that.
And now I'm going to type.
Get this a little closer.
I'm going to start Python, so it's the
operating system now is asking me what next,
I'm saying, oh the thing I want to do next is I want to run Python.
So, here we are, we're sitting in
the Python interpreter and it's asking "what next?"
Okay?
Now, I, it's like I just landed on a, on like a
planet.
And it's, like,
take me to your leader.
Take me to your leader.
That's what you always say when you land on a
planet and are confronted by some kind of a robot.
And it says,
Syntax error.
Remember, it's a dog.
It should just say aarf, right?
Could say, take me to your leader, woof, okay?
Are you friendly?
I don't even spell, think I spelled friendly right.
Syntax error.
Are you dumb?
Syntax error.
Pretty dumb.
I hate computers!
[BLANK_AUDIO]
Syntax error.
It doesn't seem to have a sense of humor.
Try this.
Knock knock.
No sense of humor.
So here's the problem.
It wants us, it's
It doesn't hate us.
It, it just wants to know what we want done.
So, we need to know the Python language.
Luckily, I know a bit of Python.
So I'm going to say, hmm, hey Python, I'm going to want some data.
I want to make a variable named "x".
Just a little place in your memory.
Go find it.
Go find one of your spare places in memory.
And I want you to put 100 in that.
Okay?
Do that.
Now, it's happy because I know the language.
Bonjour.
That we know the language.
Now I want to, but it's saying, "what next?"
So we have to put a program in.
So, let's see.
I'm going to say, hey, Python.
I'm going to make a variable called "y", another area in your memory labeled y,
and I want you to go back and remember that x I gave you before?
Go get that one back and add 50 to that.
And put that in y.
So now I've got something in x and I've got something in y, and let's print y out.
What's in y?
Go look in y where you put that and let's print it out.
150.
So.
We're doing simple things, and actually most
programming is a series of simple things.
The number of statements, different statements
you can do is is relatively few.
So we are talking to Python.
Let's run back to the slides, there we go.
And so.
We give it a series of commands and you
can do the same thing sitting on your computer.
And [SOUND] you type exit or quit with
parentheses to get out of it when you're done.
And that ends the interactive session.
Now this is interactive Python where it's asking us command by command and
then interpreting or running those commands as
we run the command, as we finish.
So.
There'll be, you'll be doing it in some kind of a window.
There's a different way to do it on Windows.
My install documentation on pythonlearn.com gives you
all of this, tells you everything to do.
So now we're basically talking to Python.
So, what language?
I gotta still teach you this language.
So, what do we say when we get ahold of Python, what kinds of things?
Just like any language, a human language, there's like vocabulary,
there's basic words, there's variables and reserved words in Python.
Then we kind of combine those in lines to make
sentence-like structures that themselves are not a full story.
And then we kind of make a story out of it.
Now, the story is in the Python language, not sort of English or
French or a human language, but it still is kind of a
a sequence of small pieces that build to make
bigger pieces that then build to make a whole program.
So here is, again, that same program, right? That same program
of how to count the most common word in a file.
And I mentioned before that it starts by opening the file.
It reads the data from the file.
Splits it into words, counts them all up and
then finds the biggest one and then prints it out.
So "name" is like a word, "equals" is another word, "raw" is a
word, all these things are words, each of these things is like a sentence.
There are blocks of stuff that are kind of paragraphs.
There's kind of a paragraph, paragraph.
I for sure, different color here.
Here's like a paragraph, and a paragraph, and a
paragraph, and then at the end of the day, once you
kind of understand it, and you will understand this before
it's all over, this is kind of like a story.
Right?
It holds together, it has a beginning, a middle, and an end.
Again.
Don't worry about the detail, we've got plenty of chapters to cover this detail.
Don't worry about this particular program.
I'm just sort of getting into the sense that, you'll get it.
But we'll start simple.
So, the first thing that you gotta know how to
do in Python is know what not to do.
Or when you use these reserved words, they have very special meaning to Python.
It's like when you say, "I don't think you're going to get any food today" to a
dog, the dog hears the word "food" and nothing
else. So food is a reserved word for dogs.
Walk, bath, there are other reserved words.
So, what it really means is you can't use
these for anything other than what they mean to Python.
So, print tells it to print things.
Return is used in functions.
Else, if, these are words that if Python sees the word if, it's like,
this means something.
Don't use it for any other purpose except its stated purpose.
We'll learn what those are.
Now if we talk about sentences.
Sentences are kind of, in Python, like a line that kind of have pieces to them.
So here is three pieces of code.
One is x equals 2.
That says, take and find me a piece of memory.
In your RAM.
Allocate it, label it x, and stick 2 in it.
This is kind of like a move 2 into x.
Then this says go get x, add 2 to it, and then put the sum back into x.
Again, little sentences that are kind of like subject predicate, right?
Especially this assignment, it's kind of just, and then print.
Print's a reserved word.
Was one of the was on the list in
the previous slide. And then go read that variable.
So, these are like three sentences in our new little language.
Okay, so that's sentences, now paragraphs, let's talk about paragraphs.
Paragraphs are the combination of sentences to make
sort of a thought together. Multiple sentences, multiple lines.
So, the interactive Python that I just showed you
is fine for running one, two, or five or six commands.
But ultimately, we're going to write much longer bits of Python.
And so we write what's called a Python script, or a Python program.
And we put these in a file.
And we, and if you went through the
prerequisite, you will see have seen me edit in a text
editor, saved a file, and then run from the Python file.
Okay?
And so we call these files put .py on the end of them, P-Y on the end of them.
And we're giving Python a script to execute.
[COUGH] So, interactive, you're typing directly into Python
and it's doing it right as you're talking.
You're still doing it in order, and the order does matter.
In a script you type it all into a file once and say Python, do it all.
[SOUND] Now when you write one of
these things there are patterns for combining these.
There are things that we do to these lines that sort of treat them differently.
Is like a recipe, a set of instructions.
Start at the beginning.
[SOUND] But it's a little more complex than that.
Some steps are just sequential.
Some steps might be skipped.
Some steps we do multiple times.
And other times we have kind of like a set of steps we do over and over again.
So here's some pictures.
And here's the four lines of Python, a little simple paragraph.
And it's got a sentence that says, x equals 2.
Print x.
x equals x plus 2, which says, go grab the old value of x, add 2 to it.
Stick it back in x.
And print x.
The output of this program is 2, then 4.
because x was 2, we printed it, and we added 2
to it, and then we printed it again so it was 4.
Now, these flowcharts, don't worry, I'm not going to make you draw these.
I just draw these in case, cognitively, it makes
it easier for you to understand what's going on.
So, x = 1.
is the first step.
Sequentially, it just continues on.
It runs the print.
x equals x plus 1.
Runs the print.
So this is just straight through.
It'll make more sense when we see a little more convoluted things.
So, this program just starts naturally.
Python starts at the beginning and works its way down through the end.
That's sequential stuff.
That's the normal order of business.
Now, a conditional is a step that may or may not get executed.
If all we did was sequential steps, programs would be kind of dull.
Right, they would just be like blah, blah, blah, blah, stop.
So, there's things like oh what if you do this or
what if you do that and so we do things like if.
If you have more than 40 hours, I'm going to pay
you a different rate than if I have under 40 hours.
Those kinds of things are if, the word if.
So in Python, the way we express this is we use the keyword if.
So we say x equals 5, and then we say if x is less than 10.
This is a question that's being asked, is x less than 10 or not, yes or no?
If it is, we execute the indented bit.
If it's not, we skip it.
This case, since x is 5, we execute it.
Then we come back here, and we're going to do another one.
If x is greater than 20, well, this turns out to be false.
So we skip that.
So Bigger does not run.
That line never runs.
So, we, the output is Smaller, Finis.
Now, here, we can take a look at it, sort of, in the picture diagram.
We run x equals 5.
We ask a question, this doesn't hurt x to ask the question, is x less than 10?
The answer is yes so we kind of drive down this little path.
We print Smaller and then we rejoin the freeway.
Is x less than 20? No, so we skip and we continue.
So, this never gets executed.
So you can think of it either way.
You can think of either sort of like
Gestalt, say, if this true, do what's indented.
Or you can imagine sort of a little car
driving down a highway making turn choices as it goes.
They're equivalent. Over time it's probably you'll just
start seeing this and start thinking this way.
But sometimes it helps to think about it this way, for a little while.
Okay.
Now, the next thing I want to show you is
repeated steps, steps that happen over and over and over again.
Okay?
And that again, when I said oh, computers are good at handling a billion words.
Well that means it has to have a loop or a repeated section, one for each word.
It's going to do something for each word.
And so,
so here we go.
And in Python, let's pick a different festive color.
Let's pick purple as a festive color.
So here's our program, starts at the beginning, sets the variable n to 5.
And then a key word, reserved word while, while
n greater than 0, again this is asking a question.
This is asking a question.
Is n greater than 0?
That's a question.
If yes we're going to do this, if no we're going to do that.
Over here if it's true we're going to execute the
indented part, and then come back and do it again.
If it's false we're going to skip down.
So it's kind of like an if, except it
keeps doing it over and over and over again.
So, it comes in, sets n to 5.
Is n greater than 0?
Yeah, sure.
So we print n, out comes 5.
Then it says, and n equals n minus 1, so n becomes 4.
We can change colors.
Then it goes back up.
Goes back up and asks the question again.
n is 4.
It's still greater than 0, so it comes through.
Prints out 4, subtracts 1.
So n is now 3.
Goes back up.
Is n 0?
Is n greater than 0?
Yes, it is.
Print out 3.
Subtract 1.
Now it's 2.
So out come 3 and 2.
Then it goes back up, still greater than 0.
Yes, it is.
Print out 2.
Or, wait.
Now it's 1.
[COUGH] Now we subtract 1, it becomes 0.
Is it greater than 0?
No, and we finally leave.
And we finally drop down.
And so the last thing that comes out is the printed Blastoff.
So this is a loop, the notion that we're
going to run this little bit of code five times.
[COUGH]
Excuse me.
We're going to run this little bit of code five times,
And loops have these things we call iteration variables.
And that is this n.
It's a variable that specifically is changing
each time it goes through the loop.
And that way, we can sort of control the loop.
We can decide when it starts and when it stops.
We can tell if we're at the beginning or at
the end, or the first one or the last one.
We'll do a lot of stuff with loops.
This is an iteration variable, because
we iterate repeatedly iterate through the loop.
Okay?
Any questions?
We can't do questions.
Okay, so now, if we go back to the little
story that I, you've got several chapters to understand, don't worry.
You actually got like through chapter nine, so
don't try to understand this program right now.
I'm just trying to give you a sense of
what the picture is going to be, right?
So, so here are some sequential statements, because they aren't indented.
Those five lines are sequential.
They just go one after the other.
Then we have for, and it's indented.
This is a loop.
This is going to run a bunch of times.
Then we're done with that.
We do some more sequential stuff.
Now, we have a for loop, and that's going to run a bunch of times.
And then we have an if, which may or may not run.
So these little, this little block of
code is conditionally executed based on something.
And here is the question that we're asking.
So that's the question.
And then at the end, we do a print.
Now, again, don't try to make too much sense of this.
I'm just trying to show you sequential, repeated, repeated, conditional.
Okay?
Just, those concepts show up in every, pretty much every program that we build.
Okay.
So, [COUGH] let's do a couple more little exercises
that get you sort of in the mind set
of being a programmer, and how programmers tend to
have to think about problems a little bit differently.
So, here we go.
This I call this An Animated Short Story.
And your job, I'm going to just give you a differ, several sets of numbers.
And I want you to find the largest number in a list of numbers.
Now, it's not so important to know what the large
number is, but also to think about how your mind
attacks the problem.
What your eyes are doing.
What your mind is doing.
How you break a bigger problem down into smaller problems.
How a human solves this problem.
And then we'll focus on how a computer
might have to look at the problem differently, okay?
So don't just, like, get the answer.
That's not so important.
Think about how you get the answer.
So, don't just like scroll ahead in your YouTube and cheat and go get the answer.
Think about actually solving the problem, and then
monitor what your brain is thinking as it goes.
So, here we go.
So, I'm going to give you a list of numbers,
and you are to tell me what the largest number is.
Ready?
Set, go.
[BLANK_AUDIO]
[SOUND] I didn't make it easy, you're looking for the largest number.
[BLANK_AUDIO]
Did you get it?
Did you have to go back a couple of times?
Actually I don't care what the answer is.
The question is
How was your brain solving?
Okay, you probably want to know what it is.
The answer is 198, that was the largest number.
Of course what I was doing is I was moving it to make it difficult.
But here's the thing, how do humans look at this?
Like, do humans, like, did you look at 25, then you looked at 1, then you looked
at 114, and then did you just look at them slowly one at a time, like this?
Or no, I doubt it.
If you are, maybe you're a computer.
Maybe I'm talking to computers, maybe you're all computers.
I'm certainly not a computer, maybe you're all computers.
Okay, enough of that.
No, that's probably not how you did it.
What you probably did was you had your
eyes move around the whole thing very rapidly.
And the first thing that you figured out is that there
were one-digit blobs, there were small, medium, and large blobs of purple.
And the first thing you knew, right away, was there
was no point at looking at any of the small blobs.
Your brain just threw the blobs away really quick.
Then you say, okay, given that it,
there's no four-digit numbers, they're three-digit numbers.
Then what you probably did is you started looking for the first digit.
You say, look, there's some ones.
Is there any twos?
Quickly, you decided there are no twos.
So you knew that you had to look for the big blobs.
And the second digit was probably the thing that mattered.
Then you start getting to the nine, you say, okay, there's some nines.
So that means it's, it's one, nine, something.
Then that was the part that you probably had to go
check to find the, oh, where the heck was the 190, aah!
Oh, 198 right there, [LAUGH] yeah I color
coded it, and I couldn't even see it, okay?
But the point is, is, humans are great
at eliminating sort of bad solutions, really fast.
And you probably looked at how big, how much purple was on
the screen, eliminating the areas that were less purple, because you knew that
your brain quickly and instinctively knew that
the more purple meant a larger number.
Computers don't do any of that, they don't do any of that.
So, in order to make you feel a little more like a computer, I have another test.
And again, the goal is not just to find the largest number.
But to monitor as you go, what your brain is thinking, while you're doing this, okay?
Do you get it?
How are you attacking the problem, what is
your feeling as you're attacking the problem?
Are you a computer or not?
Here we go.
I'm only going to give you a few seconds.
[BLANK_AUDIO]
[SOUND]
So, what did
you get?
My guess is that most of you just said, I don't care.
This is such a hard problem.
It's a stupid problem.
Or I'll try and turn my head upside down, or something.
It's a really hard problem.
The other one was kind of easy.
Not that you, you might not have got it, but you
had this natural instinct that allowed you to approach the problem.
Okay, I'll show you what the right answer is.
The right answer is.
Right there is 197.
Yay.
Right.
I, you can't even, even if I tell you to
you know there you are you know, what is this?
Is this 500 or two zero zero.
Actually, the only way I can do this is I flip it to find it.
I mean it's just not what humans are good at.
This is a little bit more like how computers see the world.
But the fact that the data's frontwards or
backwards should sort of make no difference, right?
Computers need a strategy, we need to give them a strategy.
Okay.
So here we go.
[COUGH] One last experiment.
Now I'm going to show you numbers one at a time.
And at the end, I want you to tell me what the largest number that you saw was.
Ready?
Here we go.
First number.
What was the largest number?
As a matter of fact,
how did you solve that problem?
You solved that problem most likely because you didn't, you couldn't look
at all the numbers at the same time, so you probably created
a variable in your head without even knowing.
And you put into that variable.
You call the variable, the largest number I've seen so far.
And you haven't seen any, so let's say the
largest number you've seen so far is negative 1.
Then I show you 3 and you said to yourself.
Well.
Negative 1 is no longer the largest number I've seen.
So I'm going to keep that one, I'll keep 3.
That's the largest I've seen so far.
And now I see 41, 41 is larger than 3 so I will keep that.
And now I see 12, now 12 is crap because it's nowhere near as good as 41.
So I'm keeping 41.
74. Oh, 9.
9, not nearly as good as 41, so I'm gonna throw that one away.
74, better, better.
Keep it.
Keep that one.
So I'll keep 74.
And the last number is 15.
Don't even know it's the last number, but, we don't want to keep that one.
And so now, we're done.
So we know that at the end, what was during the loop the largest so far is
the actual largest of all the numbers, and
we don't remember exactly how many numbers there were.
So that's kind of like thinking like a
program, you know, this kind of sliding window.
Didn't matter if I gave you a billion numbers or five numbers.
I think there were five numbers, actually.
This notion of the largest so far is a powerful notion.
As a matter of fact, it's central to the program I've been showing you.
And I don't want you to try to understand this, but this part in the purple.
This part in the purple is really saying,
I'm going to loop through the counts of all the words.
So it's got a word like "the" is 15 times, and "clown" is four times.
And it's going to look through all the pairs of word-value combinations.
And it's going to basically say, I'm
going to go through the counts that I have.
And I'm going to check to see if the count I'm looking at
is bigger than the biggest count I've seen so far.
And if it is, I'm going to remember it.
Now, don't worry about this.
We haven't even covered any of this stuff.
That's what chapter two, three, four, five, six.
But this is an algorithm, a paragraph, a pattern.
That allows you to find the largest number, and we'll
look at this again in great detail in upcoming chapters.
So this is kind of thinking like
a computer, having a sliding window across the
long list of numbers, and coming up with
something that is the answer that you need.
Okay, so that's the end of this lecture.
Read Chapter One, write your Hello World program.
Make sure if you haven't, get Python installed.
As you read this chapter and even as you
install Python, and even as you write the first program,
don't get too stuck on the details.
I was confused for like eight weeks or six
weeks in my first programming class; you'll be confused too.
Just sort of wander through with me, keep at it.
It will start making sense at some point that's up
to you, I can't tell you when it's going to make sense.
So, if, don't sort of stare at everything until you get it.
Just kind of keep digging in, and keep understanding, and keep playing and
sooner or later this'll make a lot of sense to you, I promise you.
See you next lecture.