Home » Python Tutorial » Leap Year Program in Python

# Leap Year Program in Python

A leap year is a year that has 366 days rather than 365 days. While you’re at doing cool stuff in programming, have you thought about writing a leap year program in python? Oh well, in this tutorial, you will learn how to write a python code that determines whether a particular year is a leap.

Let’s start by discussing the conditions (or set of conditions if you may) that determine whether a year is a leap year. A year is a leap year if:

• It has 366 days – obvious stuff already yea?
• The year is divisible by 4.
• Is a centurion year and is divisible by 400.

It is important to point out that centurion years are not leap years if they are not divisible by 400.

We will explain these conditions as we build the python program. Let’s see how to do that

## Steps to Building a Python Program for Leap Year

This will be the thought pattern we will follow when building the python program

1. For a given year, if this year is evenly divided by 4 and there is no remainder. Then the year is a leap year. For example, the year 2020 is a leap year because it is perfectly divisible by 4.

2. For a given year, if this year is divisible by 4 but not divisible by 100. Then the year is a leap year. For example, 2016 is divisible by 4 but not by 100. But if the year is divisible by both 4 and 100 (centurion years), then the next condition must be met if it’s a leap year.

3. For a given year that is divisible by 4 and 100, that number must also be divisible by 400 for it to be a leap year. For example, the year 1800 is not a leap year because it is not divisible by 400 even if it is divisible by both 4 and 100. In another case, the year 1200 is a leap year because it is divisible by both 4, 100, and 400.

## Writing the Python Program

``````#A python program to check whether a given year is a leap year or not

def check_leap_year():
#allow user input the year
year = int(input('Entire the year you wish to check: '))

#define the conditions
if year % 400 == 0:
return(f"Yes, {year} is a leap year")
if year % 100 == 0:
return(f"No, {year} is not a leap year")
if year % 4 == 0:
return(f"Yes, {year} is a leap year")
return(f"No, {year} is not a leap year")
``````

Output:

``````Entire the year you wish check: 1800

'No, 1800 is not a leap year'``````

Let’s now type in a leap year such as 2020.

``````Entire the year you wish to check: 2020

'Yes, 2020 is a leap year'``````

Wrapping up, we have seen a code for leap year program in python. Make sure to try it on your PC and experiment with it.

Shares