Okay, let's talk about for loops.
We've actually seen these in several videos so far just
as little examples.
But a for loop is really just something that's going to
loop through an iterable until it reaches the end, and then
it stops on its own.
Now, we haven't talked too much about iterables, but an iterable
is anything that can be looped over. And up until this point,
every sequence that we've worked with a string, list, tuple,
set, even dictionary items, those are all loopable, so they're
iterable. So let's just go through some examples of for
loops. So let's create a list, and this list is going to be 'pets'
["Cat", "Dog", "Bird", "Goat"]', because goats are funny.
So just to confirm what we have, we have ["Cat", "Dog", "Bird", "Goat"]'
And so we can say, 'for', an animal, 'animal in pets',
and we could do something strange here where we can replace
all the a's with a letter 4 and all the o's with the letter
0, or number 4, number 0.
And this could be an exclamation mark, just to turn it upside
down. So it's going to look a little weird.
But we could do that.
So we could say 'animal = animal',
actually, let's back up a second. 'print(animal)', and it's going
to print out each item one at a time.
So now what we can do is, 'for animal in pets',
we can now use this 'animal' because it's going to be 'animal = "Cat"',
'animal = "Bird"', 'animal = "Dog"',
'animal = "Goat"'.
Now we can do 'animal = animal',
and because we know that this is a string, we have a few
different things that we can work with here. Such as the
'replace' method.
So we can replace "a" with a "4",
we can '.replace("o", "0"),
and we can replace any "i" with an "!",
and then we can say, 'print("The animal with funky letters is",
and we'll just use an f-string.
Make sure we put an 'f' in front of that, because that's an f-string.
And let's run this.
"The animal with funky letters is C4t", "Cat".
So the "a" turned into a "4", the "o" turned into a "0", the
"!" turned into, the "i" turned into "!",
and "Goat" got both "o" and "a", turned into a "0" and "4".
And so basically, what this for loop is letting us do is
loop through all the items in this list in order, and we
can do anything we want.
We can change and work with that particular variable.
And in every single iteration all we're doing is, saying
'animal = "Cat"',
the 'animal = "Dog"', the 'animal = "Bird"', the
'animal = "Goat"'.
So now let's go through this example with a set.
So we have 'pets'.
That's a list.
But what we can do is turn 'pets' into a set by casting it,
and this is the exact same thing as saying 'pets =
'
And actually, to keep things simple, let's not cast that as a set.
Let's just use this, and 'print(pets)'.
And so now it looks almost the exact same, but it's in a
set. So we can't have multiple cats, multiple goats, anything like that.
So let's add a second "Goat", and it still just shows up with
one individual "Goat".
Now, sets do not keep their original input or their input order, rather.
So just because it says, '' doesn't mean
it's always going to be ', it's already in the wrong order, "Goat".
But every item in this particular sequence is going to be completely unique.
So now we could say, 'for set_animal', we just use a different
variable name here, 'in pets: print(set_animal)'.
[no audio]
And let's add a second parameter to our 'print' statement
just to switch it up, "is the animal".
And there we go, "Cat", "Dog", "Goat", and "Bird".
And so the syntax that we're looking at here is 'for',
and then whatever your variable name is going to be, 'in', whatever
sequence you're looping through, whatever iterable you're looping through.
Now, it's called an iterable, because we can iterate through each one.
We can iterate through 'Cat', 'Dog', 'Bird', and 'Goat'
Tuples work the exact same way,
so I'm not even going to show you an example of that because
it is literally the exact same thing as a list.
We can also loop through letters in a word.
So let's say the 'word' is going to be "Pythonisthebest". Because
I said word instead of sentence,
so I'm going to stick with it.
'for letter in word: print(letter)'.
And this is just going to go through each letter in here,
assign every letter to the variable called 'letter', and print
it out one by one, just like that.
[no audio]
Now I'm going to show you an example that's going to get
a little more advanced, and by no means you need to be familiar
with this right now,
but I would like you to get some eyes on something a little
more complicated here.
So let's loop through a tuple of tuples, and this tuple
of tuples is going to look very, very strange at first.
So let's say we have a tuple of 'people'.
And in this particular tuple, let's even put this on a new
line, we have the age of someone, 30; and the name.
Okay, that's one tuple.
Let's put another one in here.
We've got 5,
and the name, "Zephyr".
Let's put another one in here.
"Henry" is going to be 3 this year,
and "Henry".
And when we run 'people', we can see that it gets flattened.
And this is a tuple of tuples.
So the whole thing is a tuple.
So it's an iterable.
And inside of it is a pair of tuples.
So we've got one item that's an age, and the second item
is always a name.
So we're going to loop through these now.
So we're going to say, 'for age, name in people',
and what this it's going to do, is loop through it,
and it's going to say that first item is going to be age,
and that second item is going to be the variable name.
So now we can say 'print()', with an 'f' statement, " is years old".
And look at that. "Kalob is 30 years old", and "Zephyr is 5", and "Henry is 3 years
old". Now, this is called unpacking.
And what happens if one of these does not work out?
So let's say this last one, Henry doesn't have a name.
Let's rerun these.
It will work until it doesn't,
and we get this thing called a 'ValueError', and it says, "not
enough values to unpack".
So let's undo that,
and let's see what happens when there are too many values.
Let's just say, 'This is a third value'.
Rerun these cells.
And again, we can see that the first two work, but Henry
has a third value, and this one is also a 'ValueError',
but this one says, "too many values to unpack".
So your tuple of tuples needs to have the exact same formatting
through the entire thing.
And when you do that, you can then unpack it.
So this is variable age, and that's the name;
variable age, variable name; variable age, variable name.
Now, I'm not expecting you to know that right now, but this
is a slightly more advanced feature in loops, and it is going
to be very, very useful in the very near future.
I believe in the next 2 or 3 videos, we are going to get
into a little bit more unpacking when it comes to looping
through a dictionary.
Now, what I would like you to do, is create a list and then
write a for loop.
It does not need to be amazing.
It does not need to be super complex.
All it needs to do, is loop through every item in a list.
Once you've learned how to loop through a list, it is the
exact same for a tuple,
exact same for set, the exact same for a string.
[no audio]