Python Practice › If-Else Programs › Program 30
Check a leap year
Write a Python program to check whether a year is a leap year.
Input
2000, 1900, 2024
Output
2000 is a leap year 1900 is not a leap year 2024 is a leap year
Approach
- Divisible by 400: leap. Else divisible by 100: not leap. Else divisible by 4: leap.
Solution
for year in [2000, 1900, 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
2000 is a leap year 1900 is not a leap year 2024 is a leap year
Line by line
Lines 2-7
The rules are checked from the most specific (400) to the most general (4), which is why the order matters.
Learn the concept
New to this topic? Read the lesson, then come back.