Instructor: Let's talk about loops.
We've talked about conditional operators,
logical operators,
but loops gives us a whole new power
when it comes to our machines.
The concept of loops or looping in programming
is really, really powerful.
We saw that with logical operators
and conditional logic.
We're able to skip lines in our program.
So that we don't always go one, two, three,
four, five, six, so on
and so forth.
But loops do a really interesting thing.
It allows us to run
lines of code,
over and over and over
and that's really powerful
because that means we can run things thousands of times,
millions of times.
And this is where machines excel.
Machines excel
at doing small tasks, over
and over really, really fast.
Way better than humans.
So loops are one of the most powerful features
of programming languages.
So how do we create one?
Well, it's as simple as using, what we call 'for',
or in Python, we call them 'for' loops.
With this keyword 'for',
we're able to say 'for',
let's say item,
in zero to mastery,
do something.
So they're kind of like conditional operators
instead of 'if' we have 'for',
but then we have this
thing, right, where we have item, which,
what is that we, we don't really know.
Well, this is a variable that we create.
We can name it whatever we want.
We can say I,
We can say
teddybears,
whatever you want.
This is a variable.
And a variable is created here,
for each item
after the 'in'.
So it's saying, hey for every item in zero to mastery,
do something.
And we've seen this 'in' keyword before,
and we'll dig deep into this later,
but we call this an iterable.
An iterable is something that is able to get looped over.
So an iterable allows us to use this notation
of a 'for' something 'in' an iterable,
to iterate over each item.
In our case, if I print item here
and I click run,
look at that.
It prints each item in the iterable,
which is the string zero to mastery.
So every letter, it goes into every
bookshelf in our machine's memory
and prints each item one at a time.
Now, this works with strings,
but we can also use lists,
like one, two, three, four, five,
that we've seen before.
If I click run,
again this is an iterable,
we're able to iterate over it
and it grabs each item
in the list.
Let's do a
set.
Could we do this with a set?
Well, if I click run,
that works as well.
What about a topple?
Let's do it with a topple.
If I click run, that works as well.
That's amazing.
What about a dictionary?
Well, we'll get to dictionaries in a bit.
So for loops, allow us to iterate
over anything that has a collection of items.
So that in this case,
we're looping one, two, three, four, five times.
And you can see over here that we have the colon,
and then the indentation to tell Python,
hey, whatever comes here,
I can print item again.
I can do it as many times as possible,
as long as I have indentation,
it's going to keep printing
our
numbers,
but as soon as I open up
here
and print something else,
well, I only get that once because it's not in the loop.
So our program first runs this,
for one, so it's going to print one, three times,
then two, then three, then four, then five,
and then only finally then it goes to print.
You see how we're looping over
and over and over.
Now what happens if I try
and print item here?
And I click run.
Hmm.
Did you see that?
Item gets printed at the very end here?
I don't know if you can notice it,
but you see that there's four fives.
We've printed one, two, three, four,
each, three times, but five gets printed,
four times because this last print
at the very end is,
well, five.
Because by the time the loop ends, item,
the value of item is actually five.
Let me ask you this.
Can I do something like for,
let's say X, in another list
that contains strings, a, b,
and c.
Could I do something like this?
Absolutely!
I can nest things in Python.
And as a matter of fact,
when we do conditionals in Python,
like if statements,
we can always nest those as well.
Because in Python it's always the indentation, right?
So I can say print here, item,
and then also
print, and you know what?
Let's print them one next to the other.
So I'm going to say print item and print X.
If I click run,
you see that we're printing,
one and a,
one and b,
one and c,
because item we
run line one.
So item is currently one.
So we're going to go to the next line, line two,
and we're going to say,
we're going to loop over this iterable
and we're going to say, this is X.
So this is going to be one, this is going to be a.
And then Python comes back to line two,
because we're still in this loop,
and we're going to say, hey, what's X now?
Well, we're done with a, so let's go to b.
So it's going to run b
and then c.
And only once this is done,
does it go back out,
and now starts with two,
which is right here, two a, two b, two c.
So you can have nested loops
over and over and over as well.
Right, so these are loops.
And right now,
maybe it's not completely obvious why these may be useful,
but before we get into that I want to talk about,
this idea of iterable in the next video.