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.