1
00:00:01,140 --> 00:00:04,440
This code exercise is a difficult one,

2
00:00:04,440 --> 00:00:07,710
and you can see that in the description box.

3
00:00:07,710 --> 00:00:11,010
I've marked it out as a difficult challenge.

4
00:00:11,010 --> 00:00:13,620
The goal of this exercise is for you to build,

5
00:00:13,620 --> 00:00:17,790
a program that checks when given any year,

6
00:00:17,790 --> 00:00:20,343
whether if that year is a leap year.

7
00:00:21,180 --> 00:00:25,813
A leap year has 366 days instead of the usual 365

8
00:00:27,330 --> 00:00:29,790
with an extra day in February

9
00:00:29,790 --> 00:00:31,920
and it's actually kind of interesting,

10
00:00:31,920 --> 00:00:35,640
the reason behind why we need a leap year.

11
00:00:35,640 --> 00:00:38,130
And in the description box you'll see a link

12
00:00:38,130 --> 00:00:40,350
to a video that actually explains

13
00:00:40,350 --> 00:00:43,290
to you the full reasoning behind it and it's really,

14
00:00:43,290 --> 00:00:44,370
really fascinating.

15
00:00:44,370 --> 00:00:47,010
So take a look if you're interested.

16
00:00:47,010 --> 00:00:49,950
In order to work out whether if a year is a leap year,

17
00:00:49,950 --> 00:00:54,950
there is a logic to it and it's expressed like this.

18
00:00:55,180 --> 00:01:00,180
So you take a particular year and it is a leap year

19
00:01:00,240 --> 00:01:05,239
if it can be divisible by four with no remainder,

20
00:01:05,801 --> 00:01:09,450
except if that year is also divisible

21
00:01:09,450 --> 00:01:13,860
by 100 with no remainder, and unless the year

22
00:01:13,860 --> 00:01:17,910
is also divisible by 400 with no remainder.

23
00:01:17,910 --> 00:01:21,120
So there's actually three steps that we need to take,

24
00:01:21,120 --> 00:01:23,040
our year through.

25
00:01:23,040 --> 00:01:27,000
And it's because these criteria flip whether

26
00:01:27,000 --> 00:01:29,970
if a year is leap year or not leap year.

27
00:01:29,970 --> 00:01:33,810
If you take a look at the example in the description box

28
00:01:33,810 --> 00:01:36,750
and see how I've worked through various years,

29
00:01:36,750 --> 00:01:41,730
like year 2000 or year 2100 and

30
00:01:41,730 --> 00:01:46,730
how I've applied these rules into maths to show you

31
00:01:46,980 --> 00:01:49,770
which one is leap and which one's not leap

32
00:01:49,770 --> 00:01:51,990
and the logic behind it.

33
00:01:51,990 --> 00:01:55,390
What I recommend doing is to try it out yourself

34
00:01:56,422 --> 00:01:59,070
with pen and paper before you write any code,

35
00:01:59,070 --> 00:02:02,220
just pick a random year out of the top of your head

36
00:02:02,220 --> 00:02:05,430
and work through these three rules and figure out

37
00:02:05,430 --> 00:02:08,100
whether if it's a leap year or not a leap year,

38
00:02:08,100 --> 00:02:11,130
and then just type into Google that year and check is

39
00:02:11,130 --> 00:02:12,480
this a leap year?

40
00:02:12,480 --> 00:02:15,180
That's probably the easiest way to just wrap your head

41
00:02:16,342 --> 00:02:18,570
around these three rules.

42
00:02:18,570 --> 00:02:20,640
Now, if English is not your first language

43
00:02:20,640 --> 00:02:25,470
and you struggle to understand what the statements mean,

44
00:02:25,470 --> 00:02:28,907
then I've actually created a flowchart for you.

45
00:02:28,907 --> 00:02:31,980
So if you click on the link in the description box,

46
00:02:31,980 --> 00:02:34,290
it will take you to a flow chart

47
00:02:34,290 --> 00:02:37,460
which you can use to figure out whether if a year

48
00:02:37,460 --> 00:02:40,020
is a leap year or not a leap year.

49
00:02:40,020 --> 00:02:41,640
And this can be really useful

50
00:02:41,640 --> 00:02:43,680
even if English is your first language because

51
00:02:43,680 --> 00:02:45,360
it is really tricky.

52
00:02:45,360 --> 00:02:49,080
The wording for the logic can be confusing,

53
00:02:49,080 --> 00:02:52,200
but I think it's really important to convert something

