Leap Year Program in Python with Output
Check whether a given year is a leap year using the Gregorian calendar rule.
- Time
O(1) for one year, O(n) for a range of n years - Space
O(1), or O(k) to store k leap years in a list - Hints3
- Read2 min
A year is a leap year if it is divisible by 400, or if it is divisible by 4 but not by 100. So 2000 and 2024 are leap years, while 1900 and 2023 are not. In Python the full check is (year % 4 == 0 and year % 100 != 0) or year % 400 == 0. The standard library also has calendar.isleap(year).
Problem statement
Write a leap year program in Python. Given a year, print whether it is a leap year.
Use the Gregorian calendar rule, which is the calendar used in India and most of the world today. Test it on 2024, 2023, 1900, 2000 and 2100.
A positive integer year.
"<year> is a leap year" or "<year> is not a leap year".
Examples
year = 20242024 is a leap year2024 is divisible by 4 and not by 100.
year = 19001900 is not a leap year1900 is divisible by 100 but not by 400.
year = 20002000 is a leap year2000 is divisible by 400.
Constraints
- 1 <= year <= 9999
Think before you code
Reveal the questions to ask yourself first
- Is "divisible by 4" enough? What about 1900?
- Which test should come first so the later ones do not give a wrong answer?
- How do you write "divisible by" in Python?
Hints
Open one at a time — try again after each.1 Hint 1
2 Hint 2
3 Hint 3
Approach
Reveal the step-by-step approach
Leap year logic
The Earth takes about 365.2422 days to go around the Sun. Adding one day every 4 years gives 365.25, which is slightly too much. So century years skip the leap day, unless they are divisible by 400.
| Rule | Example | Leap year? |
|---|---|---|
| Divisible by 400 | 2000, 2400 | Yes |
| Divisible by 100 but not 400 | 1900, 2100 | No |
| Divisible by 4 but not 100 | 2024, 2028 | Yes |
| Not divisible by 4 | 2023, 2025 | No |
if-elif-else order
year % 400 == 0: leap.- else
year % 100 == 0: not leap. - else
year % 4 == 0: leap. - else: not leap.
If you test 4 first, 1900 passes and gives the wrong answer.
For more Python practice, try the palindrome program in Python and the Python interview questions.
Dry run
Walk through the example step by step
year | % 400 == 0 | % 100 == 0 | % 4 == 0 | result
-----+------------+------------+----------+---------
2024 | no | no | yes | leap
2023 | no | no | no | not leap
1900 | no | yes | (skip) | not leap
2000 | yes | (skip) | (skip) | leap
2100 | no | yes | (skip) | not leap
Solution
Reveal the full Python solution
def is_leap(year):
if year % 400 == 0:
return True
elif year % 100 == 0:
return False
elif year % 4 == 0:
return True
else:
return False
for year in [2024, 2023, 1900, 2000, 2100]:
if is_leap(year):
print(year, "is a leap year")
else:
print(year, "is not a leap year")Output:
2024 is a leap year
2023 is not a leap year
1900 is not a leap year
2000 is a leap year
2100 is not a leap year
Each elif runs only if the tests above it were false, so 1900 stops at the
% 100 test and never reaches % 4.
Leap year condition in Python, in one line
year = 2024
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print("Leap year")
else:
print("Not a leap year")
Leap year
To take the year from the user, replace the first line with
year = int(input("Enter a year: ")).
calendar.isleap() and leap years in a range
import calendar
print(calendar.isleap(1900), calendar.isleap(2000))
start, end = 2000, 2030
leaps = []
for y in range(start, end + 1):
if (y % 4 == 0 and y % 100 != 0) or y % 400 == 0:
leaps.append(y)
print(leaps)
False True
[2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028]
calendar.isleap() uses the same rule. In a test, write the condition yourself unless
the question allows library functions. range(start, end + 1) includes the end year.
For a working date tool, try the days calculator. For more Python practice, see linear search in Python.
O(1) for one year, O(n) for a range of n yearsO(1), or O(k) to store k leap years in a listReview before the interview
Common mistakes
- Checking only year % 4 == 0, which wrongly marks 1900 and 2100 as leap years.
- Testing % 4 first in an if-elif chain, so the century rule never runs.
- Missing brackets: year % 4 == 0 and year % 100 != 0 or year % 400 == 0 works because and binds tighter than or, but brackets make it clear.
- Forgetting int() around input(), so year % 4 fails on a string.
Edge cases to test
- 2000 is a leap year; 1900 and 2100 are not.
- Years before 1582 used the Julian calendar in history. The program applies the Gregorian rule to every year.
- Year 0 or negative input: decide whether to reject it. The rule itself gives True for 0.
- Non-numeric input makes int() raise ValueError.
Interview follow-ups
- How many days are there in February for a given year?
- How would you count the leap years between two years without a loop?
- How would you find the next leap year after a given year?
Keep practising
Find the Day of the Week for a Given Date
Hints · Dry run · SolutionSolve Similar problemCalculate Age from Date of Birth
Hints · Dry run · SolutionSolve Similar problemFizzBuzz Problem
Hints · Dry run · SolutionSolve Related learningWhat Is Python Used For
TutorialLearn Related learningVariables And Data Types
TutorialLearn Related learningPython Interview Questions
TutorialLearn CareerPilot AIPractice under pressure
Take an AI mock interview and get question-by-question feedback.
Mock interviewStartPractising Python for interviews?
CodeBegun’s Python Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
