Python Program to Check Leap Year (Theory & Example Use)

A leap year is a year that is divisible by 4. The exception is the years that are divisible by 100 but not by 400. This comprehensive guide teaches you how to check if a year is leap year in Python. You will also learn how to find the next leap year given the current year.

The Quick Answer

To check if a year is a leap year in Python, you can use this function:

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

Here are some example calls to the function:

print(is_leap(2000))  # --> True
print(is_leap(1900))  # --> False
print(is_leap(2016))  # --> True

Checking if a year is a leap year in Python is a common task in programming courses. Thus, you should implement the logic yourself as done above. But if you don’t need to implement the leap year functionality, use the built-in calendar module’s isleap function.

Here is an example:

from calendar import isleap
    
print(isleap(2000))
print(isleap(1900))
print(isleap(2016))

This is a quick answer to the problem. Next, let’s take a look at a more detailed answer.

What Is a Leap Year?

You might know that a year is not exactly 365 days long. It’s actually around 365.24 days in true length. So to compensate for this, we need to add an extra day every four years to the calendar. A year with 366 days is called a leap year.

Definition: A leap year is a year that is (evenly) divisible by 4. The exception is the years that are divisible by 100 but not by 400.

Leap year examples:

  • 2012
  • 2016
  • 1986

Examples of non-leap years:

  • 2011
  • 2003
  • 1997
  • 2009

Exceptions:

  • 1900 is not a leap year because it is divisible by 4 and 100, but not by 400.
  • 2000 is a leap year because it is divisible by 4, 100, and 400.

Now that you understand how leap years work and why they are needed in the first place, let’s take a look at a common technique for finding a leap year.

Modulo—The Remainder in Division

Modulo returns the remainder of an integer division. In other words, it gives you the leftovers of a “fair division”.

For example, if you have 7 slices of pizza and 4 hungry guests, you can share one slice per person, but 3 slices are leftover. You could figure out the number of leftover pieces by calculating 7 modulo 4, which gives 3.

How to Calculate Modulo in Python?

In Python, the modulo operator is the percent sign %.

For example, you can calculate 7 modulo 4 in Python by:

7 % 4

This returns a result of 3. Try it yourself!

How to Find Leap Year in Python Using Modulo?

To know whether a year is a leap year or not, you can use the modulo to calculate the remainder in the division.

By definition, a leap year is evenly divisible by 4. But the exception is the years that are not divisible by 100 but are divisible by 400.

1. Use If-Else Statements

Here is a way you can turn this definition into Python code:

def is_leap(year):
    if year % 4 == 0:
        if year % 100 == 0:
            if year % 400 == 0:
                return True
            else:
                return False
        else:
            return True
    else:
        return False
  • Here the outer if-else checks if the year is divisible by 4. If it’s not, the year cannot be a leap year.
  • The next if-else tests if the year is divisible by 100. If it is, the only way it can be a leap year is if it’s also divisible by 400. If it’s not, the year is not a leap year. And if the year is not divisible by 100, it must be a leap year (because it’s divisible by 4).

The above code certainly works, but you can simplify it by flattening down the nested if-else expressions by returning the result earlier.

2. Simplify the Code

Let’s reformulate the definition of a leap year into three checks in plain English:

  1. If a year is divisible by 400, it must be a leap year.
  2. If a year is not divisible by 400 but is divisible by 100, it’s not a leap year.
  3. If a year is not divisible by 400 or 100 but is divisible by 4, it’s a leap year.

With these in mind, you can simplify the if-else mess you saw earlier to a much simpler function that only uses if-statements:

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

Even though this code looks much better, you can simplify it even further. As a matter of fact, you don’t even need to use if-statements at all. Instead, you can chain the remainder checks with logical operators like this:

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

Now you know how to check if the current year is a leap year in Python. Next, let’s take a look at how you can figure out the next leap year.

Next Leap Year in Python

To calculate the next leap year in Python:

  1. Specify the current year.
  2. Get the previous leap year.
  3. Add 4 to the previous leap year.
  4. Check if the resulting year is a leap year.
  5. If it’s not, add 4 years to the year until it is a leap year.

Here is how it looks in code:

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


def next_leap(year):
    next_possible = year - (year % 4) + 4
    if is_leap(next_possible):
        return next_possible
    else:
        while is_leap(next_possible) == False:
            next_possible += 4
        return next_possible

Example run:

year = int(input("Year: "))
print(f"The next leap year is {next_leap(year)}")

Output:

Year: 2021
The next leap year is 2024

Conclusion

A leap year is a year with 366 days. It occurs every four years on years divisible by 4 (but not on years divisible by 100 and not 400).

You can use the modulo to figure out leap years in Python.

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

Thanks for reading. I hope you enjoyed it.

Happy coding!

Further Reading

Leave a Comment

Your email address will not be published. Required fields are marked *