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 what 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 0 2 mastery do something.
So they're kind of like conditional operators instead of if we have four.
But then we have this thing grade where we have an 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 n.
So think hey for every item in 0 2 mastery do something and we've seen this in keyword before and we'll
dig deep into this later but we call this an intolerable and irritable is something that is able to
get looped over.
So an irritable allows us to use this notation of A for something in an interval 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 interval
which is the strings 0 2 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 integral 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 temple let's do with the top one 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 call in 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 am 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 1 3 times then two then 3 and 4 then 5
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 ness 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 let's print them one next to the other one as a print item and print x.
If I click Run
you see that we're printing 1 and day 1 and b 1 and c because item we run line 1.
So item is currently 1 so we're gonna go to the next line.
Line 2.
And we're gonna say we're gonna loop over this interval and we're gonna say this is x.
So this is going to be 1.
This is going to be a.
And then Python comes back to line 2 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
2 which is right here to a to b to c so we 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 ignorable in the next video.