 # Leap Year Program in Python (3 Easy Ways)

In this article, we’ll see the logic behind leap year and three simple ways to write the leap year program in python.

The year with 366 days in it is called a leap year. And that one extra day is called Leap Day and it is added as the 29th day in February month.

Rules to treat a year as a leap

A year satisfying any one of the below rules is called a leap year

1. A year exactly divisible by 400

2. The year is exactly divisible by 4 but not by 100.

## The logic behind a leap year

The logic behind the leap year is Earth takes 365 days and 5.76 hours (365.24 days) to complete one orbit around the sun. So to make this extra quarter day accountable, an extra day is added to the calendar every four years.

The calendar we use (Gregorian calendar) is based on the solar year which considers the time it takes for Earth to orbit the Sun as a reference.

However, when it was introduced initially, the solar system and the calendar were not in sync due to this minute difference. But later in the 16th century, Pope Gregory XIII proposed to add an extra day to compensate for this extra time. And the same is followed ever since.

Fun Fact

In some places like Ireland, Leap Day is celebrated as Bachelor’s Day where women are allowed to propose to men. A few other cultures treat this as a lucky day and celebrate it every four years.

Now, without much ado, let’s see different ways to check if a given year is leap year in Python.

## Leap Year Program in Python:

There are three different ways to write leap year program in python

1. Straightforward python program to check leap year
2. Using the calendar module’s `isleap()` method
3. Using `DateTime` module

### 1. Straightforward python program to check leap year

Here in this leap year program in python, we’ll write a method to check the leap year. `is_leap_year()` methods return `True` if the given year is a leap, and false otherwise.

``````def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return True
else:
return False

print('2000 is a leap year?', is_leap_year(2000))
print('1996 is a leap year?', is_leap_year(1996))
print('1700 is a leap year?', is_leap_year(1700))``````

Output:

``````2000 is a leap year? True
1996 is a leap year? True
1700 is a leap year? False``````

And we will extend this program, to find all the leap years between two given years. In this leap year program in python, we have taken two date ranges i.e 1990 and 2001 and for every year we called is_leap_year() method. If it returns true we appended it to leap_years list.

``````def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return True
else:
return False

leap_years = []

# taking start and end years to calculate leap years between them.
(start_year, end_year) = (1900, 2001)

for year in range(start_year, end_year):

# calling is_leap_year method to check leap year
if is_leap_year(year):
# appending year to list if it's a leap year
leap_years.append(year)

print('There are ', len(leap_years), ' leap years between', startYear, ' and ', endYear - 1)
print(leap_years)``````

Output:

``````There are 25 leap years between 1900 and 2000

[1904, 1908, 1912, 1916, 1920, 1924, 1928, 1932, 1936, 1940, 1944, 1948, 1952, 1956, 1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992, 1996, 2000]``````

### 2. Using the calendar module’s isleap() method

An alternate way to check leap year in Python is to use python’s inbuilt `calendar` module. For `isleap()` method, input is the year in Int format. It will return True if the year is a leap and false otherwise.

``````import calendar

year = input("Enter the year to check if it is Leap Year: ")

print(calendar.isleap(year))``````

Output:

``````Enter the year to check if it is Leap Year: 1996
True

Enter the year to check if it is Leap Year: 2022
False``````

### 3. With DateTime module

This method is a bit tricky. Let’s see how?

To create an object for the date class from `datetime` module we’ll need to pass three parameters year, month and day. And if we try to create a date object for a non-leap year on Feb 29th, it will throw an error. We’ll try to write the leap year program in python using this particular case.

``````from datetime import date

def is_leap_year(year):
try:
date(year, 2, 29)
return True
except:
return False

print('2000 is a leap year?', is_leap_year(2000))
print('1996 is a leap year?', is_leap_year(1996))
print('1700 is a leap year?', is_leap_year(1700))``````

Output:

``````2000 is a leap year? True
1996 is a leap year? True
1700 is a leap year? False``````

Hope we are able to help you with the Python program to check leap year. Follow us, for more insightful posts.

• What is the logic for leap year?

Every year satisfying any one of the below conditions

1. divisible by 4 and not divisible by 100 (or)
2. divisible by 400.

• Is every year divisible by 4 a leap year?

No, Every year that is divisible by 4 is not a leap year. However, the century year (ending with 00) should also be divisible by 400.

• Is 2024 a leap year?

Yes, as it is divisible by 4 and it’s not a century year.

• Is there any relation between Leap year and Daylight Savings?

No, they aren’t directly related to each other. However, they both are used to adjust the calendar and clock to match some of the natural earth rhythms.

### 1 thought on “Leap Year Program in Python (3 Easy Ways)”

1. Michel Bruyère