- [Instructor] In this self check exercise,
we have a couple of different problems
that we'd like you to attack.
First, we want you to create a tuple called high-low
that represents a day of the week, along with the high
and the low temperature as integers for that day.
And we'd like you to display its string representation
and then perform these two tasks using that high-low tuple.
First, we want you to access each of the individual values
in that tuple via index numbers
and display the element values.
And then, we want you to unpack that tuple
into just the variables Day and High,
and show what happens and explain why that happens, as well.
In the second exercise, we want you to create a list
of names containing three name strings, and then enumerate
those values using the enumerate function so that you
can display each element's index and corresponding value.
So go ahead and pause this video,
take a shot at the two exercises you see here on the screen,
and then come back to see the answers.
Okay, let's go ahead and reveal the answer here
for this first exercise.
So again, initially we're going to create the tuple
called high-low, and we'll stick into that a name of a day
and a couple of integer values representing the high
and low temperatures, respectively.
So let me go ahead and execute that.
And to display its string representation, we can simply
evaluate the variable.
So here is that string representation.
Now in Part A up above, we said we wanted you to use
the square brackets operator and index numbers
to access each of the three pieces of information.
So here, we're going to produce a formatted string,
in which the first placeholder is going to access
element zero of the tuple, which is the day.
The second placeholder is going to access element one
of the tuple, which is the high temperature.
And the last is going to access the low temperature
within that tuple.
So let's just display that and we can see that, indeed,
we were able to pick off each of those elements.
Now, you may recall from the preceding couple of videos
that, when you're unpacking a tuple or any other sequence,
the left side of the assignment must have the appropriate
number of variables to match up with the total number
of elements within that sequence.
So in this piece of the exercise, we asked you to unpack
a three-element tuple into a set of two variables,
Day and High.
And if I execute that, you can see here that we wind
up with a value error: too many values to unpack.
So Python is going to check the length of the sequence
on the right side,
make sure it matches up with the number of variables
on the left side, and in this case it does not.
There are too few variables on the left side,
and thus, too many values to stuff into those variables.
And therefore, we get an error in this particular case.
In this last, minimize things just the answer
saying that we have to have enough variables
to unpack the corresponding elements.
Now in this second exercise, we wanted you to create a list
of names with three strings and then use a for loop
and the enumerate function to iterate over those values
and display their indices and values.
So here we have a string--
a list, excuse me, containing three string names.
And let's go ahead and iterate over that
with the enumerate function.
So Names is the list to enumerate over.
We're going to wind up with a sequence of three tuples:
zero in Amanda, one in Sam, two in David.
We're going to unpack those tuples into I for the index
number and Name for the name.
And we'll simply then display those values line by line
after the cell here.