Let's experiment with the code a little bit now so that you get a better understanding of certain aspects
of Python.
I want to focus here on the full loop.
The for loop iterates over the to do lists and accesses the index and the item in each iteration.
It constructs a string and it prints out that string in each iteration.
Now, if I press enter here and then I press backspace to go one indentation level back at this point.
Same level with for loop.
And if I say print index comma item, what do you think we're going to get?
And also I would like to print out.
Hello.
Before that.
So let's see.
I'm going to run this ad A to do clean the keyboard because it has to be clean sometimes.
Add the new one.
The monitor as well.
And a third one add.
Buy grocery press, enter press show and let's look at the output.
So perhaps you guessed what we're going to get.
So we get to clean the keyboards, clean the monitor, buy grocery.
All these three are produced by the print row line in here, this one.
And then we have this third line here to buy grocery.
So that is produced obviously by this line we get or print out.
And then we get to.
Because index is too and item prints out by grocery.
So why did I do this experiment?
Well, this was to show you that once the loop ends, which is the case when you indent the loop, as
we did here, then these are still variables.
Index and items are still variables.
And index has as a value of the last index in the for loop.
And the same goes for item item.
The variable will also have as value the last item of the whole of the list.
So the loop went over and over again.
Index was for zero.
In the second iteration was one, in the third was two.
And so once there were no more items in the loop, we get index assigns the final number, the final
value of two and the same goes for item.
So I hope that gives you some perspective to understand the for loop better.
The index particularly could be used if you want to calculate the length of the list.
So basically you would say length is
index plus one, so that would print out the length A's and index plus one, which would be two +13.
So we would get the length is three.
Or you could also use a knife string if you like.
So f length s and you'd have to put this inside curly brackets and close the string with a quote.
So let me run this and to do one add to the to show and we get length is to.
However, getting the length like this is quite a workaround.
I would even call it a hack because calling it a workaround is giving it too much credit.
The actual way to get the length of lists is by using another more appropriate function, and that is
the length function.
It's a function as argument.
It gets the list and the length function doesn't even need a full loop.
So if you wanted to show the length, all you'd have to do is like that.
Run at 2 to 1 at two, the two show and you get two.
So that is the output of the length function.
So anyway, we don't need the length in this app, so I'll just delete everything and keep the loop
as it was before.
An experiment now over the enumerate method if you open the python console.
And you do for I item or I j whatever you like.
So I will s above the index.
J is about the item itself.
It doesn't matter what variable names you use in enumerate.
Hello.
Great.
I and J execute.
And this is what you get.
So strings can also be used with the enumerate function because strings also are sequences of items
and each item is itself a string.
So each character of the string is a string itself.
That's why we get here.
We get zero in the first iteration.
H So G prints out.
H, Then we get one E and so on.
So that's something to keep in mind.
Now you know that functions return and output, right?
And this here is a function.
So what is the output of this function anyway?
Or let's try a list as we did before.
So let's say A, B, and C.
And if we check a, we get this.
So that's a bit confusing.
The reason we get this is that the enumerate function returns an enumerate object.
So just like we have list object types, we have strings object types, we have integers for numbers.
We also have this strange object here.
The difference here is that Python doesn't know how to display this object.
The object actually contains delta, but you cannot see them.
So it is not designed.
The object itself is not designed to display those data in the command line.
But if you use a popular type such as the list and you place that object in a list, so to basically
to convert it into a list, you press enter and you get this.
So now we have a representation of that enumerate object.
This is actually the internal construction of that object.
But now we can we can visualize it using a list, we can see it.
So it's a list made of tuples.
That is the first of all, the second tuple and the third tuple.
Each tuple contains the index and the item itself of the original list.
The index of B and the B itself.
Index of C and the C itself.
So actually when we do four or four item, whatever you call them in enumerate and you have that list
here, ABC, what you're actually doing is you're doing this for item in.
This list.
So enumerate is a list of tuples and you say print I item press enter, enter and we get this.
So actually I is getting the first item of the first simple and then item is getting the second item
of that first sample.
And we print them out here and we do the same for the other tuples.
So that's about the innumerate object.
Of course, you can do the same with a string.
So hello.
And you can convert it into a list.
Sorry, I did the typo here.
And you may rate.
So be is an enumerate object.
We can convert it into a list.
And now we see that same construction.
However, if you are thinking of converting of of be a numerous object into a string, you'll get a
bit disappointed because all this will do is just convert that representation to a string.
You see, it has quotes now.
In other words, STR doesn't do what you intend to do.
It's actually useless here.
It is useless because the anatomy of an enumerate object would not be able to represent using a string
because a string is just made of character of strings and at least can be made as you see, can be made
of other tuples and other lists or other items, strings, numbers.
That's why it is possible to convert enumerate into a list.
And that was a series of experiments on the for loop and the enumerate object.
Thanks a lot.