Let's talk about Loop's, we've talked about conditional operators, logical operators, but Loop's
gives us a whole new power when it comes to our machines.
The concept of loop's or looping in programming is really, really powerful.
We saw that with logical operators and conditional logic were 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 key
word 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 four.
But then we have this thing right where we have item which what is that?
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 end.
So saying, hey, for every item in 02 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, and 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 imprints each item one at a time.
Now, this works with strings, but we can also use lists like one, two, three, four or 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.
Can we do this with a set?
Well, if I click run.
That works as well.
What about a couple?
Let's do it at the top.
In fact, like 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 Colline and then the indentation to tell Python, hey, whatever
comes here, I can print item again, 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 I print something else.
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 and 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.
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 for 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 mess things in Python and as a matter of fact, when we do conditionals in Python
like if statements, we can always nesse 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.
So let's go to be 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 to A, to B to C, so you can have nested loops over and
over and over as well.
All right, so these are loops and right now, maybe it's not completely obvious why this may be useful,
but before we get into that, I want to talk about this idea of iterable in the next video.