0
0
PythonProgramBeginner · 2 min read

Python Program to Check Leap Year

You can check a leap year in Python by using the condition year % 400 == 0 or (year % 4 == 0 and year % 100 != 0) which returns True if the year is leap, otherwise False.
📋

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 check if a year is leap, first see if it is divisible by 400. If yes, it is leap. If not, check if it is divisible by 4 but not by 100. If yes, it is leap. Otherwise, it is not leap.
📐

Algorithm

1
Get the year as input
2
Check if the year is divisible by 400; if yes, it is leap
3
Else check if the year is divisible by 4 and not divisible by 100; if yes, it is leap
4
Otherwise, 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 divisibility by 400

1900 % 400 == 300 (False)

3

Check divisibility 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 400?1900 % 400 == 0False
Divisible by 4 and not 100?1900 % 4 == 0 and 1900 % 100 != 0False
💡

Why This Works

Step 1: Divisible by 400 means leap

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 100 means leap

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

Step 3: Otherwise not leap

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 function with return
python
def is_leap_year(y):
    return y % 400 == 0 or (y % 4 == 0 and y % 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 separates logic into a function for reuse and clarity.

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

Time Complexity

The program performs a fixed number of arithmetic operations and comparisons, so it runs in constant time.

Space Complexity

Only a few variables are used, so the space used is constant.

Which Approach is Fastest?

All approaches run in constant time; using the calendar module adds slight overhead but improves readability.

ApproachTimeSpaceBest For
Direct condition checkO(1)O(1)Simple and fast checks
Using calendar moduleO(1)O(1)Readability and built-in reliability
Function with returnO(1)O(1)Code reuse and clarity
💡
Remember that years divisible by 100 are not leap years unless also divisible by 400.
⚠️
Beginners often forget to exclude years divisible by 100 but not by 400 as leap years.