‫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 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 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 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 teddy bears.
‫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 two 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.
‫Can we do this with a set?
‫Well, if I click Run.
‫That works as well.
‫What about a tuple?
‫Let's do it with the tuple.
‫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 column 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.
‫And 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, 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 or 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 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 these may be useful.
‫But before we get into that, I want to talk about this idea of Iterable in the next video.