0
0
PythonProgramBeginner · 2 min read

Python Program to Check if Year is Leap Year

You can check if a year is a leap year in Python using if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0): to test the leap year conditions.
📋

Examples

Input2000
Output2000 is a leap year
Input1900
Output1900 is not a leap year
Input2024
Output2024 is a leap year
🧠

How to Think About It

To decide if a year is leap, check if it divides evenly by 400, or if it divides evenly by 4 but not by 100. This matches the real-world leap year rules.
📐

Algorithm

1
Get the year as input
2
Check if the year is divisible by 400; if yes, it is a leap year
3
Otherwise, check if the year is divisible by 4 but not by 100; if yes, it is a leap year
4
If none of the above, it is not a leap year
5
Print the result
💻

Code

python
year = int(input("Enter a year: "))
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
    print(f"{year} is a leap year")
else:
    print(f"{year} is not a leap year")
Output
Enter a year: 2024 2024 is a leap year
🔍

Dry Run

Let's trace the year 1900 through the code

1

Input year

year = 1900

2

Check divisible by 400

1900 % 400 == 300 (False)

3

Check divisible by 4 and not by 100

1900 % 4 == 0 (True) and 1900 % 100 == 0 (True), so condition is False

4

Result

Print '1900 is not a leap year'

StepConditionResult
Divisible by 4001900 % 400 == 0False
Divisible by 4 and not 1001900 % 4 == 0 and 1900 % 100 != 0False
💡

Why This Works

Step 1: Divisible by 400 means leap year

If a year divides evenly by 400, it is always a leap year, so we check year % 400 == 0 first.

Step 2: Divisible by 4 but not by 100 means leap year

If the year is divisible by 4 but not by 100, it is a leap year, checked by year % 4 == 0 and year % 100 != 0.

Step 3: Otherwise, not a leap year

If neither condition is true, the year is not a leap year.

🔄

Alternative Approaches

Using calendar module
python
import calendar
year = int(input("Enter a year: "))
if calendar.isleap(year):
    print(f"{year} is a leap year")
else:
    print(f"{year} is not a leap year")
This uses Python's built-in calendar module for simplicity but adds an import dependency.
Using a function
python
def is_leap_year(year):
    return (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0)
year = int(input("Enter a year: "))
if is_leap_year(year):
    print(f"{year} is a leap year")
else:
    print(f"{year} is not a leap year")
This approach wraps the logic in a function for reuse and clarity.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of arithmetic and logical operations regardless of input size, so it runs in constant time O(1).

Space Complexity

Only a few variables are used, and no extra data structures are created, so space complexity is O(1).

Which Approach is Fastest?

All approaches run in constant time, but using the calendar module adds a small overhead due to import, while the direct arithmetic check is fastest and simplest.

ApproachTimeSpaceBest For
Direct arithmetic checkO(1)O(1)Simple and fast checks without imports
calendar.isleap() functionO(1)O(1)Using built-in library for readability
Function wrapperO(1)O(1)Reusable code and clarity
💡
Remember to check the 400-year rule before the 4 and 100-year checks to handle century years correctly.
⚠️
Beginners often forget to check the 400-year rule, causing century years like 1900 to be wrongly marked as leap years.