- [Speaker] In this video we're going to take a look
at a script example that processes a dictionary
of student grades.
Now the script we'll be looking at is fig06_01.py
And I'm just going to go ahead and run it here
so you can see the results of the script.
On a student by student basis
we're going to calculate that student's average
for the class.
And we'll format that
with two digits to the right of the decimal point.
And then for the entire class
we're also going to calculate the class average.
So now that we've seen the output of the script
let's jump over to a text editor here
and take a look at the script itself.
Now once again we always start our scripts
with a comment showing you the file names
so that you can find them in the files
that are associated with these videos.
And as always you should start your script
with a doc string that specifies the purpose of the script.
And depending on how complex your script is
that doc string may be more complex as well.
Now the key part of this example
is of course our dictionary.
The great book.
And because it's a little bit more complex dictionary
I put it across multiple lines of code here
in the script.
So it's not uncommon to see people line up the curly braces
like you see here.
And it's not uncommon for them
to put individual key value pairs one per line
for readability in their code.
In this case we have keys, which are strings
the students names
and we have values which are list objects
which in turn contain other values.
And those list objects of course
are mutable as well
although we won't modify them in this example.
So on a student by student basis
what we're going to do down below in a loop
is pick off one key value pair at a time.
Sum up the grades in the list.
Display the students name and average for those grades.
And then keep track of the total
of all the grades we've seen so far
and the number of grades we've seen so far
so that when the loop is done
we can calculate the class average.
So along those lines we've created a couple of variables
both initialized to zero here.
Where we'll keep track of the total of all the grades
and account of all the grades.
And here is the for loop
that's going to process that dictionary up above.
So of course we've already seen previously
gradebook.items is going
to give us back something that we can iterate over
that gives us two bulls containing
the keys and the values.
One key value pair at a time.
We're going to unpack that two bull
into name for the key
and grades for the list of grades
that's the associated value.
Once we have that we can sum up those grades
simply by handing the list to the built in sum function.
Again that's one of those
python functional style reductions
that's built into the language.
You give it a sequence of values
it gives you back the sum of those values.
We store that here in total.
We'll use that down below as well a couple of times.
In this print statement here
we have a formatted string that consists
of the name of the student
and another place holder over here
where we'll display the average
and notice the calculation being performed
which is the total
divided by the length of that grades array.
So that'll give us our class average
and we decided we'd like to format that
as a floating point number
with two digits to the right of the decimal point.
Don't forget that the division operator
always gives you a floating point result.
Now once we've displayed the information
for the current student
we then take that students total and add it
to the all grades total.
And we take the length of the grades array
and add that to the all grades count as well.
And as you can see down here
after the loop completes we have another print statement
that's going to output the classes average is
and notice we have a single quote character in the strings.
So we delineated this string with double quote characters.
And the place holder performs another one
of those average calculations.
This time with all grades total and all grades count.
And formatted once again with two digits
to the right of the decimal point.