Welcome back. So, we've already covered sequences,
and in this lesson, we're going to cover iteration.
Iteration runs a piece of code for every item in a sequence,
be it a string, list or a tuple.
In order to do iteration,
we're going to use a for loop,
which is the first of many control structures that we're going to see in this course.
So, let's look at a for loop in action.
In order to create a for loop that's going to iterate over a sequence,
we start out by saying the keyword for,
which specifies that we want to create a for loop.
After the for, then we create something called a loop variable.
We could call our loop variable any valid variable name,
but in the case of this for loop,
we're calling our loop variable name.
After we declare a loop variable,
then we say in, and after that,
we specify what sequence we're actually iterating over.
So, in this case, we're iterating over this list,
which has one, two, three,
four, five, six, seven items.
What this for loop is going to do is it's first going to
assign name to be the first item in the sequence, so,
Joe, and then it's going to assign name to be the second item,
and then it's going to assign it to be the third item and so on.
After it assigns it to be any particular item in this sequence,
then we're going to run whatever is inside of the for loop.
In this case, the only code that we have inside of this for loop is this print statement,
which prints out "Hi" and then whatever name
our loop variable's value is "Please come to my party on Saturday".
So, when we run this code,
what we get is "Hi Joe,
please come to my party on Saturday" and then, "Hi Amy,
please come to my party on Saturday",
"Hi Brad, please come to my party on Saturday", and so on.
So, in general, what's happening here is that Python is running this code,
this print statement for every single item in this sequence,
and for every single item in the sequence,
it's its first assigning name to be that item,
and then we're printing out "Hi" and then whatever that name is,
"Please come to my party on Saturday".
So, for all seven items in this list,
then we get seven print statements.
So, let's look at the same code inside of CodeLens.
So again, we're iterating over this list of names and our loop variable, is called name.
So, the first time that we run this code,
you'll see that name gets assigned to the first item in our list.
In this case, name gets assigned to Joe.
Now, when we run this print statement to print out "Hi name,
please come to my party on Saturday",
then we print out, "Hi Joe,
please come to my party on Saturday.
Then next, name gets reassigned to the second item in our list.
So, in our case, it gets assigned to Amy,
and now when we print out "Hi name,
please come to my party on Saturday",
we get "Hi Amy,
please come to my party on Saturday".
Then name gets reassigned to the third item or Brad.
So now, when we print out "Hi name,
please come to my party on Saturday",
we get "Hi Brad,
please come to my party on Saturday".
This pattern repeats for every single item in the sequence that we're iterating over,
until we are done iterating over all of
the items in which case our program is terminated.
So, when we create a for loop,
let's say abstractly, it looks like something on the left hand side here.
So, we have some code that might come before,
we have some code that might come after,
and then we have our actual for loop.
We declare our for loop again by saying, "for",
and then our loop variable,
and then we say "in" and then we say whatever sequence we're iterating over.
Then inside of the for loop,
we have what's called a code block.
So, here the code block is represented in green.
The way that we say that this loop code block is inside of the for loop
as opposed to outside of the for loop is through either tabs or spaces.
So, we have to indent the loop code block to
indicate that this code belongs to this leaf.
So, when we declare a for loop like this,
then this is equivalent to first designing the loop variable,
in our case x, to the first item in the sequence,
and then running the loop code block.
Then assigning our loop variable x to the second item in our sequence,
and then running the loop code block and so on and so forth.
So, assigning x to the third item, the fourth item,
and so on until we finally assign our loop variable x to the last item,
and running the loop code block.
So, one thing to note here is that
the loop code block may or may not reference our loop variable,
but if it does reference our loop variable,
then that reference is going to change every single time.
So, for example, in our previous code,
we referenced our loop variable name inside of the code block in our loop.
So, that means that name is going to change for every single item in our list.
So, in addition to iterating over lists,
for loops allow you to iterate over any sequence,
including strings, lists and tuples.
So, let's go over an example of iterating over a string.
So, here we're iterating over the string "Go Spot Go",
and we're calling our loop variable achar.
So, what that means is that the first time this loop body runs,
then achar is going to be assigned to the character capital G,
the second time that it runs it's going to be assigned to the character lowercase o,
the third time that it runs it's going to be assigned to a space character,
and then capital S, and so on.
So, what that means is that when we run our code,
then when we're first going to print capital G, and then lowercase o,
and space and so on for every character in the string,
and because print prints out characters on a separate line,
then we get Go Spot go printed out vertically.
So, let's go over a multiple choice question.
First, we assign the string S to be python rocks,
and then we say for every character in s. So,
again here, our loop variable is ch,
and the thing that we're iterating over is s,
and our loop body consists of one print statement that prints out the string "HELLO".
The question is how many times is the word "HELLO" printed by the following statements?
So, one thing that we know is
that ch is going to be assigned to the first character in s,
then the second character,
then third character and so on.
What that means is that this loop body is going to run
once for every character in the string s. So in other words,
this loop body is going to run however many times s is long.
So, if s were one-character,
then it would run one time,
if s for two characters it would run two times.
So, in order to find out how many times the word "HELLO" is printed out in this code,
I'm going to find out what's the length of s,
and I find out that it's one, two, three, four,
five, six, seven, eight, nine, 10, 11,
12 characters long, which means that I should expect this code to run 12 times.
So, next we're asked how many
times is the word HELLO printed out by the following statement?
So, here we're assigning S to the same value,
and we have the same loop variable ch.
Now, rather than iterating over the entirety of s,
we're iterating over a slice of s. Specifically,
we're iterating over s from indices three to eight.
So, what that means is that we're iterating over s from zero, one, two,
three from here to four,
five, six, seven, eight to here.
So, we should expect this to run one,
two, three, four, five times.
So, we can see that because a five character sequence is returned by the slice,
then we run our for loop five times.
So, in this code example,
we create a list that has four items,
and we have a loop variable named afruit,
and we iterate over our for item list.
Now, when we print out the value of our loop variable afruit,
then we should expect to get every item of our list printed out on a separate line.
For loops also work even if we don't care about the sequence that we're iterating over.
So, here we have an example that uses turtle,
and let's suppose that we want to draw a square with our turtle,
one way to draw a square,
is by going forward and then left four different times.
So, we first set up our turtle,
and we call it turtle alex,
and then we create a for loop and we have our loop variable i
and we have the sequence that we're iterating over which is a four items list,
but here inside of our for loop,
we're not referencing our loop variable at all.
Instead, we're taking advantage of the fact that we're repeating this code four
times in order to form a square by going left and forward four times in a row.
It doesn't matter what we're actually repeating over
if we're not referencing the loop variable.
So, for example, here we're looping over
a list of strings that represent different colors,
but we're still not referencing
our loop variable aColor in any of the code inside of our for loop.
So, when we run our code,
then we still get the same result,
which is a square with four sides,
and every side is still black.
If we did want to reference our loop variable,
then we might want to say something like this.
So, here we're referencing
our loop variable aColor by assigning that color to our turtle alex.
So, first aColor is assigned to yellow,
then red then purple then blue.
So, the result here is that every side of our square
ends up being a different color because we first go with yellow,
then red, then purple, then blue.
So, let's answer just a few more questions on for loops.
So, here we have a list that has one,
two, three, four, five, six,
seven, eight, nine items,
and we're asked how many times will the for loop
iterate through the following statements?
So, because ch gets assigned to all nine items once,
then we should expect this to execute nine times.
So, this question asks,
how does Python know what statements are contained in a loop body.
So, Python always strikes the indentation of
statements to check to see if it's in the loop body.
So, we can have more than one line in the loop body
even though many of the examples that we went over only had one line,
and the way that Python knows if something is in the loop body
is if it's indented the same after we declare the for loop.
That's all for now, until next time.