Now, one of the things that you'll start to realize about the Python programming language, especially
if you have experience of other programming languages, is that Python is extremely number friendly.
It has a lot of built in functions to help us work with numbers.
For example, if I had a class of students and I list each of their exam scores inside this giant list,
and I wanted to work out, well, what is the total exam score.
So what if I added each of these numbers up together,
what would I get?
Well, Python has something called the sum(), which allows us to put in any iterable data type, including
lists.
So I can put the student scores into here.
And I can set this, let's say, to the total_exam_score.
And then if I print this, you'll see that it has added up each of those numbers in the list, and it
equals 2068.
Now if I wanted to do this manually though, I could because at the end of the day, all that's happened
is the Python team, when they developed the programming language they created each of these helpful
functions so we could create some ourselves as well, given that we understand how for loops work.
So we could say for score in student_scores, colon.
And then in the indented block we could say, well, we need to add each of these scores to each other,
right?
And then we need some sort of variable to keep hold of it.
So let's create a variable called sum and set it to start off as equal to 0.
And then we could say, sum += score.
And every time this for loop runs it's going to go to the next score and the next score,
and each time that score is added to the previous value of sum, so then it accumulates in that variable.
So now what we can do is go ahead and print the sum and go ahead and hit Run.
And you can see that it's exactly the same number as before.
So this is a way that we can use the primitive parts of Python.
So the building blocks of Python, to build out functionality for various use cases, such as summarizing
each of the numbers in a list and adding them together.
So here is your challenge.
You're going to do the same, but in your case you're going to try and replicate the max functionality.
So this is a built-in function that allows us to pass in a list, for example, student_scores,
and if we print it, you can see it will pick out the largest number in this list, which happens to
be 199.
Now your job is to replicate this functionality, but using what you have learned about for loops,
about Lists, about Conditionals, and see if you can figure out how to achieve this.
So pause the video now and give this challenge a go.
So similar to previously, we will use a for loop to loop through each of the scores in the list of
student_scores.
and if you want to verify it as you go along while you're writing your code, it's always a good idea
just to make sure that your code is doing what you expect it to do.
So in this case, we're going through each of the scores.
Now instead of printing it, we want to check it against the number which we want to set as maximum.
So similar to sum we need a variable to hold all of the intermediate values until we reach the maximum.
So let's create a variable called max_ score and let's just set it to equal 0.
Now when we loop through each of the scores, we're going to compare the current score that the loop
is going through.
So the first time it will be looking at 150.
And we're going to say, well, if the score that we're currently looking at, let's say 150 is greater
than the max_score, well, in that case, we're going to set the max_score to this current score that
we're looking at.
And what this loop is going to do is it's going through each of these numbers.
So let's go through the first three in order, just so that we can make this process a bit more clear
in your mind.
The first score is equal to 150.
Is 150 greater than 0?
Well, yes.
So then this bit of code carries out,
and now the max score is equal to 150.
So for the next score it's 142.
So is 142 greater than 150?
No.
So we're not going to do anything at all.
You could, of course, have an else statement that just prints something, but in this case it's unnecessary.
We only need to check for one thing, and that one thing is each time we loop through each of the scores,
is it greater than the previous maximum?
And now if I go ahead and exit the indentation levels and go to the very start.
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
result as using the max function, which is 199.
So if this code doesn't make any sense to you, I recommend going back to the previous lesson and revising
what we learned about for loops.
Alternatively, maybe you need to do some revision on how if statements and conditionals work, but
either way, make sure that all of this code makes perfect sense to you, and you can write it from
scratch without having to look at the answer code, because it's really, really important.
As we progress that, I'm going to assume that all of this makes sense to you so that we can build upon
our knowledge.
And then once you're ready, I'll see you over in the next lesson.