1
00:00:00,440 --> 00:00:01,620
In this exercise

2
00:00:01,620 --> 00:00:04,890
you're going to write a program that calculates the sum

3
00:00:04,890 --> 00:00:09,890
of all the even numbers from 1, to a particular number.

4
00:00:11,280 --> 00:00:15,480
So in the Input we're going to put in a particular number

5
00:00:15,480 --> 00:00:20,480
and then the code is to go through starting from 1,

6
00:00:20,640 --> 00:00:23,250
finding all the even numbers between 1

7
00:00:23,250 --> 00:00:27,240
and the Input number, and then adding them all together.

8
00:00:27,240 --> 00:00:28,080
In order to make sure

9
00:00:28,080 --> 00:00:30,630
that the code doesn't take too long to run,

10
00:00:30,630 --> 00:00:34,080
we are making sure the inputs don't go beyond 1,000

11
00:00:34,080 --> 00:00:36,480
because you might be sitting here for ages.

12
00:00:36,480 --> 00:00:39,930
So make sure that you put in the input as a small number

13
00:00:39,930 --> 00:00:44,070
like less than a hundred, or at least less than a thousand.

14
00:00:44,070 --> 00:00:47,280
The longer it is, the longer your code is going to take to run.

15
00:00:47,280 --> 00:00:50,700
But make sure that your code achieves its functionality.

16
00:00:50,700 --> 00:00:52,170
Check the Description pane,

17
00:00:52,170 --> 00:00:54,090
take a look at the Example Inputs

18
00:00:54,090 --> 00:00:56,550
and expected Example Outputs,

19
00:00:56,550 --> 00:00:59,040
and see if you can figure out how to do this

20
00:00:59,040 --> 00:01:01,980
using any Python function that you can come across

21
00:01:01,980 --> 00:01:04,200
but also using all of the basic skills

22
00:01:04,200 --> 00:01:06,453
in Python programming that you already have.

23
00:01:07,410 --> 00:01:10,890
And as a hint, you might want to look up the range() function

24
00:01:10,890 --> 00:01:12,873
to help you complete this exercise.

25
00:01:15,360 --> 00:01:18,870
Now, there are a number of ways of achieving this

26
00:01:18,870 --> 00:01:23,760
and I've included two in the complete solution

27
00:01:23,760 --> 00:01:25,510
but let's go through the first one.

28
00:01:26,370 --> 00:01:29,190
We start off with a variable called even_sum,

29
00:01:29,190 --> 00:01:31,980
and we set it equal to 0.

30
00:01:31,980 --> 00:01:33,660
This is going to be our accumulator.

31
00:01:33,660 --> 00:01:36,690
We're going to use this variable to add to it

32
00:01:36,690 --> 00:01:38,673
and be able to get the final sum.

33
00:01:40,140 --> 00:01:43,500
Now, how are we going to accumulate into it?

34
00:01:43,500 --> 00:01:46,320
Well, we're going to use a for loop

35
00:01:46,320 --> 00:01:50,910
to loop through each of the numbers in the range,

36
00:01:50,910 --> 00:01:55,910
starting from 2, which is the first even number after 1.

37
00:01:56,220 --> 00:01:58,800
So remember, we're looking for all the even numbers

38
00:01:58,800 --> 00:02:02,220
between 1 and our target number.

39
00:02:02,220 --> 00:02:06,060
And the range() function takes a starting point,

40
00:02:06,060 --> 00:02:07,380
an ending point,

41
00:02:07,380 --> 00:02:11,730
and an increment, similar to our traditional for loop,

42
00:02:11,730 --> 00:02:12,930
which you might have come across

43
00:02:12,930 --> 00:02:15,420
if you've learned the C programming language

44
00:02:15,420 --> 00:02:18,300
or other older programming languages.

45
00:02:18,300 --> 00:02:20,730
Essentially, we can provide three inputs

46
00:02:20,730 --> 00:02:23,400
through the range() function start, end,

47
00:02:23,400 --> 00:02:27,930
and the increment that we're going to go by each time.

48
00:02:27,930 --> 00:02:31,020
Now you can read upon the Python range() function

49
00:02:31,020 --> 00:02:33,270
but essentially what we're doing here

50
00:02:33,270 --> 00:02:35,130
is we're creating a for loop,

51
00:02:35,130 --> 00:02:37,440
and we're creating a range,

52
00:02:37,440 --> 00:02:41,940
starts from 2, ends at the target + 1.

53
00:02:41,940 --> 00:02:44,820
And the reason why it ends at target + 1

54
00:02:44,820 --> 00:02:49,820
is because the range will stop just before the end.

55
00:02:49,830 --> 00:02:53,640
So if your target was say, 100,

56
00:02:53,640 --> 00:02:56,640
then range will give you a range of numbers

57
00:02:56,640 --> 00:02:58,860
starting from 2 up until 99.

58
00:02:58,860 --> 00:03:02,310
So we have to modify that by adding 1

59
00:03:02,310 --> 00:03:06,240
if we want to include the target number in this range.

60
00:03:06,240 --> 00:03:09,030
So 2 comma and then target + 1,

