Check a leap year

EasyIf-Else Programs · 30 of 64

Write a Python program to check whether a year is a leap year.

Advertisement
Input
2000, 1900, 2024
Output
2000 is a leap year
1900 is not a leap year
2024 is a leap year

Approach

  1. Divisible by 400: leap. Else divisible by 100: not leap. Else divisible by 4: leap.

Solution

Python
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.

More if-else programs

← All 64 If-Else Programs

Leave a Comment