1 00:00:00,550 --> 00:00:02,540 - [Speaker] In this video we're going to take a look 2 00:00:02,540 --> 00:00:06,030 at a script example that processes a dictionary 3 00:00:06,030 --> 00:00:07,860 of student grades. 4 00:00:07,860 --> 00:00:12,860 Now the script we'll be looking at is fig06_01.py 5 00:00:13,160 --> 00:00:15,220 And I'm just going to go ahead and run it here 6 00:00:15,220 --> 00:00:17,520 so you can see the results of the script. 7 00:00:17,520 --> 00:00:19,270 On a student by student basis 8 00:00:19,270 --> 00:00:21,990 we're going to calculate that student's average 9 00:00:21,990 --> 00:00:23,050 for the class. 10 00:00:23,050 --> 00:00:24,110 And we'll format that 11 00:00:24,110 --> 00:00:26,510 with two digits to the right of the decimal point. 12 00:00:26,510 --> 00:00:28,250 And then for the entire class 13 00:00:28,250 --> 00:00:31,890 we're also going to calculate the class average. 14 00:00:31,890 --> 00:00:34,590 So now that we've seen the output of the script 15 00:00:34,590 --> 00:00:37,130 let's jump over to a text editor here 16 00:00:37,130 --> 00:00:39,590 and take a look at the script itself. 17 00:00:39,590 --> 00:00:42,050 Now once again we always start our scripts 18 00:00:42,050 --> 00:00:44,330 with a comment showing you the file names 19 00:00:44,330 --> 00:00:46,670 so that you can find them in the files 20 00:00:46,670 --> 00:00:48,600 that are associated with these videos. 21 00:00:48,600 --> 00:00:51,760 And as always you should start your script 22 00:00:51,760 --> 00:00:56,380 with a doc string that specifies the purpose of the script. 23 00:00:56,380 --> 00:00:58,920 And depending on how complex your script is 24 00:00:58,920 --> 00:01:02,260 that doc string may be more complex as well. 25 00:01:02,260 --> 00:01:04,920 Now the key part of this example 26 00:01:04,920 --> 00:01:06,480 is of course our dictionary. 27 00:01:06,480 --> 00:01:07,340 The great book. 28 00:01:07,340 --> 00:01:10,410 And because it's a little bit more complex dictionary 29 00:01:10,410 --> 00:01:13,120 I put it across multiple lines of code here 30 00:01:13,120 --> 00:01:14,138 in the script. 31 00:01:14,138 --> 00:01:19,138 So it's not uncommon to see people line up the curly braces 32 00:01:19,190 --> 00:01:20,150 like you see here. 33 00:01:20,150 --> 00:01:21,890 And it's not uncommon for them 34 00:01:21,890 --> 00:01:25,890 to put individual key value pairs one per line 35 00:01:25,890 --> 00:01:28,230 for readability in their code. 36 00:01:28,230 --> 00:01:31,040 In this case we have keys, which are strings 37 00:01:31,040 --> 00:01:32,400 the students names 38 00:01:32,400 --> 00:01:35,830 and we have values which are list objects 39 00:01:35,830 --> 00:01:38,380 which in turn contain other values. 40 00:01:38,380 --> 00:01:40,220 And those list objects of course 41 00:01:40,220 --> 00:01:42,040 are mutable as well 42 00:01:42,040 --> 00:01:45,110 although we won't modify them in this example. 43 00:01:45,110 --> 00:01:47,370 So on a student by student basis 44 00:01:47,370 --> 00:01:49,680 what we're going to do down below in a loop 45 00:01:49,680 --> 00:01:52,920 is pick off one key value pair at a time. 46 00:01:52,920 --> 00:01:55,500 Sum up the grades in the list. 47 00:01:55,500 --> 00:02:00,500 Display the students name and average for those grades. 48 00:02:00,810 --> 00:02:03,690 And then keep track of the total 49 00:02:03,690 --> 00:02:05,730 of all the grades we've seen so far 50 00:02:05,730 --> 00:02:07,960 and the number of grades we've seen so far 51 00:02:07,960 --> 00:02:09,730 so that when the loop is done 52 00:02:09,730 --> 00:02:11,990 we can calculate the class average. 53 00:02:11,990 --> 00:02:15,960 So along those lines we've created a couple of variables 54 00:02:15,960 --> 00:02:18,230 both initialized to zero here. 55 00:02:18,230 --> 00:02:20,990 Where we'll keep track of the total of all the grades 56 00:02:20,990 --> 00:02:22,870 and account of all the grades. 57 00:02:22,870 --> 00:02:24,240 And here is the for loop 58 00:02:24,240 --> 00:02:27,350 that's going to process that dictionary up above. 59 00:02:27,350 --> 00:02:29,940 So of course we've already seen previously 60 00:02:29,940 --> 00:02:31,810 gradebook.items is going 61 00:02:31,810 --> 00:02:35,070 to give us back something that we can iterate over 62 00:02:35,070 --> 00:02:37,310 that gives us two bulls containing 63 00:02:37,310 --> 00:02:39,055 the keys and the values. 64 00:02:39,055 --> 00:02:41,360 One key value pair at a time. 65 00:02:41,360 --> 00:02:43,690 We're going to unpack that two bull 66 00:02:43,690 --> 00:02:45,670 into name for the key 67 00:02:45,670 --> 00:02:49,060 and grades for the list of grades 68 00:02:49,060 --> 00:02:51,480 that's the associated value. 69 00:02:51,480 --> 00:02:54,660 Once we have that we can sum up those grades 70 00:02:54,660 --> 00:02:58,500 simply by handing the list to the built in sum function. 71 00:02:58,500 --> 00:03:00,100 Again that's one of those 72 00:03:00,100 --> 00:03:02,690 python functional style reductions 73 00:03:02,690 --> 00:03:04,680 that's built into the language. 74 00:03:04,680 --> 00:03:06,620 You give it a sequence of values 75 00:03:06,620 --> 00:03:09,480 it gives you back the sum of those values. 76 00:03:09,480 --> 00:03:12,030 We store that here in total. 77 00:03:12,030 --> 00:03:14,970 We'll use that down below as well a couple of times. 78 00:03:14,970 --> 00:03:17,510 In this print statement here 79 00:03:17,510 --> 00:03:20,480 we have a formatted string that consists 80 00:03:20,480 --> 00:03:22,700 of the name of the student 81 00:03:22,700 --> 00:03:24,840 and another place holder over here 82 00:03:24,840 --> 00:03:26,550 where we'll display the average 83 00:03:26,550 --> 00:03:28,971 and notice the calculation being performed 84 00:03:28,971 --> 00:03:30,400 which is the total 85 00:03:30,400 --> 00:03:33,480 divided by the length of that grades array. 86 00:03:33,480 --> 00:03:35,270 So that'll give us our class average 87 00:03:35,270 --> 00:03:37,390 and we decided we'd like to format that 88 00:03:37,390 --> 00:03:38,590 as a floating point number 89 00:03:38,590 --> 00:03:41,600 with two digits to the right of the decimal point. 90 00:03:41,600 --> 00:03:43,660 Don't forget that the division operator 91 00:03:43,660 --> 00:03:46,553 always gives you a floating point result. 92 00:03:47,600 --> 00:03:49,350 Now once we've displayed the information 93 00:03:49,350 --> 00:03:50,420 for the current student 94 00:03:50,420 --> 00:03:52,860 we then take that students total and add it 95 00:03:52,860 --> 00:03:54,750 to the all grades total. 96 00:03:54,750 --> 00:03:57,330 And we take the length of the grades array 97 00:03:57,330 --> 00:04:00,130 and add that to the all grades count as well. 98 00:04:00,130 --> 00:04:01,840 And as you can see down here 99 00:04:01,840 --> 00:04:05,710 after the loop completes we have another print statement 100 00:04:05,710 --> 00:04:08,800 that's going to output the classes average is 101 00:04:08,800 --> 00:04:11,880 and notice we have a single quote character in the strings. 102 00:04:11,880 --> 00:04:15,660 So we delineated this string with double quote characters. 103 00:04:15,660 --> 00:04:18,190 And the place holder performs another one 104 00:04:18,190 --> 00:04:19,840 of those average calculations. 105 00:04:19,840 --> 00:04:22,950 This time with all grades total and all grades count. 106 00:04:22,950 --> 00:04:25,870 And formatted once again with two digits 107 00:04:25,870 --> 00:04:27,570 to the right of the decimal point.