A leap year has 366 days instead of 365, because February has an extra day, the 29th. In this lesson you will write a Python program to check whether a year is a leap year, and list all leap years in a range.
Run the code in our online Python compiler. The compiler does not take keyboard input, so the examples use fixed values.
The rules
A year is a leap year if:
- It is divisible by 4, and it is not divisible by 100, or
- It is divisible by 400.
Year | Divisible by 4? | Divisible by 100? | Divisible by 400? | Leap year? |
|---|---|---|---|---|
2024 | Yes | No | No | Yes |
2023 | No | No | No | No |
1900 | Yes | Yes | No | No |
2000 | Yes | Yes | Yes | Yes |
Century years such as 1900 and 2100 are not leap years, unless they are also divisible by 400, like 2000.
Program: check a year
def is_leap_year(year):
return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
for year in [2000, 1900, 2024, 2023, 2100]:
print(year, is_leap_year(year))Output:
2000 True
1900 False
2024 True
2023 False
2100 FalseThe % operator gives the remainder of a division. A remainder of 0 means the year is divisible by that number.
The same program with if-else
This version prints a message, and shows the rules step by step.
year = 2024
if year % 400 == 0:
print(year, 'is a leap year')
elif year % 100 == 0:
print(year, 'is not a leap year')
elif year % 4 == 0:
print(year, 'is a leap year')
else:
print(year, 'is not a leap year')Output:
2024 is a leap yearThe order of the checks matters. We test 400 first, then 100, then 4, so that century years are handled correctly.
Use the calendar module
Python has a ready-made function for this:
import calendar
print(calendar.isleap(2024))
print(calendar.isleap(1900))Output:
True False
Program: all leap years from 2000 to 2030
Add this below the is_leap_year() function:
leap_years = [y for y in range(2000, 2031) if is_leap_year(y)]
print(leap_years)Output:
[2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028]
Take the year from the user (on your computer)
year = int(input('Enter a year: '))
if is_leap_year(year):
print(year, 'is a leap year')
else:
print(year, 'is not a leap year')Our online compiler does not support input() yet, so use a fixed value there.
Common mistakes
- Checking only divisibility by 4. This makes 1900 look like a leap year, but it is not.
- Forgetting the 400 rule. The year 2000 is a leap year because it is divisible by 400.
- Wrong order in if-else. If you test the year % 4 condition first, century years give the wrong answer.
Practice questions
- Count the leap years between 1900 and 2100.
- Print the next leap year after a given year.
- Write a function that returns the number of days in a given year.
- Find how many days there are in February of a given year.
FAQs
Is 2100 a leap year?
No. It is divisible by 100 but not by 400.
Is 2000 a leap year?
Yes, because it is divisible by 400.
Why do leap years exist?
The Earth takes about 365.24 days to go around the Sun, so an extra day is added roughly every 4 years to keep the calendar in step.
