(upbeat music)
- Loops are programming constructs
which repeatedly perform an operation,
or series of operations, over, and over, and over.
There are two types of loops, For Loops, and While Loops.
One of the key differences between them
is that with a for loop, you know exactly how many times
that loop is going to perform its operation,
before it even runs.
Some languages do this by creating a variable
that will count up to a certain value.
And for loops, can also be used
to iterate through items in a list.
That list will have a certain number of items,
which will be the number of times the for loop executes.
With a while loop, you don't know how many times
the loop is going to be executed.
Instead, as the loop is running,
it's constantly checking and checking for a condition,
to determine whether or not,
it should execute, one more time.
I'll use loops to help me perform a complex operation
that I'm very familiar with, doing the dishes.
My kitchen is a little bit of a mess,
and I want to clean it up before my friends come over.
I'll start by loading the dirty dishes from the sink,
into the dishwasher, with the for loop.
I know exactly, how many dishes I'm going to move
from the sink to the dishwasher,
before I start this task.
Because there are exactly, three dirty dishes in my sink.
And I won't stop loading the dishwasher,
until I've completed that task
for all three of those dishes.
So, I'll pick up the bowl,
(sink clanks)
and put it in the dishwasher.
(bowl clanks)
That's one item.
Then, I'll pick up the plate,
and put that in the dishwasher.
(plates clanking)
That's two.
And finally, I'll pick up the cup,
(dish washer clanking)
and put that in the dishwasher.
(cup clanks)
Three.
Now, after loading those three dishes,
I've reached the end of my list.
And since a for loop, operates on each element in the list,
I'm done.
I'll never load more than three dishes
because that's how many dishes were in the sink.
This is the example script,
which represents the process of loading the dishwasher.
I've created a list called "sink,"
which has three dirty dishes in it.
The bowl, the plate, and the cup.
Then, I use a for loop to iterate through those dishes.
For loops can be used to iterate through the items in a list
or some other type of iterable object using this syntax.
For each iteration of the loop,
the name dish, will point to a different object in the list.
And then, print a statement to simulate the act
of putting that dish into the dishwasher.
Just like with function definitions, Python uses indentation
to identify the lines of code that exists within a loop.
When I run this script,
it starts by putting away the bowl, then the plate,
and finally, the cup.
This for loop will iterate
through all of the dishes in the sink.
It will always do the exact same thing,
put away those three dishes.
I'll never put away more or less dishes than that.
Now, using a print statement to say
that I'm putting away a dish, is not really the same as
removing that dish object from the sink.
In fact, if I display the contents of the sink list,
you'll see that after running that script,
the sink is still full of dishes.
The program didn't actually remove anything.
So, let's modify the script,
so that when a dish goes into the dishwasher,
it also removes that object from the sink list.
I can do that by using the remove method, on the sink list,
and passing it, the dish, to be removed.
When I save and run the script, the print statements
only put the bowl and the cup into the dishwasher.
And if I check the sink?
Sure enough, the plate is still there.
So, what's going on?
Well, what I've accidentally done here,
is make changes to the list that I'm iterating through,
by removing objects from it inside of the for loop.
The first iteration through the loop
grabs the first dish in the list, which is the bowl,
and then removes it from the list.
Now, the problem occurs
in the next iteration of the for loop,
because it retrieves the second object in the sink list.
But the second object in the list is no longer the plate.
Now, it's the cup.
When the bowl was removed from the list
in the first loop iteration,
that caused everything to shift up in the list.
So now, the plate, is in the first position,
and the cup, is second.
This is why you should be careful
when adding or removing items from a list,
inside of a for loop, that's iterating over the same list.
It causes all sorts of confusion.
So, how can we get around this problem?
Well, I need to remove items from the sink list
inside of the for loop.
So, rather than having the for loop
iterate through that sink,
I'll make a separate copy of the sink
that the for loop can use to iterate.
I can do that by calling the list function,
on sink, in the for statement.
This will create a copy of the sink list
that the for loop will use to iterate through,
leaving the original sink free to modify as I need to.
Now, I'll save and run the script.
It shows that all three dishes were put into the dishwasher.
That's a good sign.
And, if I check the contents of the sink,
it's an empty list, which is what I want.
The moral of the story
is to avoid changing the contents of a list,
inside of a for loop, that's iterating through that list.
Create a second copy for the loop to use,
and you can do whatever you want to the original list.
(upbeat music)