61
00:03:09,030 --> 00:03:11,880
and then the final number is how much we want to add

62
00:03:11,880 --> 00:03:14,370
to the starting number each time.

63
00:03:14,370 --> 00:03:17,640
So what is the gap between each number.

64
00:03:17,640 --> 00:03:19,410
Because we want all the even numbers,

65
00:03:19,410 --> 00:03:21,900
each time we're going to add 2,

66
00:03:21,900 --> 00:03:23,520
and then we're going to loop through

67
00:03:23,520 --> 00:03:25,653
each of the numbers in that range.

68
00:03:27,480 --> 00:03:30,300
I recommend, if this is confusing at all,

69
00:03:30,300 --> 00:03:33,840
to simply just print out what that range creates

70
00:03:33,840 --> 00:03:37,170
and then change each of the inputs in the range

71
00:03:37,170 --> 00:03:39,873
and then making sure you understand how it works.

72
00:03:41,670 --> 00:03:44,910
But once you understand it, it's actually pretty simple,

73
00:03:44,910 --> 00:03:46,560
we're looping through each of the numbers

74
00:03:46,560 --> 00:03:49,230
in this artificial range that we've created,

75
00:03:49,230 --> 00:03:51,720
and then we simply add each of the numbers

76
00:03:51,720 --> 00:03:54,690
that's in that range to each other

77
00:03:54,690 --> 00:03:58,890
and we end up with the sum of all the numbers in that range,

78
00:03:58,890 --> 00:04:00,813
and then we can simply print it out.

79
00:04:02,400 --> 00:04:06,150
In the second method of completing this coding exercise,

80
00:04:06,150 --> 00:04:07,590
and believe me, there's way more

81
00:04:07,590 --> 00:04:09,810
than two ways of achieving this,

82
00:04:09,810 --> 00:04:12,420
this is just another one I want present you with.

83
00:04:12,420 --> 00:04:14,160
In this case, we're creating a range

84
00:04:14,160 --> 00:04:16,350
that just has a start and an end.

85
00:04:16,350 --> 00:04:18,959
Now, when we don't specify the increment,

86
00:04:18,959 --> 00:04:21,779
the third value in the range function,

87
00:04:21,779 --> 00:04:24,060
then it's going to increment by 1.

88
00:04:24,060 --> 00:04:29,060
So we start at 1 and we end our range at target + 1

89
00:04:29,070 --> 00:04:32,280
to include the target, not go above the target,

90
00:04:32,280 --> 00:04:35,040
that's just the peculiarity of how range works,

91
00:04:35,040 --> 00:04:37,080
and then we loop through the range,

92
00:04:37,080 --> 00:04:39,030
and then we use our even checker

93
00:04:39,030 --> 00:04:42,660
which we've created before, by using the modulo (%).

94
00:04:42,660 --> 00:04:45,660
So we say if the number we're currently looping through,

95
00:04:45,660 --> 00:04:47,910
modulo 2 is equal to 0,

96
00:04:47,910 --> 00:04:50,070
then it's cleanly divisible by 2,

97
00:04:50,070 --> 00:04:52,800
therefore it must be even, in that case,

98
00:04:52,800 --> 00:04:56,280
we're going to add this number to our alternative_sum

99
00:04:56,280 --> 00:04:59,430
to accumulate the numbers that are even.

100
00:04:59,430 --> 00:05:01,590
And then finally, we print it out.

101
00:05:01,590 --> 00:05:06,330
So if you created a different program but yet it still works

102
00:05:06,330 --> 00:05:10,440
and you managed to use a for loop, then congratulations.

103
00:05:10,440 --> 00:05:12,690
That's all I wanted to see.

104
00:05:12,690 --> 00:05:14,670
Now, very often as you're programming

105
00:05:14,670 --> 00:05:18,060
you might have certain needs that you might want to

106
00:05:18,060 --> 00:05:20,790
be able to get the sum of all the numbers

107
00:05:20,790 --> 00:05:24,480
in a list or the maximum of all the numbers in the list.

108
00:05:24,480 --> 00:05:26,130
Now, if you have a quick Google around

109
00:05:26,130 --> 00:05:29,400
and you can't find a good way of doing it,

110
00:05:29,400 --> 00:05:33,210
a simple way like the len() function or the sum() function,

111
00:05:33,210 --> 00:05:36,660
then you know that you can always just write it yourself.

112
00:05:36,660 --> 00:05:38,370
But if you do look for it

113
00:05:38,370 --> 00:05:39,930
and you find something, for example,

114
00:05:39,930 --> 00:05:41,700
in this case the range() function,

115
00:05:41,700 --> 00:05:43,020
and you do want use it,

116
00:05:43,020 --> 00:05:45,150
then it can often save you a lot of time.

117
00:05:45,150 --> 00:05:46,710
So there are lots of options

118
00:05:46,710 --> 00:05:49,230
and there are more options when you have more skills.

119
00:05:49,230 --> 00:05:52,530
So hopefully this exercise worked out for you.

120
00:05:52,530 --> 00:05:56,043
If not, be sure to go back and modify your code as needed.

