>> Let's introduce the concept of loops in Python.
One of my favorite things about Python
is they do a fantastic job of going, hey,
these are all the different things that you're going to need,
and not necessarily overloading you with a bunch of
additional things that you don't
necessarily need or could live without.
I think nowhere else is this more true than
when we're talking about loops inside of Python.
Now, what you're going to notice with loops inside of
Python is there's really actually only two.
You'll notice that there's a for
which honestly acts like a forEach if you're
familiar with that from
other programming languages, and there's a while.
There's no concept of an until,
there's no concept of maybe putting
the condition at the end of the loop,
or anything like that.
Because honestly, in almost every case,
there's other ways that you could write that without
needing the extraneous syntax.
So I love this about Python that's like,
"Hey, this is the tools that you're going to need.
Let's not overwhelm you with a whole bunch
of other ones where you're then trying to figure out,
"Okay well, when do I use this and when do I use?"
It's really nice that way.
So let's start off by looking at for loops inside of Python.
So there's the syntax.
So what we're going to do is we're going to save
for and then some variable right here.
So we'll say name in and then our list.
Now what will automatically happen
behind the scenes is the first time through,
name will be Christopher.
Then the second time through,
name will be Susan, that's it.
It will just automatically go through that list for us.
Each time it finds a new value,
it'll put it inside of name,
and then I can do whatever it is that I might want with it.
As with keeping with all of our examples,
we'll just print it out to the screen.
So there is my names of Christopher and Susan.
If you have a list of items and you use that for, that's perfect.
But you might be thinking, "Hey wait a minute,
how do I then loop X number of times?"
Well, the way you do that is by using
a built-in little function called range.
What range will do for you is it will
automatically create a list of numbers for you,
where this right here,
let me just change my color,
that little zero right there,
that's going to be the starting number.
That blue really isn't.
Let's do it this way.
There we go. We'll just circle it.
That right there, that's going to be our starting number.
Then the second parameter is going to
be the number of items that we're going to get.
So if I go in and I run this,
what you would notice is it will then print out zero and one.
So it behaves the exact same way that we saw before.
It's still going to do that for loop.
The only difference really is that when we call range,
it's just automatically going to
create that array of zero and one.
Yes, I know it looks like a four-year-old drawing on the screen
there rather than well however old I am,
but you get the picture there.
So if you want a loop a particular number of times,
then you use range for that.
Now, the last loop that's available to us is a while loop.
Now with a while loop what we get to specify is some condition.
So as long as something is true,
so in my case index is less than the length,
we're going to stay inside of that while loop.
Now, the cat with
the while loop is you have to change the condition.
At some point, this needs to test as false.
If it doesn't, you're going to be stuck in there forever,
you'll wind up getting a stack overflow error.
So you need to make sure that you change the condition.
In my case, I'm doing that right here at
the very bottom where I'm adding one to the index.
Now this is the example that I'm using and this is probably
the most common example that you'll
see when people are introducing loops,
but I have to be honest with you.
This is not a typical example of a while loop.
Typically, you would want to use a while loop in
situations where something is going to change automatically.
So if I was maybe reading through a list of
lines inside of a file or something like that,
that's perfect for a while loop.
In this example here where I've got a list,
I'd really want to use a for loop almost always.
If I maybe wanted to go like
every other item or something like that,
then maybe I go in and I use a while,
or if I'm looking for something,
then maybe I would use a while.
But most commonly, what I'm going to wind up doing
here is really just using a for loop.
In any event, let's get in and take a look at
some examples of a for loop and a while loop.