This code exercise is a difficult one,
and you can see that in the description box.
I've marked it out as a difficult challenge.
The goal of this exercise is for you to build,
a program that checks when given any year,
whether if that year is a leap year.
A leap year has 366 days instead of the usual 365
with an extra day in February
and it's actually kind of interesting,
the reason behind why we need a leap year.
And in the description box you'll see a link
to a video that actually explains
to you the full reasoning behind it and it's really,
really fascinating.
So take a look if you're interested.
In order to work out whether if a year is a leap year,
there is a logic to it and it's expressed like this.
So you take a particular year and it is a leap year
if it can be divisible by four with no remainder,
except if that year is also divisible
by 100 with no remainder, and unless the year
is also divisible by 400 with no remainder.
So there's actually three steps that we need to take,
our year through.
And it's because these criteria flip whether
if a year is leap year or not leap year.
If you take a look at the example in the description box
and see how I've worked through various years,
like year 2000 or year 2100 and
how I've applied these rules into maths to show you
which one is leap and which one's not leap
and the logic behind it.
What I recommend doing is to try it out yourself
with pen and paper before you write any code,
just pick a random year out of the top of your head
and work through these three rules and figure out
whether if it's a leap year or not a leap year,
and then just type into Google that year and check is
this a leap year?
That's probably the easiest way to just wrap your head
around these three rules.
Now, if English is not your first language
and you struggle to understand what the statements mean,
then I've actually created a flowchart for you.
So if you click on the link in the description box,
it will take you to a flow chart
which you can use to figure out whether if a year
is a leap year or not a leap year.
And this can be really useful
even if English is your first language because
it is really tricky.
The wording for the logic can be confusing,
but I think it's really important to convert something
that is logic,
so in words to a program.
I can of course reword this to say, "If this,
then that..." but then that's kind of like cheating
and writing it for you.
So don't worry if you get it wrong,
just try your best at figuring out
how the logic works, understanding it
if you still don't understand it use the flow chart
and then convert that logic into code.
So that is your challenge.
The first thing we know is we can get hold
of a year as an integer into our program on line two
and we can work with it
through using various conditional statements to check
whether if that year matches our criteria.
The first thing we're going to check is
whether if that year can be divided
by four with no remainder,
and remember in order to figure out what the remainder is
in programming we use the modulo symbol (%).
So we've seen this before when we did the odd
and even exercise, and in this case,
it's again the same thing we can write.
if year % 4 == 0:
then that means it's cleanly divisible by four.
So that means that we can proceed
to the next thing to check.
And if this is not true,
so here we've got an else statement that matches
this if condition,
then in that case it's obviously not a leap year.
We've already eliminated that year
because it can't be divisible by four.
The next step is to drill it down even further.
So if that year is cleanly divisible by 4,
well then is it cleanly divisible by 100?
Because according to our logic,
if it's divisible by four but also divisible cleanly by 100
then it's actually not a leap year.
This is the part that gets a little bit confusing
for most people because in this case,
when it's divisible by 4 and divisible by 100,
then it is actually not a leap year
and we have to continue checking the final condition.
So that's why we've got this embedded if and else statement.
So at this point we've already verified the year
is cleanly divisible by four
and we're now checking if it is cleanly divisible by 100
then we're going to continue checking.
But if it is not divisible by 100 with no remainder
and it's divisible by 4 ,
then it is now confirmed as a leap year,
and this will become more obvious
if you actually look at the flow chart that I provided
in the description box as well.
This is just one of those peculiarities
about working out which year is a leap year.
The final check we have to do is
in the case when a year is cleanly divisible by 4
also cleanly divisible by 100.
Now the final thing is to check,
well is that year also cleanly divisible by 400?
Because in that case it flips again
and it is now a leap year
if all three of those conditions are met.
There's many routes to navigate through the flow chart,
but in the final section here
if all three criteria is true, divisible cleanly by 4,
100 and 400, then it is a leap year,
otherwise, if that last condition fails
then it is not a leap year.
That's why we've got three nested if and else statements
in order to complete this logic in the flow chart
or in the text version of how to work out a leap year.
There are many ways of solving this exercise
and this code solution is not the only way.
Feel free to Google for how to check for leap year
in Python, and you'll see many,
many other methods.
You could combine different conditions,
you could use singular if and else statements,
there are many, many ways of doing the same thing
so don't be too hard on yourself.
If your solution passed all the checks
then that means you got it right
and every solution is accepted as long
as it passes all the tests
because we checked for all the different years
against all the different conditions.
And if your code works,
then that's great.
Now, as always,
my solutions optimize for the most readability
and hopefully the easiest to understand.
And in this case,
this one kind of mirrors the flow chart one for one.
So hopefully you can understand where it comes
from by comparing it against the flow chart.
And this is a good tip for programming,
whenever you get stuck with your logic,
try and make a flow chart for yourself
and then use that flow chart to create the code.
If you had any fixes you need to do go back
and make that now and make sure you've understood
how we've converted logic into code.