Python: To check whether a given year is a leap year or not?

Question:

I wanted to create a python program with user defined function which should read a year entered by user and return True/False by checking whether the entered year is a leap year or not.

The code should follow the Gregorian Calendar System also.

This is what I tried

def is_leap(year):
    leap = False
    if(year%4==0):
        if(year%100!=0):
            if(year%400==0):
                leap= True
            else:
                leap= False
        else:
            leap= False
                
    else:
        leap= False
    
    return leap

year = int(input())
print(is_leap(year))

And I am not getting the desired output.

I tried this code with following two inputs

2024

Output was

False

And

2023

Output was

False

What am I missing here?

Asked By: Ansu Sinha

||

Answers:

By nesting the if/else, you act as an AND while you would need an OR.

Best is to refactor your code:

def is_leap(year):
    if (year%4==0) and (year%100!=0) or (year%400==0):
        return True
    return False

Which is equivalent to:

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

Tests:

assert not is_leap(1700)
assert not is_leap(1800)
assert not is_leap(1900)
assert not is_leap(2023)
assert is_leap(1600)
assert is_leap(2000)
Answered By: mozway

Let’s step through your code for 2024:

  • year%4==0 is True, so we enter the if block
  • year%100!=0 is also True, so we enter the if block again.

And now you check for year%400==0. Um, why? 2024 isn’t a century, so it can never be a multiple of 400. So your logic is broken here. 2024 is divisible by 4 and not divisible by 100. At this point we can simply return True!

Where we need to check for divisibility by 400 is in the next case (where year%100!=0 is False).

If we take all of this, we get this code:

def is_leap(year):
    if(year%4==0):
        if(year%100!=0):
            return True
        else:
            if(year%400==0):
                return True
            else:
                return False
    else:
        return False

(note how I got rid of the completely unnecessary variable.)

Answered By: Sören

Handling the exceptions first yields clearer code.

def is_leap(year):
    if year % 400 == 0:
        return True  # an exception to the century rule
    if year % 100 == 0:
        return False  # centuries aren't leap years
    return year % 4 == 0
Answered By: J_H
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.