In this video we'll take another look
at that enumerate function that
we just demonstrated in the preceding video.
This time in the context of
the script fig05_01.py.
Let's go ahead and run that and see
what the results look like.
So basically what we're going to do
is we're going to take a list of values,
which you see in the middle column here,
and we're going to use the enumerate function
to help us produce the output that you see here
consisting of the index number of each value,
the actual value, and then a bar of asterisks
representing that value.
So basically a primitive bar chart.
And we're going to show you later on in this lesson
much nicer looking visualizations
that produce bar charts automatically for you.
So we'll take a look at that in
the intro to data science section at the end of this lesson.
So let's switch over to the script
that actually produces the output you just saw there.
Again anytime we create a script
we're going to tell you the name of the file
in a comment at the beginning so you can
find that script in the corresponding lesson's examples.
And all scripts should start with a doc string
that states the purpose of that script.
And since most of our scripts are very straightforward,
we typically just have a one-line doc string;
however, it's not uncommon for a complex script
or a library to have a very detailed documentation string.
Now we're going to be working in this example
with the list called numbers, which
just contains five values.
And we have a couple of print statements
to get us started here.
Let's take a look at the second one for a moment
because in this f string we have a placeholder.
And I just want you to reemphasize
the fact that we have a single quoted f string.
And because of the fact that we're outputting
a string value in this placeholder
we're wrapping that string, that nested string,
in double quote characters that way we don't get an error
because of nesting single quote characters
within single quote characters.
So that's why we have double quote characters here.
And you may recall that this is not an emoji,
but rather a format specifier indicating
that we want to write a line, the string value,
in a field of eight characters.
So what's going to happen here
is we'll display the literal characters' index,
which is five character positions,
and then in the next eight character positions
we'll display these five characters
preceded by three spaces, and then
we'll have a couple of spaces and the word Bar.
And that will help us produce the three column heads,
and we'll use some similar formatting
down here in the for loop to produce the three columns.
Now let's talk about this for loop.
So we just showed you that the enumerate function
can produce a sequence of tuples consisting of each value
in the sequence that you give it as an argument,
in this case the list numbers and
the index for each of those values.
And again the order within those tuples
will be the index followed by the value.
So because we're getting tuples from the enumerate function
we are able to unpack those tuples
into the appropriate number of variables.
In the case of enumerate that will always be two.
And so we have the index and the value,
and we'd like to format the output.
We're going to display the index
right aligned in a field of five,
which will right align it under the word index,
five characters long.
Then in a field of eight, right aligned we're
going to display the value of the variable value.
Then we have a couple of spaces here,
same number as up above.
And underneath the word bar, we are going to
display the value of this highlighted expression.
Now we haven't done this previously
and this is another one of those
cool little python features.
When you multiply a string by an integer
it repeats the string that number of times.
So what we want in this example
is a bar of asterisks that matches up
with the value of each individual list element.
So if I say multiply the string asterisk by the value 19,
what I'm going to get is 19 consecutive asterisks
as a string that is the value of
this highlighted expression.
Or if I multiply the asterisk string by three
I'll get a string of three asterisks, etc. etc.
So anytime you multiply a sequence by an integer,
you repeat that sequence the specified number of times.
And this little fore loop produced,
going back over here, these rows of the output.