54
00:02:52,200 --> 00:02:53,610
that is logic,

55
00:02:53,610 --> 00:02:57,150
so in words to a program.

56
00:02:57,150 --> 00:03:00,900
I can of course reword this to say, "If this,

57
00:03:00,900 --> 00:03:02,910
then that..." but then that's kind of like cheating

58
00:03:02,910 --> 00:03:04,320
and writing it for you.

59
00:03:04,320 --> 00:03:06,070
So don't worry if you get it wrong,

60
00:03:07,202 --> 00:03:08,430
just try your best at figuring out

61
00:03:08,430 --> 00:03:11,160
how the logic works, understanding it

62
00:03:11,160 --> 00:03:14,370
if you still don't understand it use the flow chart

63
00:03:14,370 --> 00:03:17,610
and then convert that logic into code.

64
00:03:17,610 --> 00:03:19,893
So that is your challenge.

65
00:03:28,110 --> 00:03:30,180
The first thing we know is we can get hold

66
00:03:30,180 --> 00:03:35,180
of a year as an integer into our program on line two

67
00:03:35,340 --> 00:03:37,170
and we can work with it

68
00:03:37,170 --> 00:03:40,890
through using various conditional statements to check

69
00:03:40,890 --> 00:03:43,923
whether if that year matches our criteria.

70
00:03:45,210 --> 00:03:47,430
The first thing we're going to check is

71
00:03:47,430 --> 00:03:49,590
whether if that year can be divided

72
00:03:49,590 --> 00:03:52,560
by four with no remainder,

73
00:03:52,560 --> 00:03:55,620
and remember in order to figure out what the remainder is

74
00:03:55,620 --> 00:03:58,410
in programming we use the modulo symbol (%).

75
00:03:58,410 --> 00:04:01,444
So we've seen this before when we did the odd

76
00:04:01,444 --> 00:04:03,360
and even exercise, and in this case,

77
00:04:03,360 --> 00:04:05,340
it's again the same thing we can write.

78
00:04:05,340 --> 00:04:09,900
if year % 4 == 0:

79
00:04:09,900 --> 00:04:13,290
then that means it's cleanly divisible by four.

80
00:04:13,290 --> 00:04:15,450
So that means that we can proceed

81
00:04:15,450 --> 00:04:17,579
to the next thing to check.

82
00:04:17,579 --> 00:04:20,160
And if this is not true,

83
00:04:20,160 --> 00:04:23,070
so here we've got an else statement that matches

84
00:04:23,070 --> 00:04:25,350
this if condition,

85
00:04:25,350 --> 00:04:27,420
then in that case it's obviously not a leap year.

86
00:04:27,420 --> 00:04:29,310
We've already eliminated that year

87
00:04:29,310 --> 00:04:31,443
because it can't be divisible by four.

88
00:04:32,700 --> 00:04:35,700
The next step is to drill it down even further.

89
00:04:35,700 --> 00:04:39,330
So if that year is cleanly divisible by 4,

90
00:04:39,330 --> 00:04:42,930
well then is it cleanly divisible by 100?

91
00:04:42,930 --> 00:04:44,820
Because according to our logic,

92
00:04:44,820 --> 00:04:49,820
if it's divisible by four but also divisible cleanly by 100

93
00:04:49,890 --> 00:04:52,020
then it's actually not a leap year.

94
00:04:52,020 --> 00:04:54,030
This is the part that gets a little bit confusing

95
00:04:54,030 --> 00:04:56,010
for most people because in this case,

96
00:04:56,010 --> 00:04:59,850
when it's divisible by 4 and divisible by 100,

97
00:04:59,850 --> 00:05:02,250
then it is actually not a leap year

98
00:05:02,250 --> 00:05:05,280
and we have to continue checking the final condition.

99
00:05:05,280 --> 00:05:09,600
So that's why we've got this embedded if and else statement.

100
00:05:09,600 --> 00:05:12,460
So at this point we've already verified the year

101
00:05:13,322 --> 00:05:14,220
is cleanly divisible by four

102
00:05:14,220 --> 00:05:18,330
and we're now checking if it is cleanly divisible by 100

103
00:05:18,330 --> 00:05:20,610
then we're going to continue checking.

104
00:05:20,610 --> 00:05:25,140
But if it is not divisible by 100 with no remainder

105
00:05:25,140 --> 00:05:26,700
and it's divisible by 4 ,

