In previous section, you learned how to make a simple list and you learned how to work with individual
elements in list.
In this section, you will learn how to loop through an entire list using just a few lines of code,
regardless of how long the list is.
So looping allows you to make the same action or set of actions with every item in a list.
As a result, you will be able to work efficiently with a list of any length, including those with
thousands or even millions of items.
And you will often want to run through all entries in the list.
So performing the same task with each item.
For example, in a game you might want to move every element.
But on the screen to the same account practice.
Um, or in a list of numbers you might want to perform the same statistical operation on every element.
Or perhaps you will want to display each headline from a list of articles on a website.
So when you want to do the same action with every item in a list, you can use The python's for loop,
Python's for loop.
So say we have a list of magician's names and we want to print out each name in the list.
We could do this by retrieving each name from the list individually, but this approach could cause
several problems here.
For one, it would be repetitive to do this with a long list of names.
Also, we'd have to change our code each time the list length created and using the for loop avoids
both of these uses by letting Python manage these uses internally.
So let's use a for loop to print out each name in a list of magicians.
So here actually mutations.
So just like that.
Yes.
So, magicians.
And let's actually make it Alice.
Alice.
David.
David and Carolina.
And here we will create a for loop here.
So for magician.
In magicians.
And after that we will print the magician every time.
And here we begin by defining a list, just as we did in previous lectures.
Oops.
Here, let me fix my mouse right now.
Yes.
So here we go.
We begin by defining a list, just as we did in previous lectures here.
So.
Then we define a for loop.
This is new to us, right?
So then we define the for loop.
So this line.
Tells Python to pull a name from the list.
Magicians.
And.
Associated with the variable name magician.
Next we tell Python to print the name that just been assigned to this magician here.
Biden then repeats these last two lines, one for each name in the list.
It might help to read this code as like this.
So here we for this code, we have this explanation.
Every magician in the list of magicians.
Magicians.
Print the magician's.
Nay.
So the output is simple printout of each name in the list.
Let's try this out.
As you can see here, we printed every user in the in the our magicians list without a problem.
Now looping is important because it's one of the most common ways of computer automates repetitive tasks.
For example, in a simple loop like we used in this file, in this lecture.
Here.
Python initially reads the first line of the loop.
This line here.
So this line tells Python to retrieve the first value from the list magicians and associated with the
variable magician and the first value here.
So here, actually, let me write that down here.
Okay.
So the first value here, The first.
Oh, sorry.
Here.
The first value here.
Is Alice, right?
Yes.
The first value here is.
And the second value.
And I'll also python prints the first value as Alice and the current.
This is the current value of magician right now which is still Alice because the list contains more
values and Python returns to the first line of the loop.
Right.
So Python retrieves the next name in the list, which is.
In this case.
David Right.
So Python also retrieves the.
David.
And associates that value within the variable magician here.
And Python then executes the line which is print the magician and Python prints the current value of
magician again which is now is David and Python repeats the entire loop once more with the last value
in the list, which is Caroline.
This here.
So because no more values are in the list.
Python moves on to the next line of the program.
In this case, nothing.
Nothing comes after this line here.
So this is empty here for now.
And.
And nothing comes after the for loop.
And so for loop will end here.
So when you are using the loops for the first time, keep in mind that the set of steps is repeated
once for each item in the list.
So no matter how many items are in the list and how much.
So if you have a million items in your list, the python repeats these steps a million times and usually
very quickly.
Also, keep in mind when writing your own for loops that you can choose any name you want for the temporary
variable.
That will be associated with each value in the list.
So however itself will do, choose a meaningful name that represents a single item for the list.
So for example, you can.
Let's actually create a good way to for naming these loops here.
So another good way for naming this loop is, for example, look for Cat in cats or for dog in dogs
or for item in list of items.
Okay, so these are the just examples that you can name your loops.
So to make it more meaningful and readable.
Right.
So these naming conventions can help you to follow the action behind being done on each item within
a for loop.
And using singular and plural names can help you identify whether a section of code is working with
a single element from the list or the entire list at.