Last modified: Nov 16, 2026

Python Leap Year Program with Examples

A leap year has 366 days instead of 365. The extra day lands on February 29. Checking for a leap year is a classic beginner exercise in Python.

It teaches you how to combine conditions with logical operators. It also shows how the modulo operator works in real code.

In this guide, you will learn the leap year rules. You will also see several clean Python programs with full output. Each example is short and easy to follow.

What Is a Leap Year?

A leap year happens almost every four years. The extra day keeps our calendar in sync with Earth's orbit around the sun.

The rules are simple. A year is a leap year if it is divisible by 4. But years divisible by 100 are not leap years, unless they are also divisible by 400.

So 2024 is a leap year. 1900 is not. 2000 is a leap year. These rules matter for correct results.

The Leap Year Rules in Plain English

Here is the logic in three short steps:

Step 1: If the year is divisible by 400, it is a leap year.

Step 2: If the year is divisible by 100 but not 400, it is not a leap year.

Step 3: If the year is divisible by 4 but not 100, it is a leap year. Otherwise, it is not.

You can express all of this in one line of Python. The modulo operator % gives the remainder of a division. A remainder of zero means the year divides evenly.

Basic Python Leap Year Program

Let's start with the simplest version. We ask the user for a year and print the result.


# Ask the user for a year
year = int(input("Enter a year: "))

# Apply the leap year rules
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
    print(year, "is a leap year")
else:
    print(year, "is not a leap year")

Run the program and try a few years:


Enter a year: 2024
2024 is a leap year

Enter a year: 1900
1900 is not a leap year

The condition reads like the rules. The or handles the 400 rule. The and handles the 4 and 100 rules together.

This pattern of combining conditions appears everywhere in Python. For example, the same idea powers a simple check for whether a number is even or odd.

Leap Year Program Using a Function

Functions make your code reusable. Wrap the logic in a function and call it whenever you need it.


def is_leap_year(year):
    # A year is a leap year if divisible by 400
    # or divisible by 4 but not by 100
    if year % 400 == 0:
        return True
    if year % 100 == 0:
        return False
    if year % 4 == 0:
        return True
    return False

# Test the function with several years
for y in [2024, 2023, 1900, 2000]:
    print(y, "->", is_leap_year(y))

2024 -> True
2023 -> False
1900 -> False
2000 -> True

This version uses early returns. Each rule is checked in order. The first match decides the answer.

Early returns keep functions flat and readable. They are a good habit to build early.

You can pass years into the function in many ways. If you want to review how values reach a function, read this guide on how Python function arguments work.

Leap Year Program Using the Calendar Module

Python has a built-in way to check leap years. The calendar module includes a ready-made function.


import calendar

# Use the built-in function
print(calendar.isleap(2024))
print(calendar.isleap(1900))
print(calendar.isleap(2000))

True
False
True

This is the fastest option in real projects. But writing the logic yourself is great practice for beginners.

You can also count leap years in a range. The module has a helper for that too.


import calendar

# Count leap years between 2000 and 2020
count = calendar.leapdays(2000, 2021)
print("Leap years from 2000 to 2020:", count)

Leap years from 2000 to 2020: 6

One-Line Leap Year Check

Sometimes you want a compact expression. A single line works well for quick scripts.


year = 2024

# Compact conditional expression
result = "Leap year" if (year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)) else "Not a leap year"
print(result)

Leap year

Keep one-liners for simple cases. For larger programs, the function version is easier to test and maintain.

Common Mistakes to Avoid

Mistake 1: Checking only divisibility by 4. This marks 1900 as a leap year, which is wrong.

Mistake 2: Forgetting the 400 rule. This marks 2000 as a regular year, which is also wrong.

Mistake 3: Using the wrong operator order. Wrap each rule in parentheses so the logic stays clear.

Mistake 4: Not converting input to an integer. The input function always returns a string, so wrap it with int.

Small logic errors like these are common in beginner code. The same care applies to other classic exercises, such as a prime number check program, where one wrong condition changes the result.

Practice Ideas

Try these short exercises to lock in the logic:

Print all leap years from 2000 to 2050. Store the results in a list.

Ask the user for a start and end year, then count the leap years between them.

Write a version that returns the next leap year after any given year.

Each exercise builds on the same core condition. Repetition is how the rules become second nature.

Why This Exercise Matters

The leap year program looks tiny. But it covers big ideas. You practice conditional logic, the modulo operator, functions, and input handling.

These skills appear in almost every Python project. Number logic shows up in tasks like converting temperatures, such as this example on converting Celsius to Fahrenheit in Python.

Conditional thinking also powers sequence problems and math checks. Once the leap year rules feel easy, harder conditions will too.

Conclusion

The Python leap year program is a perfect first project. The rules are short, the code is short, and the output is instant.

Use the function version for real code. Use calendar.isleap when you need speed. Use the one-liner for quick tests.

Practice with different years, including edge cases like 1900 and 2000. That is how you master the logic for good.