In previous lectures, you learned how to create and use sequencers and dictionaries.
These are collections of multiple items and this is actually showing you how you can look through those
collections so that you can process each of them containing items individually.
So in the Python three documentation, there is Section five point six that talks about looping techniques
in some of those who going to show you in this lecture.
But you can go here for more if you're curious.
All right.
Let's go into our examples.
Let's start with Rangers'.
A couple of years ago, you learned that in order to create a range, you can use the range function.
In this case, I'm using it with three parameters.
So I'm going to create a range of numbers from zero to 50 50 non-inclusive step five account.
So now to print out the individual items in this range, I can just print out the range itself.
Show what happens if I do that.
So if I just say print range of zero to 50, that's the name of the variable that I'm using here.
What I'm going to get back is just the object.
In order to print out the individual items in the range, you need to use a loop and to do that, going
to create a loop using the full keyword, I'm going to give it a variable so that each time we go through
the loop, the current item out of the loop will be stored in this arbitrary variable.
So this name here is a variable.
I choose to hold that current item that we are browsing through the range in this case.
So this is going to be in this range zero to 50 step files.
And this is going to be a block that I'm creating here.
And that's why I need to finish with the column that dots Sony realizes that I'm creating a block because
I've just finished with the column.
So here I just want to print out what's inside.
So the value stored in a number variable, enter and then enter again to get out of this block and the
interpreter will just immediately execute my folder and print out the individual.
Items out of this sequence.
That's how you can create a loop from a range.
Let's do something similar now, but this time for a list.
So I've got a list of letters, which is this one here.
We saw this for the first time a couple of years ago.
So we got a list of letters to iterate through it in a loop.
Use the exact same methodology.
The only difference is that instead of a list created by the range function, we've got a list that
we have explicitly created that contains letters, so say for later in the list of letters.
Print out.
Enter and enter again, and there are my three letters to exactly the same methodology for both range
and lists.
What about tuples by name?
You can probably guess that when we are looping through a tuple, the method we follow is exactly the
same as if we are looping through a list.
So we're going to use it for keyword.
We're going to go for just the generic word item since this list contains both numbers and letters.
I'm just going to go for item and then the name of the tuple in this case.
The.
Interesting, so.
I made a mistake here, did not include in and I just show you what happened could completely normally
works unless there is a problem with the syntax that you're using.
So I will go to you in here.
And I went to use Teppo and then the code completion.
But there's and nothing is happening.
That's because the syntax broken.
But if my syntax is correct, then code completion will work correctly as well.
So is that a gotcha?
All right.
So then you can just print the item.
And we have to loop goes through each item of the tuple and print it out.
Now, the last thing that I want to show you, of course, is a dictionary.
We can use dictionary in a slightly modified way because remember, the dictionary contains keys and
values.
So we can't just use a single variable, temporary variable here because we've got two things that we
want to capture.
We've got both the key and each value.
So to make this work.
Again, we start with the four key word, but then we say key value.
And of course, these are arbitrary names.
You can go for whichever name you want for the key and the value and the rest is the same.
So we go for E.S.P three to six dictionary.
And then for the print, we've got two things to print out now, shall we say, key comma value, and
that's a mistake.
What happened?
Yes, it did make a mistake.
So there's one more thing that I need to do and take a step back.
So if I tried to print out a speech, 30 to spec dictionary, what you'll get back is a dictionary like
this.
But what we want to do instead is to get the items out of this dictionary like this.
So now we have a dictionary items object, which looks like this.
And therefore, in my loop, I want to use the items version of the dictionary.
So let's try this again.
I'm going to copy you save a bit of time and get the items for my loop and then print those items out
sophisticated and then devalue it since and now we've got to manufacture a key and it's value, that
type key and each value and so on.
And that's how you can loop through sequences and dictionaries and put in.