Write Python. Understand every error.

Open the editor

Python programs · Basics

Python program to check a leap year

A leap year has 366 days. The rule has three parts, and the order of the tests matters.

Approach

  1. A year divisible by 4 is a leap year...
  2. ...unless it is divisible by 100...
  3. ...except that years divisible by 400 are leap years again.

The program

year = int(input())
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
    print(year, "is a leap year")
else:
    print(year, "is not a leap year")

Sample run

Input
2024
Output
2024 is a leap year

Run this program in Py-CRAFT

How the code works

The condition says: divisible by 4 and not by 100, or divisible by 400. So 2024 (divisible by 4, not by 100) is a leap year, 1900 (divisible by 100 but not by 400) is not, and 2000 (divisible by 400) is.

Try changing it

Common mistakes

Learn the ideas

More programs

Share this page: