WEBVTT 1 00:00:00.260 --> 00:00:01.330 Hi and welcome back. 2 00:00:01.330 --> 00:00:03.310 In this video I just wanted to introduce to you 3 00:00:03.310 --> 00:00:07.230 another function called sum that along with length 4 00:00:07.230 --> 00:00:10.383 we can use to calculate an average of a list. 5 00:00:11.560 --> 00:00:14.370 Imagine you've got your grades and you have 6 00:00:14.370 --> 00:00:18.090 something like 80, 75, 90, and 100. 7 00:00:18.090 --> 00:00:21.773 And you want to calculate the average of these grades. 8 00:00:22.630 --> 00:00:25.540 Something you could do is you could sum up 9 00:00:25.540 --> 00:00:27.680 all of the grades by doing something like 10 00:00:27.680 --> 00:00:30.070 total equal sum of grades. 11 00:00:30.070 --> 00:00:33.190 And this is the new function I wanted to tell you about. 12 00:00:33.190 --> 00:00:37.150 You've got the sum function and inside the brackets 13 00:00:37.150 --> 00:00:39.930 you put in the list or tuple or set 14 00:00:39.930 --> 00:00:41.430 that you want to add together. 15 00:00:42.420 --> 00:00:44.450 And that is going to add all the values together 16 00:00:44.450 --> 00:00:46.890 and give you the grand total. 17 00:00:46.890 --> 00:00:50.760 You've also got length which is len of grades. 18 00:00:50.760 --> 00:00:52.610 And we've seen this before but I'll forgive you 19 00:00:52.610 --> 00:00:54.470 if you've forgotten about this. 20 00:00:54.470 --> 00:00:57.680 Len gives you the length of the collection. 21 00:00:57.680 --> 00:01:01.760 So in this case, this list is going to give us four. 22 00:01:01.760 --> 00:01:03.570 You can then calculate the average by doing 23 00:01:03.570 --> 00:01:06.263 total divided by length. 24 00:01:09.160 --> 00:01:12.640 Just like that, you get 86.25. 25 00:01:12.640 --> 00:01:16.120 Now this is a perfect opportunity for a quick question. 26 00:01:16.120 --> 00:01:20.560 Which of these data structures is less ideal for grades? 27 00:01:20.560 --> 00:01:22.660 You've got a list of grades. 28 00:01:22.660 --> 00:01:24.630 You've got a tuple of grades. 29 00:01:24.630 --> 00:01:26.823 And you've got a set of grades. 30 00:01:28.010 --> 00:01:31.860 The tuple of grades may seem like a bad choice 31 00:01:31.860 --> 00:01:35.440 because you cannot add new grades over time. 32 00:01:35.440 --> 00:01:38.150 And indeed, if you are currently in the middle 33 00:01:38.150 --> 00:01:40.740 of your programme, say, and you want to be adding more grades 34 00:01:40.740 --> 00:01:43.790 as students takes exams or the user gives you 35 00:01:43.790 --> 00:01:45.830 some more data or something like that. 36 00:01:45.830 --> 00:01:47.680 Then the tuple might be a bad choice. 37 00:01:48.830 --> 00:01:51.870 But the set is actually the worst choice here 38 00:01:51.870 --> 00:01:54.920 because if you have a set of grades, 39 00:01:54.920 --> 00:01:58.870 you cannot have another 100 percent on another test, say. 40 00:01:58.870 --> 00:02:02.260 Because if you put 100 and a 100 in a set, 41 00:02:02.260 --> 00:02:03.720 one of them will be removed 42 00:02:03.720 --> 00:02:06.260 because you cannot have duplicates. 43 00:02:06.260 --> 00:02:08.950 So, in this case, I would say that the set is 44 00:02:08.950 --> 00:02:12.230 the worst of these collections for the use case, 45 00:02:12.230 --> 00:02:14.360 storing student grades. 46 00:02:14.360 --> 00:02:16.650 The tuple might be a bad idea depending on 47 00:02:16.650 --> 00:02:18.360 what you want to do in your programme. 48 00:02:18.360 --> 00:02:20.330 And the list is the safest of them all. 49 00:02:20.330 --> 00:02:24.080 It allows you total flexibility while giving you 50 00:02:24.080 --> 00:02:25.363 all the power you need. 51 00:02:26.300 --> 00:02:30.100 Again, remember if you do not have to add more things 52 00:02:30.100 --> 00:02:33.270 to your collection, I would use a tuple. 53 00:02:33.270 --> 00:02:35.470 Otherwise I would use the list. 54 00:02:35.470 --> 00:02:37.590 And finally, I would only use sets for when 55 00:02:37.590 --> 00:02:40.070 you want to compare sets together. 56 00:02:40.070 --> 00:02:41.400 Thanks for joining me in this video. 57 00:02:41.400 --> 00:02:42.450 I hope you've learned something. 58 00:02:42.450 --> 00:02:44.100 And I'll see you in the next one.