- Rule
- %4 and not %100
Check Leap Year in Python
What you’ll learn
- Gregorian leap-year rule with 4/100/400 divisibility.
- A clean Python predicate function.
- Printing leap years in a range and live checking.
Overview
Leap years reduce calendar drift by adding February 29 under specific modular rules.
Two programs
Single year check and range listing.
Live preview
Instant result for safe integer years.
Rigor
Century exceptions handled correctly.
Prerequisites
Modulo operator and boolean logic.
- Know
%andand/orconditions in Python. - Use a
forloop for range scan.
What is a leap year?
Leap year has February 29 in Gregorian calendar.
Divisible by 4 is not enough: centuries require divisible by 400.
Predicate
(y % 4 == 0 and y % 100 != 0) or (y % 400 == 0)
20242024 % 4 == 0 and 2024 % 100 != 0, so leap year.
Intuition
- Rule
2021 % 4 = 1
Takeaway: century years are special cases.
Live preview
Check any integer year in safe range.
Algorithm
Goal: evaluate Gregorian leap predicate.
Divisible by 4
If false, not leap.
Century exception
If divisible by 100, require divisible by 400.
📜 Pseudocode
function is_leap_year(y):
return (y mod 4 = 0 and y mod 100 != 0) or (y mod 400 = 0)Single year: 2024
Checks one year with the standard Gregorian rule.
def is_leap_year(year: int) -> bool:
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
year = 2024
if is_leap_year(year):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")Explanation
Boolean expression directly matches the textbook leap rule.
Leap years in [2024, 2050]
Prints all leap years in the same range as reference.
def is_leap_year(year: int) -> bool:
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
start_year = 2024
end_year = 2050
print(f"Leap years in the range {start_year} to {end_year}:")
for y in range(start_year, end_year + 1):
if is_leap_year(y):
print(y, end=" ")
print()Explanation
Pattern is mostly every 4 years, with century exceptions outside this interval.
Alternatives
Lookup table: useful for fixed small year ranges in UI code.
Date libraries: prefer standard libs for production-grade date handling.
Interview: state the 4/100/400 rule cleanly first.
❓ FAQ
🔄 Input / output examples
Test these years with the predicate.
| Year | Leap? |
|---|---|
2024 | Yes |
2021 | No |
2000 | Yes |
1900 | No |
Edge cases and pitfalls
Leap predicate is only one part of full date validation.
Century override
2000 is leap, 1900 is not.
Historical vs proleptic
Code usually applies Gregorian formula uniformly.
start > end
Guard or swap bounds in range loops.
Non-integer year
Reject non-integers in UI/preview parsing.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
| One year check | O(1) | O(1) |
| Range [a,b] | O(b-a+1) | O(1) |
No extra structures needed beyond simple counters.
Summary
- Rule:
(y%4==0 and y%100!=0) or (y%400==0). - Code: one predicate function plus optional range loop.
- Watch-outs: century years and historical calendar context.
Gregorian reform refined the simple every-4-years rule: century years are not leap years unless divisible by 400.
8 people found this page helpful
