# Leap Year Program in Python

In this program, we will be discussing how to check the Leap Year Program in Python. We will be using the if condition, pre-defined function, and Logical operator in a single-line code.

We use leap years to keep our calendar in sync with the seasons. How do leap years work, and how often do they occur? Leap years are years where an extra, or intercalary, day is added to the end of the shortest month, February. The intercalary day, February 29, is commonly referred to as leap day.

Simply a leap year is a year with an extra day on February 29. Which is added nearly every four years to the calendar year.

## Python Program to Check Leap Year

### Leap Year Rules: How to Calculate Leap Years

You should follow the following steps to determine whether a year is a leap year or not.

1. If a year is evenly divisible by 4 means having no remainder, then go to the next step. If it is not divisible by 4. It is not a leap year. For example, 1997 is not a leap year.
2. If a year is divisible by 4 but not by 100. For example 2012, it is a leap year. If a year is divisible by both 4 and 100, go to the next step.
3. If a year is divisible by 100 but not by 400. For example 1900, then it is not a leap year. If a year is divisible by both, then it is a leap year. So 2000 is a leap year.

### Method 1: Leap Year Program in Python Using If Conditions

```year = int(input("Enter a Year:"))

if(year % 4) == 0:
if(year % 100) == 0:
if(year % 400) == 0:
print("{0} is a leap year". format(year))
else:
print("{0} is not a leap year". format(year))
else:
print("{0} is a leap year". format(year))
else:
print("{0} is not a leap year". format(year))
﻿```
##### Output:
``````Enter a Year:2000
2000 is a leap year``````

Video Explanation of Leap Year Program in Python

### Method 2: Pre-defined function to check leap year in Python

```#we are importing calender module to check leap year
import calendar

year = 2000
if (calendar.isleap(year)):
print("Leap Year")
else:
print("Not a Leap Year")```
##### Output:
``Leap Year``

#### Code Explanation Method 2: Pre-defined function to check leap year in Python

In the above code, we have used a predefined function of python. For this, we have imported the calendar module of python. In the next line, we have defined a year variable with the value 2000; you can even take the value of the year from user input.

Then we have an if condition where we will check if the year is a leap year or not. For this, we have used python’s predefined “isleap” function from the calendar module. The Isleap function accepts only one parameter, which is the year, and it will return a boolean result. Inside, if we have a print statement stating that year is a leap year else print, it is not a leap year. If you run this program, then the program will return. It is a leap year because 2000 is a leap year.

### Method 3: Check Leap year from Logical operator in a single line in Python

```year = 2000
if((((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0))):
print("Leap Year")
else:
print("Not a Leap Year")```
##### Output:
``Leap Year``

#### Code Explanation Method 3: Check Leap year from Logical operator in a single line in Python

In the above program, we have used the same logic as we have used in the first program, but here we have saved some lines of code; let’s see how. In the first line, we have stored the year into a variable called year. And in the next line, we have our logic using the logical operator.

If the statement we have checked is that the year is divisible by 4 and not by 100 or the year is divisible by 400, then it is a leap year. Else it is not a leap year. Run the program, and you will get a leap year.

### Why Are Leap Years Necessary?

Leap days keep our modern-day Gregorian calendar in alignment with Earth’s revolutions around the Sun. It takes Earth approximately 365.242189 days, or 365 days, 5 hours, 48 minutes, and 45 seconds, to circle once around the Sun. This is called a tropical year, and it starts on the March equinox. However, the Gregorian calendar has only 365 days in a year. If we didn’t add a leap day on February 29 almost every four years, each calendar year would begin about 6 hours before the Earth completes its revolution around the Sun (see illustration).

As a consequence, our time reckoning would slowly drift apart from the tropical year and get increasingly out of sync with the seasons. With a deviation of approximately 6 hours per year, the seasons would shift by about 24 calendar days within 100 years.

Allow this to happen for a while, and Northern Hemisphere dwellers will be celebrating Christmas in the middle of summer in a matter of a few centuries. Leap days fix that error by giving Earth the additional time it needs to complete a full circle around the Sun.

For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions