Check Leap Year in Python

Beginner
⏱️ 8 min read
📚 Updated: May 2026
🎯 2 Code Examples
Gregorian

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 % and and/or conditions in Python.
  • Use a for loop 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.

2024Leap
1900Not
2000Leap

Predicate

(y % 4 == 0 and y % 100 != 0) or (y % 400 == 0)

2024

2024 % 4 == 0 and 2024 % 100 != 0, so leap year.

Intuition

2024Leap
Rule
%4 and not %100
2021Common
Rule
2021 % 4 = 1

Takeaway: century years are special cases.

Live preview

Check any integer year in safe range.

Try 2000, 1900, or 2021.

Live result
Press “Check leap”.

Algorithm

Goal: evaluate Gregorian leap predicate.

Divisible by 4

If false, not leap.

Century exception

If divisible by 100, require divisible by 400.

📜 Pseudocode

Pseudocode
function is_leap_year(y):
    return (y mod 4 = 0 and y mod 100 != 0) or (y mod 400 = 0)
1

Single year: 2024

Checks one year with the standard Gregorian rule.

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

2

Leap years in [2024, 2050]

Prints all leap years in the same range as reference.

python
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

Leap iff (y%4==0 and y%100!=0) or (y%400==0).
Yes, because it is divisible by 400.
No, divisible by 100 but not by 400.
No. This page uses the usual proleptic Gregorian arithmetic rule.
In code you can still apply the formula to integer years, but historical interpretation differs.
Single-year check is O(1). Range scan is O(b-a+1).

🔄 Input / output examples

Test these years with the predicate.

YearLeap?
2024Yes
2021No
2000Yes
1900No

Edge cases and pitfalls

Leap predicate is only one part of full date validation.

400

Century override

2000 is leap, 1900 is not.

Year systems

Historical vs proleptic

Code usually applies Gregorian formula uniformly.

Range

start > end

Guard or swap bounds in range loops.

Input

Non-integer year

Reject non-integers in UI/preview parsing.

⏱️ Time and space complexity

TaskTimeExtra space
One year checkO(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.
Did you know?

Gregorian reform refined the simple every-4-years rule: century years are not leap years unless divisible by 400.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful