In this exercise
you're going to write a program that calculates the sum
of all the even numbers from 1, to a particular number.
So in the Input we're going to put in a particular number
and then the code is to go through starting from 1,
finding all the even numbers between 1
and the Input number, and then adding them all together.
In order to make sure
that the code doesn't take too long to run,
we are making sure the inputs don't go beyond 1,000
because you might be sitting here for ages.
So make sure that you put in the input as a small number
like less than a hundred, or at least less than a thousand.
The longer it is, the longer your code is going to take to run.
But make sure that your code achieves its functionality.
Check the Description pane,
take a look at the Example Inputs
and expected Example Outputs,
and see if you can figure out how to do this
using any Python function that you can come across
but also using all of the basic skills
in Python programming that you already have.
And as a hint, you might want to look up the range() function
to help you complete this exercise.
Now, there are a number of ways of achieving this
and I've included two in the complete solution
but let's go through the first one.
We start off with a variable called even_sum,
and we set it equal to 0.
This is going to be our accumulator.
We're going to use this variable to add to it
and be able to get the final sum.
Now, how are we going to accumulate into it?
Well, we're going to use a for loop
to loop through each of the numbers in the range,
starting from 2, which is the first even number after 1.
So remember, we're looking for all the even numbers
between 1 and our target number.
And the range() function takes a starting point,
an ending point,
and an increment, similar to our traditional for loop,
which you might have come across
if you've learned the C programming language
or other older programming languages.
Essentially, we can provide three inputs
through the range() function start, end,
and the increment that we're going to go by each time.
Now you can read upon the Python range() function
but essentially what we're doing here
is we're creating a for loop,
and we're creating a range,
starts from 2, ends at the target + 1.
And the reason why it ends at target + 1
is because the range will stop just before the end.
So if your target was say, 100,
then range will give you a range of numbers
starting from 2 up until 99.
So we have to modify that by adding 1
if we want to include the target number in this range.
So 2 comma and then target + 1,
and then the final number is how much we want to add
to the starting number each time.
So what is the gap between each number.
Because we want all the even numbers,
each time we're going to add 2,
and then we're going to loop through
each of the numbers in that range.
I recommend, if this is confusing at all,
to simply just print out what that range creates
and then change each of the inputs in the range
and then making sure you understand how it works.
But once you understand it, it's actually pretty simple,
we're looping through each of the numbers
in this artificial range that we've created,
and then we simply add each of the numbers
that's in that range to each other
and we end up with the sum of all the numbers in that range,
and then we can simply print it out.
In the second method of completing this coding exercise,
and believe me, there's way more
than two ways of achieving this,
this is just another one I want present you with.
In this case, we're creating a range
that just has a start and an end.
Now, when we don't specify the increment,
the third value in the range function,
then it's going to increment by 1.
So we start at 1 and we end our range at target + 1
to include the target, not go above the target,
that's just the peculiarity of how range works,
and then we loop through the range,
and then we use our even checker
which we've created before, by using the modulo (%).
So we say if the number we're currently looping through,
modulo 2 is equal to 0,
then it's cleanly divisible by 2,
therefore it must be even, in that case,
we're going to add this number to our alternative_sum
to accumulate the numbers that are even.
And then finally, we print it out.
So if you created a different program but yet it still works
and you managed to use a for loop, then congratulations.
That's all I wanted to see.
Now, very often as you're programming
you might have certain needs that you might want to
be able to get the sum of all the numbers
in a list or the maximum of all the numbers in the list.
Now, if you have a quick Google around
and you can't find a good way of doing it,
a simple way like the len() function or the sum() function,
then you know that you can always just write it yourself.
But if you do look for it
and you find something, for example,
in this case the range() function,
and you do want use it,
then it can often save you a lot of time.
So there are lots of options
and there are more options when you have more skills.
So hopefully this exercise worked out for you.
If not, be sure to go back and modify your code as needed.