Remember that conditions are evaluated in the order they are written.
if year%4==0:
return True
Every year divisible by four is a leap year...
elif year%400==0:
return True
and every year divisible by 400 is also divisible by four, so this condition is never evaluated
elif year%100 != 0:
return False
All other years not divisible by 100 are not leap years.
else:
return False
And everything else is also not a leap year.
There is no point in having these two last cases; if condition: return False else: return False
is equivalent to return False
.
In summary: your conditional is equivalent to
if year%4==0:
return True
else:
return False
or simply return year % 4 == 0
.
I hope you agree that this is incorrect.
Let's define a leap year.
The year X is a leap year if
- It is divisible by four,
- unless it is divisible by 100,
- except if it is divisible by 400
That is,
return x % 4 == 0 and not (x % 100 == 0 and not (x % 400 == 0))
which we can simplify,
return x % 4 == 0 and not (x % 100 == 0 and x % 400 != 0))
One of DeMorgan's laws: not (a and b) == (not a) or (not b)
:
return x % 4 == 0 and (x % 100 != 0 or x % 400 == 0)