106
00:05:26,700 --> 00:05:30,720
then it is now confirmed as a leap year,

107
00:05:30,720 --> 00:05:32,640
and this will become more obvious

108
00:05:32,640 --> 00:05:35,190
if you actually look at the flow chart that I provided

109
00:05:35,190 --> 00:05:37,770
in the description box as well.

110
00:05:37,770 --> 00:05:39,450
This is just one of those peculiarities

111
00:05:39,450 --> 00:05:42,063
about working out which year is a leap year.

112
00:05:43,770 --> 00:05:45,900
The final check we have to do is

113
00:05:45,900 --> 00:05:49,620
in the case when a year is cleanly divisible by 4

114
00:05:49,620 --> 00:05:52,006
also cleanly divisible by 100.

115
00:05:52,006 --> 00:05:55,050
Now the final thing is to check,

116
00:05:55,050 --> 00:06:00,050
well is that year also cleanly divisible by 400?

117
00:06:00,720 --> 00:06:03,750
Because in that case it flips again

118
00:06:03,750 --> 00:06:07,064
and it is now a leap year

119
00:06:07,064 --> 00:06:09,330
if all three of those conditions are met.

120
00:06:09,330 --> 00:06:13,410
There's many routes to navigate through the flow chart,

121
00:06:13,410 --> 00:06:16,020
but in the final section here

122
00:06:16,020 --> 00:06:21,020
if all three criteria is true, divisible cleanly by 4,

123
00:06:21,150 --> 00:06:24,990
100 and 400, then it is a leap year,

124
00:06:24,990 --> 00:06:27,900
otherwise, if that last condition fails

125
00:06:27,900 --> 00:06:30,360
then it is not a leap year.

126
00:06:30,360 --> 00:06:34,050
That's why we've got three nested if and else statements

127
00:06:34,050 --> 00:06:39,050
in order to complete this logic in the flow chart

128
00:06:39,224 --> 00:06:42,660
or in the text version of how to work out a leap year.

129
00:06:42,660 --> 00:06:45,627
There are many ways of solving this exercise

130
00:06:45,627 --> 00:06:47,910
and this code solution is not the only way.

131
00:06:47,910 --> 00:06:51,840
Feel free to Google for how to check for leap year

132
00:06:51,840 --> 00:06:53,850
in Python, and you'll see many,

133
00:06:53,850 --> 00:06:55,020
many other methods.

134
00:06:55,020 --> 00:06:57,360
You could combine different conditions,

135
00:06:57,360 --> 00:07:01,560
you could use singular if and else statements,

136
00:07:01,560 --> 00:07:04,290
there are many, many ways of doing the same thing

137
00:07:04,290 --> 00:07:06,600
so don't be too hard on yourself.

138
00:07:06,600 --> 00:07:09,330
If your solution passed all the checks

139
00:07:09,330 --> 00:07:12,060
then that means you got it right

140
00:07:12,060 --> 00:07:15,780
and every solution is accepted as long

141
00:07:15,780 --> 00:07:17,160
as it passes all the tests

142
00:07:17,160 --> 00:07:19,680
because we checked for all the different years

143
00:07:19,680 --> 00:07:21,600
against all the different conditions.

144
00:07:21,600 --> 00:07:22,770
And if your code works,

145
00:07:22,770 --> 00:07:24,210
then that's great.

146
00:07:24,210 --> 00:07:25,200
Now, as always,

147
00:07:25,200 --> 00:07:28,740
my solutions optimize for the most readability

148
00:07:28,740 --> 00:07:31,740
and hopefully the easiest to understand.

149
00:07:31,740 --> 00:07:32,820
And in this case,

150
00:07:32,820 --> 00:07:36,090
this one kind of mirrors the flow chart one for one.

151
00:07:36,090 --> 00:07:38,550
So hopefully you can understand where it comes

152
00:07:38,550 --> 00:07:41,790
from by comparing it against the flow chart.

153
00:07:41,790 --> 00:07:44,484
And this is a good tip for programming,

154
00:07:44,484 --> 00:07:45,317
whenever you get stuck with your logic,

155
00:07:45,317 --> 00:07:47,220
try and make a flow chart for yourself

156
00:07:47,220 --> 00:07:50,190
and then use that flow chart to create the code.

157
00:07:50,190 --> 00:07:53,284
If you had any fixes you need to do go back

158
00:07:53,284 --> 00:07:54,960
and make that now and make sure you've understood

159
00:07:54,960 --> 00:07:57,513
how we've converted logic into code.

