Welcome back, everyone.
We're now going to switch gears and our discussion of control flow to begin discussing loops.
We're going to begin with the for loop.
The term enterable means you can quote unquote iterate over the object.
So what do we mean by iterate?
That means that for every item in a sequence, you can perform an action.
So, for example, remember that a string is a sequence of characters so you can perform an action or
use every character in a string.
Or you could iterate over every item in a list that is perform an action for each item in the list or
use each item in the list.
And you can even iterate over every key in a dictionary.
So let's talk about the syntax for for loop.
This is what the typical syntax for for loop looks like.
Here I have my interval as a variable.
It's equal to a list and then I'm performing the for loop.
Let's break down what's happening here.
So first, I'm actually assigning an interval object here.
We can think of sequences as the adorable object here, and we have a list of one two three.
Besides that variable, then you actually get to choose the name of a temporary variable that is going
to be the placeholder for each item in that list.
So it's going to be either the one, the two or three, and you're going to say for each item in that
list in my interval.
So note the syntax here you say for your made up variable name in then your object, you perform an
action.
This is a very simple action performed here, just printing out the items in the list.
But that's all we need to do here to see the results.
One two three.
Notice how they are on separate lines each one.
So it's going one at a time through each item in that list.
Let's check this out in more detail in our code editor.
OK, so we're going to do is just practice looping through an object so I can create my list here.
And I'm going to say this list is the let's say, numbers one, two, three, four or five.
And then how do I iterate through this list, I simply say for.
Item in my list, Colon.
And note, when you had colon enter, you're actually going in the patient.
And so what's going to happen is I'm going to have this block of code execute for every single item
in that list.
So I could say go ahead and print out the item and note that this is technically just this little temporary
variable.
So if we save this, I should see one print it out.
Then two, then three, then four and five.
So we go ahead and run this.
And there you have it one, two, three, four or five.
Now what's interesting here is you can call this temporary variable whatever you want.
So you can call it for jelly in my list.
Go ahead and print jelly.
I just want to make it clear that it's totally up to you what you call that variable.
So if I run this code again, it still works just fine one, two, three, four or five.
The other thing I want to note here is that you don't necessarily need to use the actual item in the
list.
This is just saying for every item in this list, go ahead and do something.
So if I said for item in my list print.
Hello.
Then it's just going to print hello.
Five times.
This is just to give you the idea that you don't necessarily need to use each particular item in the
list.
Now what would be kind of a good use case for this?
You could say something like.
Prince, let's imagine that these were our sales numbers, so we can say for numb in sales just to give
you an idea of kind of how you would set up a for loop.
We have some sales here, one through a five four number in sales.
I can use again if string literal formatting and say sales, for example.
Sale of and then in curly braces person and um.
Save that change and then run this, you see example sale one two three four five.
OK, so that's the very basics of a for loop on a list.
Again, you just set up the adorable object and then say for whatever temporary variable you want in
that object, then you execute this block of code.
It doesn't have to be a one liner here later on.
This will be a more complicated.
Now, I should note, this works the same for tuples.
So if this were actually a tuples, so if I just change the square brackets for a tuple here, that's
also totally OK.
This will actually work just the same.
Now what happens if this is a dictionary?
So let's kind of set up a dictionary here.
I'm going to say.
Employees.
Is equal to.
And then let's say our CEO.
Is Cindy.
And let's say our CFO.
Is.
James.
OK, then what's going to happen is if you loop through a dictionary, it's going to loop through the
keys.
So for key in.
Employees.
Let's say print the key.
Just so we can explore what happens here.
So we should expect to see CEO and then followed by CFO.
OK.
If I actually want to grab the value here, Cindy and James and print that out, I could be a little
clever and say the following I could say.
Employees and then grabbed the item at that particular key, so it actually pass and see you back into
employees as it loops through these keys.
And then we see Cindy and James.
And so it would probably be better terminology here would be for position and employees.
And then let's actually use both of them.
I can print out using f string literals here.
I can say.
The.
Position, let's make sure we use curly braces.
The position.
Is held by and then will actually then look up the employee inside the curly braces employees.
At that position.
OK.
So again, let's break down what's happening here, I have this dictionary and I know when I loop through
dictionaries, I actually have the keys.
In this case, the keys in my dictionary employee stands for position so I can say the position it employs
and then print out this string literal and her position and then look up who is the employee at that
position.
Hopefully, it's beginning to kind of change some gears in your mind or get those spinning.
I can see the CEO's hold by saying the CFO is held by James.
The last thing I want to point out is there is something called tuple unpacking.
Remember, I said that it's really common to return back items in the forms of tuples and we can actually
see a list of tuples as a result often.
In fact, if you print out employees the items.
Open close parentheses there and run this, I can see an actual list of tuples.
Remember this the key value pairs.
So because this is such a common way of returning back items, a list of tuple pairs.
There's something called tuple and packing, and I want to show it to you now, just so it's not a complete
surprise when you see it later.
So I'm going to make a little artificial list and let's say my list.
Square brackets.
And then I'm going to set up some key value pairs.
So I'm going to say a b.
Comma.
And then say, see the.
So let's actually make one more here, just so it's really clear.
And let's make this last one.
One.
Let's just say one and two.
So what's happening here?
I have a list.
And in this list, there's three tuples.
So I'm just going to space them out a little bit.
So you can see clearly what's happening here.
I have the three tuples, A, B, C D and then one two.
Now look what happens if I were trying to.
Iterate through this list for item in my list.
Prince item, we should expect that to actually print each tuple and there's three pupils.
And there it is, a b c d one two.
But what if I actually wanted to get individual elements from those tuples, let's say only wanted a
C and one and only one of the first items.
This is where tuple and packing comes to play.
Instead of writing something like item, I can write a little tuple here and say key or let's say,
item one and then item two.
And then I can specify I only want a printout.
Item one.
So this is known as tuple unpacking.
Now, this only works if you know for a fact that in your list, you're getting objects returned back
as tuple pairs like this.
And this could extend if you have a third item in the tuple, you could say Item three, etc. But for
now, we're just going to say I don't want item to.
Go ahead and print this out, and we would expect to see a sea in one.
And there it is, a sea and one because I'm saying grab the tuple items, but only print out item one.
And technically speaking, you don't actually even need these parentheses here.
So you typically don't see tuple and packing like this where just says four and then tuple item one
comma tuple item two common to bottom three, etc. in my list.
Then you can do something here and you can actually then print them out on separate lines so you can
do whatever you want with these, so I can print out.
Item one, then print out Item two, so take them over here and just think, what would you expect the
output of this to be?
Well, it's going to print out a then B, then C then D than one than to remember because it's going
through each of these tuples.
One at a time.
Let's actually see the results here.
And there it is, it's a then B, then C, then the then one and two, because remember, it's still
going one two at a time.
It's just up to you.
Which items are printing out in which order?
OK, so that's the very basics.
A for loop.
Main thing to understand is, do you have the four key word?
Then you choose some temporary variable name in and then often we'll be dealing with this.
But you can do this for many different types of objects and then you execute a block of code each time.
And as you mentioned, technically, this block of code does not need to be related to the items in
the list.
I can still say something like prints.
Hello.
And this would still work, it just prints Halo three times, so I see.
Hello, hello.
Hello.
OK.
And that's it for four loops coming up next, we're going to talk about while loops.