How to Find a Leap Year in Python

black marker on notebook

A quick answer: Use this Python function to check if a given year is a leap year.

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

Example usage:

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

If you don’t need to implement the leap year functionality yourself, use the built-in calendar module’s isleap function:

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

Don’t use this shortcut in your programming course assignments 😉

More Details about Leap Years

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 divisble by 4, 100, and 400.

Modulo—How to Calculate Remainders

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.

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

How to Find Leap Year in Python Using Modulo

To know if a year is a leap year, you can use modulo.

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.

Here is one 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 or not. If it’s not, there is no way the year can be a leap year.
  • The next if-else checks 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).

This approach works, but you can simplify it by flattening down the nested if-else expressions:

  • If a year is divisible by 400, so it is a leap year.
  • If a year is not divisible by 400 but is divisible by 100, so it is not a leap year.
  • If a year is not divisible by 400 nor 100 but is divisible by 4, so it is a leap year.
def is_leap(year):
    if year % 400 == 0:
        return True
    if year % 100 == 0:
        return False
    if year % 4 == 0:
        return True
    return False

And you can simplify it even further to get the simplest form possible:

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

Practice Python Leap Year Yourself

Implement a program that asks a user a year as an input, and prints the next leap year as an output.

For example:

Year: 2011
The next leap year is 2012

Year: 2020
The next leap year is 2024

Year: 1896
The next leap year is 1904

The solution is here.

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

50 Python Interview Questions with Answers

50+ Buzzwords of Web Development

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest

Leave a Comment

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