Hello. This lesson is going to introduce
Python commands that control how a computer actually executes a program.
Basically, when your Python interpreter is reading your Python code,
it processes each data, and it does something.
These commands will actually tell it to change the order in which things are done.
So for instance, you may want to do a specific task if a condition is true,
and do a different task if a different condition is true.
You also may want to continually execute the same operations over,
until a certain condition is met.
So, specifically what you're going to learn in this lesson,
is how to write effective 'if' statements.
This will allow you to perform conditional code evaluation.
You'll also learn how to write 'for' statements that
allow you to iteratively process data.
And to write 'while' statements that will allow you to perform
predefined operations repeatedly until a stopping condition is reached.
So, in the notebook for this lesson,
which we've called Python Flow Control because we're
controlling the flow through a program, or a script.
We're going to look at these statements in more detail.
First, is the 'if' statement.
The 'if' statement is very simple.
We write 'if', and then we have a test which evaluates to either true or false.
So in this case, if e x is greater than 10 we will do this statement right here.
We end the 'if' statement with the colon.
We then indent the next statement because this is what's called a code block.
We saw this with functions.
Now, we can have compound 'if' statements,
where we say if x is greater than 10, do this.
Otherwise or else if x is greater than five do this.
Else, do this.
So, you can see that if x is 10 we have a big or large number.
If x is less than 10 but greater than five,
we have a medium number.
Otherwise, x is less than five,
that means x is small.
That's all these 'if' statements are doing.
And you can see how by putting them together with perhaps more complex tests,
we can have a very powerful conditional statement.
And you could see that we could also nest this.
So we can have if x is greater than 10,
and then by indenting four spaces if y is greater than 10,
indenting four spaces, we have x and y are large.
Else, x is large but y is not.
And you can see how the flow goes through these.
Now, important to note.
If x is greater than 10,
we're going to execute this code block here
because it's indented four spaces, and none of the rest.
If x is not greater than 10,
we're going to come down to this one,
and test is x greater than five.
If so, we will print this statement or do this test,
et cetera down to this else.
It takes a little bit of time,
but if you think about it,
you'll be able to quickly understand and read these if, elif, else statements.
So, try that out and see how you can do.
Here's that code that I just showed you,
but it's actually in a code cell.
You should change the values here of x and y,
and make sure that you understand how these if,
else if, and else statements are actually working.
Next, we go into iterations and loop statements.
One of the powerful things about the compound data structures that you learned earlier,
the list the top of the string in the dictionary,
is that we can iterate through them quickly.
So for example, here's a list of data and the four statement takes the item out of data.
So in this case, it'll take one.
It'll do something, and then it'll print.
And then it goes up and takes the second item two et cetera,
and then three et cetera,
and then four et cetera.
All the way it until it gets to the end.
And when it reaches the end of the data,
it exits the loop.
The same thing works for tuples and also for strings,
and for dictionaries although there's a little bit of a difference because here,
we have a key and a value.
So, when we access the items out of our dictionary,
we'll get the key and the value.
Now sometimes you just want to do the same thing over and over again,
in that case, you use the range.
What this does, is range creates a list
of numbers and this case zero, one, two, three, four, five, six, seven, eight,
nine because Python is zero index,
we start at zero and we end before the 10,
and then we iterate through them.
So, you should test these things out in
the different code cells and make sure you understand how they all work.
The other loop condition is called 'while'.
Which 'while' this condition is true,
does the indented statements or the code block.
This will become an infinite loop if you
never change the thing that's actually tested here.
So, you need to make sure you keep that in mind and that's
demonstrated in this particular code cell here,
where we change value.
It starts at one, and we keep iterating through until we reach a million.
That's what 1E6 is.
It's a shorthand for one million.
These also can take other optional things here.
The 'for' and 'while' can take else.
We can also break out of them,
and we can also use continue in the notebook talks about how to use those.
And lastly, the 'pass' it does nothing,
but sometimes you need to have a statement in there to make sure it's clear.
That it is the body of the loop.
So, this should show you how to use these things.
The last part of this talks about list comprehensions or dictionary comprehensions.
These are a powerful way to quickly build up a list,
and it uses an implicit loop inside.
This is optional, so you don't have to do it.
But it's something that's very powerful,
and you will see in notebooks later on in this course.
And so, it does behoove you,
if you can to try to go through this and understand this.
With that, I will stop.
If you have any questions, let us know in the class forums. Good luck.