Home / Python / Example programs / Conditions

Python program: Check a leap year

A year is a leap year if it is divisible by 4, except century years, which must be divisible by 400.

The code

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

for y in [2024, 2026, 1900, 2000]:
    print(y, "leap" if is_leap(y) else "not leap")

import calendar
print(calendar.isleap(2024))   # the standard library agrees

Output

2024 leap
2026 not leap
1900 not leap
2000 leap
True

How it works

The whole rule fits in one Boolean expression: divisible by 4, and either not a century or divisible by 400. The calendar module has the same check built in.

Python features used

Learn the ideas behind it in the free lesson Making Decisions.