Tip:
Highlight text to annotate it
X
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]