Python for Informatics: Chapter 1 – Introduction
Articles,  Blog

Python for Informatics: Chapter 1 – Introduction


Hello and welcome to
Python for Informatics. Right now, we’re going to
cover chapter one on 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 if you have not yet done
it, please install Python. You’re going to do
it sooner or later. And you actually might as well
do it before this lecture. You can listen to this lecture,
obviously, without Python, but it allows you to play
with some of the things. And we might even do a little
bit of Python in this lecture and show you Python
in the lecture. And so, you can go along if you
have Python in another window. There is
pythonlearn.com/install.php has instructions for Microsoft
Windows and Macintosh. And it’s not on this slide, but
I just uploaded instructions on how to do this on a
Raspberry Pi– the new really cool $25 computer. And they are really easy
and straightforward. They’re complete screen
recordings, step by step, 10, 15 minutes at the maximum. You can stop them,
you could start them, you can download them
to your hard drive. They will walk you through what
it takes to install Python, if needed. Install text editor, if needed. And then run your very
first Python program. And you’re going to have
to run a Python program. So, this is as good of a
time as any to stop and get that done and then come back. OK? So, now, back to
the introduction. So, computers, basically,
want to be helpful. They are programmed– matter of
fact this is a micro processor. This is really just
an electrical part. It’s got wires and
circuits inside of it. And somebody spend 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 is like, tell me
what you want to do next? What do you want to do next? What you want to do next? And after that, what
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 low level piece. But you can also think
if you have a PDA, something like this, all
of the buttons on here are some kind of, what’s next? Right? Each of those is 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 nothing without you. We humans are the things
that cause computers to start doing something. And this will sit here
happily until I’ve caused it to do something. Now, whoa. Whoa. Hope it’s still OK. Yeah. Seems to be fine,
seems to be fine. Takes a licking and
keeps on ticking. 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, and 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 do it for free,
sometimes you get paid. Sometimes, you write
things for yourself. 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 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 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 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 of all 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 are 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 professional
programmer, but that’s not the
purpose of this class. Sometimes you just want
to get something done. You’ve got an Excel
spreadsheet at work, and the data is not right. You’ve got the data
from somebody else and it’s got extra
spaces where it shouldn’t have it or missing fields. Or something, you’ve
got to do something to it that Excel can’t do. And you’re stuck saying I
want to mess with this data and put 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 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 tend to build things
and 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 a next one. Give it back, in out, in out. So it’s really a sequence
of stored instructions. But it’s more than that. It’s 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. 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. That I could leverage the
capabilities inside of here, and I could do things, the 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 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 stop and let you
will watch the Macarena and then come back. 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
the 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, it
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 at 3
billion beats a second, but as it were– we’re going
to 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 look and see if
you can find the mistakes in the program. OK. 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, 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’s another example. OK, let’s see how
this comes out. You’re supposed to count
the number of times the word “the” appears
in this sentence. Count it. The word “the”, how many times? OK. It’s your turn. Now here, 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 3 million words and find the most common word. And it’ll never make a mistake. But 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 wanted let
you count words in files, OK? 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. You got a million words,
a million lines in a file, you want to find the
most common word. So I will send you this
file called words.py. I spent little time, it’s
a friendly gift to you. And this is what I
type in– now I’ll give you kind of outline on
what this is going to do. The first thing it’s going to
do is open the 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, this word is one,
this word is one, oh I saw that one again
so I turned 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 write another loop that’s going to figure out
which is the most common word, by looking through all
those little histograms that we built up. And then it’s going to print
those things are 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 it can find out that a
different thing as the word “two” and it occurs 16 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 be happily. And so you don’t have
to do menial work, once you understand the way
to instruct the computer to do menial work. [IMITATES KEYBOARD CLICK] So, we always start
all programming classes with hardware architecture. 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 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
into a hardware nut, I just want you to
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. 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
the microprocessor, and the memory is
in here as well, and it looks up with USB
and HDMI and various things. And, if you want,
in this course, you probably can do all 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 inside of a 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 so, we have a block diagram
of what’s going on in here. And there just some terminology. The brains of it all– well, we
draw this block diagram partly because– and this is
a, from a– oh, parts are coming off of this. I don’t know what that was. It’s OK, it’s broken anyways. 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 a lacquer. And then, there are things
to 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– central processing unit is the closest thing
to computer has to a brain. But it’s barely a
brain; it’s really just a super fast
programmable calculator. We make it flexible
by our creativity when we write programs. We make it seem intelligent. It’s people that
make an 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 program 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. 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 where a high
speed data is stored, and this is the thing
that asks, what next? And it reads its
instructions from here. And you see that can– boop–
they’re not quite connected together, but eventually
they’re connecting together. Don’t feel too bad about
this hardware, it’s all old, and it’s all broken
and can’t be hurt. So, the next thing we got
is input/output device. 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 an HDMI that you
can connect a monitor to, it 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, on
the Raspberry Pi, the CPU, the central
processing unit, and the RAM are all in 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, they’ve
collapsed this and this, and a lot of this, all down in a
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, et cetera. So input/output
devices– oh and this guy has input/output devices, too,
like USB, keyboard, monitor, et cetera. So there are very similar,
it’s just this is new and this is old. Everything gets smaller
when it gets newer. OK. OK, so last thing we’re
going to talk about is the secondary memory. 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 this
secondary storage is for. This is permanent. This is fast. And it cha cha cha
cha really fast. But this is permanent
and this is slower. OK? So the secondary
memory– you got two kinds of secondary memory–
oh, dropped it on the floor. Two kinds of a 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 it’s 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. 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, our secondary memory
was a disk drive. And it had 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 a bit. 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, 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
brings the operation. Main memory, fast but goes
away when we power off. Input/output devices–
keyboards, et cetera. And then storage that maintains
its data across power cycles. OK. And, I just said all that, OK? So then, the question is,
where do you belong in this? Where do programs live? Where do we write? And the answer is, we
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 3, print, but that’s what we
tend to want to write. Because what the computers
really execute is this thing called machine languages,
which is a series of 0s and 1s 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 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 they 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–
that 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. OK, so welcome back. So the next thing I’m
going to show you– I showed you a hard
disk that didn’t work, but we’re actually
going to show you real short video on
how a hard disk works 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 few minutes
and then the drive destroys itself if you
run it with the cover off. So let’s take a look at this. OK, 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. There’s lots of different
programming languages– Python, JavaScript C++, tons of them. Python is 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 maybe even hereditary. Nearly all known
Parselmouth 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 are 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. 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 him. 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? So, let’s see. 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, I think he
was trying to capture a playfulness, a certain kind
of silly, fun aspect of Python. And, so there we go. Enough of that. We done? Yeah. OK, the music’s done now. Thank Heaven for that. OK. 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, and I think we should,
like, indent this and do these things. And he’s made choices. And some languages, people
like some different, or better than others. It’s kind of an artistic choice. And I like to equate this to
learning a language to speak with people, with humans When we’re a baby, we
don’t know how to talk and we start babbling–
baba blah blah blah blah. Maybe we even just start
crying is first thing we do. But we’re on this
strange planet. We’ve 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 I say a word
incorrectly like, [SLURS WORD]. Right? I don’t know, that
was pretty dumb. Someone listening will say,
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 it’s cute, and it says, syntax error. And we go like, OK,
let me try this– and it says syntax error. And so, we get this notion–
I had this problem when I was first programming. I would like, here’s my
program, do you like it? And 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 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? Can you talk to a dog? Can you say, lovely sunset
we’re having, to a dog. Because dog’s not going
to understand that. The dog does understand some
things like, food, bath, walk. But it doesn’t understand
the accumulated works of Shakespeare. So when you talk
to a dog, you got to 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 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 that you kind of type
anything to 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 type C colon
backslash 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 waiting for you
tell it what to do, OK? 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? And 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? OK? Now, it’s like I just
landed on 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, arf, right? I could say, take me
me to your leader. Arf. OK? Are you friendly? I don’t think I
spelled friendly right. Syntax error. Are you dumb? Syntax error. Pretty dumb. I hate computers. Syntax error. It doesn’t seem to
have a sense of humor. Try this. Knock knock Knock knock. No sense of humor. So here’s the
problem, it wants us– it doesn’t hate
us– 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, OK? Do that. Now, it’s happy, because
I know the language. Bonjour. So we know the language. 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 go
back, and remember that x I gave you before? Go get that one back, and add
50 to that, and put that in one. So now, I’ve got something in
x and I 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 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 you type exit or
quit, with parentheses, to get out of it
when you’re done. And that ends the
interactive session. This is interactive
Python, where it’s asking us
command by command and then interpreting, or
running those commands, as we finish. So, you’ll be doing 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’ve got to still teach
you this language. So what we say when we
get a hold of Python, what kinds of things? Just like any language, a human
language, there’s vocabulary, there’s basic words,
there’s variables and reserved words in Python. Then we combine those
in lines to make sentence-like structures,
that themselves are not a full story. And then we make
a story out of it. Now, the story is in
the Python language, not English or French,
or a human language, but it still is kind of 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 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,
they’re kind of paragraphs. There’s kind of a
paragraph, paragraph– maybe I should put a
different color here– here’s a paragraph and a
paragraph and a paragraph. And then at the end of the
day, once you 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’ve got to 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– they’re
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 it’s stated purpose; we’ll learn
what those are. Now if we talk about sentences,
sentences are, in Python, like a line that
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. Especially with this assignment. And then print prints
a reserved word. One that was on the list on
the previous slide, and then go read that variable. So, these are like
three sentences in our new little language. OK. So that’s sentences. Now, paragraphs. Let’s talk about paragraphs. Paragraphs are the
combination of sentences to make 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 if you went through
the prerequisite, you will have seen me edit in
a text editor, save the file, and then run from
the Python file. OK? So we call these files–
put dot py on the end them, py on the end of them. And we’re giving Python
a script to execute. So interactive, you’re
typing directly into Python and it’s doing it right
as you’re talking. You’re still doing
it in an order, and the order does matter. In a script, you type it all
into a file once and say, Python do it all. 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. It’s like a recipe, a
set of instructions. Start at the beginning,
tsh tsh tsh tsh tsh, 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 set of steps we do over and over again. So here’s some pictures. And here’s a four
lines of Python, a little, simple paragraph. And it’s got a sentence that
says, x equals 2, print 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. So the output of this
program is 2 then 4. Because x was 2, we printed
it, then we added 2 to it, and then we printed
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 equals 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 business. 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 blah stop. So there’s things like,
what if you do this or what if you do that? So we do things like, 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 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. In this case, since x
is 5, we execute it, then we come back here,
we’re going to 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 the output is
smaller, [? finni. ?] Now, here, we can take a look
at it 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 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 it either sort
of like gestalt. Say, if this is true, do what’s indented. Or you can imagine 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. OK. Now, the next thing I want to
show you is repeated steps. Steps that happen over
and over and over again. OK? 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. 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, reserve 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, outcomes 5. Then it says, 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 greater 0? Yes, it is. Print out 3. Subtract 1, now it’s 2. So outcome 3 and 2. And it goes back up. Still greater than 0? Yes, it is. Now it’s 1. 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
becomes the printer blastoff. So this is a loop. The notion that we’re going
to run this little bit of code five times. 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
control the loop. We can decide when
it starts and when it stops we can tell if we’re
at the beginning, or 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. OK? Any questions? [CHUCKLES] Can’t do questions. OK. 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 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 four. 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 four
loop, and that’s going to run a bunch of times. And then we have an if,
which may or may not run. So this little block of
code is conditionally executed, based on
something, and here’s the question 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. OK? Just those concepts show up
in pretty much every program that we build. OK. So let’s do a couple
more little exercises that get you sort of in the
mindset 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 give
you several sets of numbers, and I want you to
find the largest number in the 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, OK? So don’t just get the answer,
that’s not so important. Think about how
you get the answer. So don’t just 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 tell me what
the largest number is. Ready? Set. Go. I didn’t make it easy. You’re looking for
the largest number. Did you get it? Did you get it? Did you have to go
back a couple times? Actually, I don’t care
what the answer is. The question is– how
was your brain solving? OK. 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? Do humans– did you look at
25, then you looked at 1, then you looked at 114,
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. OK, 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, OK, given that
there’s no four-digit numbers, there are 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 nines– you say, OK, there’s some nines. So that means 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? Ugh. Oh, 198. Right there. Had it color coded,
I could even see it. OK But the point is, humans
are great at eliminating 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 know 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 a large number, but to monitor, as you go,
what your brain is thinking while you’re doing this. OK? 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. 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 to turn my head
upside down, or something. It’s a really hard problem. The other one was kind of easy. You might not have
got it, but you had this natural
instinct that allows you to approach the problem. OK, I’ll show you what
the right answer is. The right answer
is– right there. It is 197. Yay. Right. You can’t– even if I tell
you, there you are– you know, what is this? Is this 500 or 2 00? [MOANING] 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
is frontwards or backwards should make no difference. Right? Computers need a strategy. We need to give them a strategy. OK. So here we go. 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 the same time, so
you probably created a variable in your head without
even knowing it. And you put into
that variable, you called the variable, the
largest number I’ve seen so far. And you hadn’t seen
any, so let’s say the largest number you’ve
seen so far is negative 1. Then, I showed you 3. And you said 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
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. 9. 9. Not nearly as good as 41,
so I’ll throw that away. 74? Better, better. Keep it. Keep that one. So I’ll keep 74. And the last number’s 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 like thinking
like a program. This kind of sliding window. Didn’t matter if I gave you
a billion numbers or five numbers– I think the 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. I don’t want you 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 loop 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
is 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 covered
any of this stuff. That’s a 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 thinking
like a computer, having the sliding window
across the long list of numbers and coming up with
something that is the answer that you need. OK. 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– well, probably 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, don’t stare at
everything until you get it, just keep digging in and keep
understanding and keep playing, and sooner or later this will
make a lot of sense to you. I promise you. See you next lecture.

62 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *