Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Check Leap Year in Python

1900 is not a leap year; 2000 is. Build the full Gregorian test in Python and practice the 400, 100, and 4 exceptions with beginner-friendly Boolean logic.

BeginnerBasic Python ProgramsExample 17 of 25
check-leap-year.py
1# Program to check if a year is a leap year
2
3year = int(input("Enter a year: "))
4
5if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
6 print(year, "is a leap year")
7else:
8 print(year, "is not a leap year")

Sample Output

2024 is a leap year

2024 is divisible by four and not by one hundred, so the second route succeeds.

In this one

  • Apply the Gregorian century exception
  • Combine and with or
  • Test boundary years instead of only ordinary years

How it works

  1. 1

    Read year as an integer.

  2. 2

    Test divisibility by 400 or divisibility by 4 without divisibility by 100.

  3. 3

    Print the leap or non-leap message from the resulting Boolean.

Edge cases

Century exception

Enter 1900.

It is divisible by 100 but not 400, so it is not a leap year.

Four-century exception

Enter 2000.

The % 400 route succeeds, so it is a leap year.

What's going on

The rule people remember is every four years. Then they ship a bug for 1900. Gregorian century years need one more check: divisible by 400 means leap, divisible by 100 alone means not leap.

The condition accepts a year divisible by 400, or a year divisible by 4 but not by 100. Parentheses make those two routes visible. 2000 takes the first route; 2024 takes the second; 1900 takes neither.

% computes each remainder and == 0 checks divisibility. and requires both parts of the ordinary four-year rule, while or accepts either route. The sample prints 2024 as a leap year. This is calendar-rule arithmetic, not a historical calendar converter; the program also accepts zero and negative integers without a date-range policy.

After this program

If the idea is still fuzzy, read Day 1: Introduction to Python in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 basic python programs, or see every topic on Python exercises.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Why is 1900 not a leap year under this rule?

Pick an answer to check it.

Question 2

Why does 2000 pass?

Pick an answer to check it.

Questions people ask

Why is 1900 not a leap year under this rule?

Century years must also be divisible by 400, and 1900 is not.

Why does 2000 pass?

2000 is divisible by 400, so the first route accepts it.

What does the 2024 example test?

It tests an ordinary multiple of four that is not a century year.