Python Leap Year Checker
Determine if a year is a leap year using standard divisibility checks in Python.
How it Works
A leap year contains 366 days instead of 365, with an extra day added to February. It occurs to keep the calendar year synchronized with the astronomical year.
The rule states that a year is a leap year if it is divisible by 4, except for century years (years ending in 00), which must also be divisible by 400.
This calculation is implemented in Python using combined logical operators (`and`, `or`) and modulo operators.
Source Code
Divisibility-based algorithm to verify multiple test years including century years.
def is_leap_year(year):
# A year is leap if it is divisible by 4
# and (not divisible by 100 or divisible by 400)
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
years = [2000, 2004, 1900, 2023, 2024]
for y in years:
status = "Leap Year" if is_leap_year(y) else "Not a Leap Year"
print(f"Year {y}: {status}")Year 2000: Leap Year
Year 2004: Leap Year
Year 1900: Not a Leap Year
Year 2023: Not a Leap Year
Year 2024: Leap YearReal-world Applications
- Scheduling, calendar, and datetime modules validation
- Determining age or date differences accurately
- Finance and interest calculation formulas
Frequently Asked Questions
Why was 1900 not a leap year?
Even though 1900 is divisible by 4, it ends in 00 (century year), so it must be divisible by 400 to be a leap year. Since 1900 / 400 is not a whole number, it is not a leap year.