WEBVTT

0
00:00.260 --> 00:06.140
Now, one of the things that you'll start to realize about the Python programming language, especially

1
00:06.140 --> 00:13.400
if you have experience of other programming languages, is that Python is extremely number friendly.

2
00:13.400 --> 00:18.230
It has a lot of built in functions to help us work with numbers.

3
00:18.230 --> 00:27.350
For example, if I had a class of students and I list each of their exam scores inside this giant list,

4
00:27.350 --> 00:32.980
and I wanted to work out, well, what is the total exam score.

5
00:32.980 --> 00:36.850
So what if I added each of these numbers up together,

6
00:36.850 --> 00:38.110
what would I get?

7
00:38.110 --> 00:46.570
Well, Python has something called the sum(), which allows us to put in any iterable data type, including

8
00:46.570 --> 00:47.200
lists.

9
00:47.200 --> 00:50.170
So I can put the student scores into here.

10
00:50.170 --> 00:56.580
And I can set this, let's say, to the total_exam_score.

11
00:57.090 --> 01:04.830
And then if I print this, you'll see that it has added up each of those numbers in the list, and it

12
01:04.830 --> 01:07.050
equals 2068.

13
01:07.140 --> 01:15.750
Now if I wanted to do this manually though, I could because at the end of the day, all that's happened

14
01:15.780 --> 01:22.940
is the Python team, when they developed the programming language they created each of these helpful

15
01:22.940 --> 01:29.810
functions so we could create some ourselves as well, given that we understand how for loops work.

16
01:29.810 --> 01:38.240
So we could say for score in student_scores, colon.

17
01:38.240 --> 01:47.530
And then in the indented block we could say, well, we need to add each of these scores to each other,

18
01:47.530 --> 01:47.800
right?

19
01:47.800 --> 01:50.740
And then we need some sort of variable to keep hold of it.

20
01:50.740 --> 01:56.440
So let's create a variable called sum and set it to start off as equal to 0.

21
01:56.440 --> 02:01.630
And then we could say, sum += score.

22
02:01.630 --> 02:07.540
And every time this for loop runs it's going to go to the next score and the next score,

23
02:07.540 --> 02:15.930
and each time that score is added to the previous value of sum, so then it accumulates in that variable.

24
02:15.960 --> 02:24.990
So now what we can do is go ahead and print the sum and go ahead and hit Run.

25
02:24.990 --> 02:29.700
And you can see that it's exactly the same number as before.

26
02:30.720 --> 02:38.630
So this is a way that we can use the primitive parts of Python.

27
02:38.630 --> 02:47.750
So the building blocks of Python, to build out functionality for various use cases, such as summarizing

28
02:47.750 --> 02:51.560
each of the numbers in a list and adding them together.

29
02:51.770 --> 02:54.470
So here is your challenge.

30
02:54.470 --> 03:00.860
You're going to do the same, but in your case you're going to try and replicate the max functionality.

31
03:00.860 --> 03:09.010
So this is a built-in function that allows us to pass in a list, for example, student_scores,

32
03:09.010 --> 03:17.080
and if we print it, you can see it will pick out the largest number in this list, which happens to

33
03:17.080 --> 03:18.730
be 199.

34
03:18.760 --> 03:27.360
Now your job is to replicate this functionality, but using what you have learned about for loops,

35
03:27.360 --> 03:33.900
about Lists, about Conditionals, and see if you can figure out how to achieve this.

36
03:34.050 --> 03:38.130
So pause the video now and give this challenge a go.

37
03:42.120 --> 03:50.880
So similar to previously, we will use a for loop to loop through each of the scores in the list of

38
03:50.880 --> 03:52.320
student_scores.

39
03:52.340 --> 03:58.250
and if you want to verify it as you go along while you're writing your code, it's always a good idea

40
03:58.250 --> 04:02.960
just to make sure that your code is doing what you expect it to do.

41
04:02.960 --> 04:05.780
So in this case, we're going through each of the scores.

42
04:05.780 --> 04:14.690
Now instead of printing it, we want to check it against the number which we want to set as maximum.

43
04:14.690 --> 04:24.010
So similar to sum we need a variable to hold all of the intermediate values until we reach the maximum.

44
04:24.010 --> 04:32.980
So let's create a variable called max_ score and let's just set it to equal 0.

45
04:33.190 --> 04:41.320
Now when we loop through each of the scores, we're going to compare the current score that the loop

46
04:41.320 --> 04:42.190
is going through.

47
04:42.190 --> 04:45.270
So the first time it will be looking at 150.

48
04:45.270 --> 04:53.580
And we're going to say, well, if the score that we're currently looking at, let's say 150 is greater

49
04:53.580 --> 05:01.890
than the max_score, well, in that case, we're going to set the max_score to this current score that

50
05:01.890 --> 05:03.150
we're looking at.

51
05:03.150 --> 05:08.070
And what this loop is going to do is it's going through each of these numbers.

52
05:08.070 --> 05:12.950
So let's go through the first three in order, just so that we can make this process a bit more clear

53
05:12.950 --> 05:13.790
in your mind.

54
05:13.820 --> 05:16.580
The first score is equal to 150.

55
05:16.580 --> 05:19.220
Is 150 greater than 0?

56
05:19.220 --> 05:20.360
Well, yes.

57
05:20.360 --> 05:23.750
So then this bit of code carries out,

58
05:23.750 --> 05:27.260
and now the max score is equal to 150.

59
05:27.260 --> 05:30.410
So for the next score it's 142.

60
05:30.440 --> 05:33.410
So is 142 greater than 150?

61
05:33.430 --> 05:34.210
No.

62
05:34.210 --> 05:36.550
So we're not going to do anything at all.

63
05:36.550 --> 05:41.770
You could, of course, have an else statement that just prints something, but in this case it's unnecessary.

64
05:41.770 --> 05:48.940
We only need to check for one thing, and that one thing is each time we loop through each of the scores,

65
05:48.940 --> 05:52.660
is it greater than the previous maximum?

66
05:52.750 --> 05:58.150
And now if I go ahead and exit the indentation levels and go to the very start.

67
05:58.150 --> 06:07.770
So I'm outside of the for loop and I print out the max score, you'll see that it will give me the same

68
06:07.770 --> 06:12.960
result as using the max function, which is 199.

69
06:13.110 --> 06:20.160
So if this code doesn't make any sense to you, I recommend going back to the previous lesson and revising

70
06:20.160 --> 06:22.200
what we learned about for loops.

71
06:22.230 --> 06:28.220
Alternatively, maybe you need to do some revision on how if statements and conditionals work, but

72
06:28.220 --> 06:33.740
either way, make sure that all of this code makes perfect sense to you, and you can write it from

73
06:33.740 --> 06:39.380
scratch without having to look at the answer code, because it's really, really important.

74
06:39.380 --> 06:46.130
As we progress that, I'm going to assume that all of this makes sense to you so that we can build upon

75
06:46.130 --> 06:47.180
our knowledge.

76
06:47.180 --> 06:50.360
And then once you're ready, I'll see you over in the next lesson.