#Python Leap Year problem

8 messages · Page 1 of 1 (latest)

coral basin
#

Introduction
A leap year (in the Gregorian calendar) occurs:

-In every year that is evenly divisible by 4.
-Unless the year is evenly divisible by 100, in which case it's only a leap year if the year is also evenly divisible by 400.


In my head the conditions for a leap year are:

when it's evenly divisible by 4
OR
when it's evenly divisible by 100 AND 400

Am I not getting this introduction right? I'm french canadian so maybe I'm just not reading this right because I can't seem to complete the task. Here's my code;

def leap_year(year):
    return year % 4 == 0 or year % 100 == 0 and year % 400 == 0

The actual solution is:

def leap_year(year):
    return year % 4 == 0 and year % 100 != 0 or year % 400 == 0

Which in my head translate to the year has to be evenly divisible by 4 but not 100, or evenly by 400 only. I feel dumb, can somebody explain this to me ahah?

verbal spade
#

Per your code, would the year 300 be considered a leap year or not? Then, is 300 in fact a leap year or not?

coral basin
#

Why 300 ? But yes, 300 is a leap year because it is evenly divisible by 4

hot smelt
coral basin
#

So "in which case it's only a leap year if the year is also evenly divisible by 400" applies to divide by 4 AND 100?

#

My bad, like I said I'm french canadian, english is not my first language and I thought "in which case" was only meant for that last thing you said. Not all.

hot smelt
#

A leap year is defined by the following rules

A year is a leap year when it's divisible by 4
It's not a leap year when it's also divisible by 100
unless it's also divisible by 400 then it's a leap year again

#

So is 300 a leap year?