Good, now you know the definition of a loop and you know that we have for
loops and while loops in Python and in this lecture you'll learn how to write
for loops. Now a for loop normally iterates through a list of objects and
applies some methods or some functions or some operations over each of these
objects. The array that the for loop iterates through is not necessarily a
list. It can be a tuple. Sometimes it can be a dictionary. It can even be a
string so every object that is made of other simpler objects, so let's create a list.
First let's say emails is a list of three emails, three mail addresses, so
let's write some fake email addresses. Me at gmail.com you hotmail.com and
they at gmail.com. Now let's say you want to print out, you want to apply the print
function to each item of this list and the stupid way to do this would be to
print out every item one by one. Let's say the first item which has an index of
zero and then you do the same for emails with index of one and so on.
That's definitely not how we want to do it, so to print out these three items
we use a for loop and for loop always start with the for keyword and then you
pick a variable let's say email so don't confuse the email variable with the
emails variable, these are totally different. And for email in emails, so here you pass
the variable that calls the list you want to iterate through and press ENTER and
as you see here we got the indentation so the line that follows the first line
of the loop is always invented. So that means this line here that will write
belongs to this loop, so it's part of this block and here is where we write
the action, the statement we want to execute multiple times, so we want
to print the email so that is very
straightforward and very clean and very readable. Now let's execute this before
I explain it to you better. Save the script first and execute. Now this is
script 4, so we got the three items printed out on the screen and I
obviously got an ad wrong here, no problem. So what the code does is that
you say for each item in the list emails print aout the item and of course you can
use every variable that you like if this is more readable for you so for each
item in email please print out the item. Great, let's do something more fancy here.
I'll delete this line of code and now I would like to print out only the emails
that have Gmail address associated with them so I don't want to print out any
other item rather than the Gmail ones. So that means we want Python to make a
decision. If it finds Gmail in the item in the string, it will print it out, if it
doesn't find it will not print it out, so we want to use conditional here and that
would be an if statement. So again we say for each item in the list if the string
Gmail is in item print out the item so I'm sure this was easier than you were
expecting. Let's save ctrl s and execute. So we got
only the Gmail accounts print adult. So again we are saying for item in emails,
and when we do this Python grabs the first item of the list so the first
iteration is there is about the first item and this item is temporarily stored
in the variable item then while this value is stored in this variable, Python
checks if the string Gmail is in that variable, so with the first item this is
true and that's why python prints out the item, so it prints out
me@gmail.com then it goes to the next item. If gmail is in that item, it prints
out the item. In this case gmail is not there, so it doesn't do anything and it
goes to the next item, so that's why we call it loop, so the loop goes in and
out in and out until the list is exhausted so when it goes to the third
item to the last item on the list the loop ends and the output is generated
like here. And don't forget about indentation so be careful and look here
that we indented under the for loop and again we indented under the if
statement because this indentation here is part of the if block and this one
here is part of the for loop block so very readable code and I believe very
fun to write. I'll see you in the next lecture.