Python: How to List All Leap Years in a Given Range (3 Examples)

Updated: February 13, 2024 By: Guest Contributor Post a comment

Overview

Finding leap years within a specified range involves understanding what constitutes a leap year and applying that knowledge to generate a list of years that fulfill the leap year criteria. This tutorial will guide you through the process using Python, a highly versatile programming language known for its readability and efficiency. Whether you’re a beginner or an experienced developer, this guide will provide you with clear examples and explanations to effortlessly list all leap years in a given range.

Understanding Leap Years

A leap year is a year that is divisible by 4 but not by 100 unless it is also divisible by 400. This means that years like 2000 and 2020 are leap years, but 1900, despite being divisible by 4, is not a leap year because it is also divisible by 100 and not divisible by 400.

Setting Up Your Environment

Before diving into the code, ensure that you have Python installed on your computer. You can download the latest version of Python from the official Python website. Once installed, you can write your Python scripts using a text editor or an Integrated Development Environment (IDE) like PyCharm or Visual Studio Code.

Example 1: Basic Leap Year Checker

This first example demonstrates the basic logic for determining whether a year is a leap year.

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

print(is_leap_year(2000))  # Output: True
print(is_leap_year(1900))  # Output: False

As shown, our is_leap_year function returns True if the year meets the leap year criteria, otherwise False.

Example 2: Listing Leap Years in a Range

Building on our leap year checker, we can create a function to list all leap years within a specific range.

def list_leap_years(start_year, end_year):
    leap_years = []
    for year in range(start_year, end_year + 1):
        if is_leap_year(year):
            leap_years.append(year)
    return leap_years

print(list_leap_years(2000, 2020))  # Output: [2000, 2004, 2008, 2012, 2016, 2020]

This function iterates through each year in the specified range, checks if it is a leap year, and if so, adds it to the list of leap years.

Example 3: Optimizing the Leap Year Listing

For larger ranges, efficiency becomes important. Let’s optimize our function to reduce the number of iterations.

def optimized_list_leap_years(start_year, end_year):
    leap_years = []
    # Adjust start_year to the next possible leap year
    if start_year % 4 != 0:
        start_year += 4 - (start_year % 4)
    for year in range(start_year, end_year + 1, 4):
        if is_leap_year(year):
            leap_years.append(year)
    return leap_years

print(optimized_list_leap_years(1900, 2020))  # Output: [1904, 1908, ... 2016, 2020]

Here, we adjust the starting year to the next leap year if necessary and increment by 4 in our loop. Though many years divisible by 4 are included, only those also meeting the additional leap year criteria are added to our list.

Conclusion

Python’s simplicity and power allow us to effectively implement logic to list all leap years in a given range. By understanding the leap year rules and applying them through Python’s syntax, we can create functions ranging from basic checks to more optimized listings for better performance. Whether for educational purposes, application in software projects, or just for fun, these examples serve as a solid foundation for working with leap years in Python.

As always, remember to test your code thoroughly and experiment with modifications to better understand how different parts of the language work together. Python’s readability and flexibility make it an excellent choice for such tasks, offering a rewarding learning experience.