Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Python Program to Check Leap Year

Last Updated on May 12, 2023 by Prepbytes

To align the calendar year with the solar year, an extra day, or February 29, is added to the year every four years. It is well known that this year was a leap year. For many Python programming programs that work with dates and timers, checking for a leap year is essential. We will discuss the leap year program in Python using if else for better logic. If a year is a leap year, it must meet a variety of criteria, such as being divisible by 4, 100, and 400, as well as specific requirements for different calendar systems. The checking for leap years can be implemented by utilizing if-else statements or other logical constructs. Accurately determining leap years is a crucial aspect of time calculation that is necessary for many programs and applications.

## What is a Leap Year?

A leap year is a year that has 366 days. For aligning the calendar year with the solar year, one extra day is added to this year. This extra day falls in the month of February, giving it 29 days instead of 28.

### Mathematical Conditions for Leap Year

The following equations must be true for the leap year:

• The year must divide evenly by 400.
• The year should divide evenly by 4, but not by 100.

### Examples of Leap Year

1. 1947 is a leap year or not
Since 1947 cannot be divided by either 4 or 400, it is not a leap year. Both of the above-mentioned mathematical prerequisites for a leap year are being disregarded by it.

2. 1900 is a leap year or not
1900 can be divided by 4, 100, but not 400. 1900 is not a leap year because it can be divided by 4 and 100. The requirement for a leap year is that the year must be divisible by 400 or by 4 but not by 100.

3. 2000 is a leap year or not
Since 2000 is divisible by 400, it is a leap year. It satisfies the first of the mathematical prerequisites for the leap year section, as was stated above.

4. 2004 is a leap year or not
Since 2004 is divisible by 4 but not by 100, it is a leap year. It satisfies the second of the mathematical conditions for the section on leap years that was mentioned before.

## Python Program to Check Leap Year

The following are different method to create leap year program in Python

### 1. Leap Year Program in Python using Calendar Module

The Calendar module offers extra calendar-related functions in addition to allowing you to output calendars similar to how the Unix cal program does. The start of the week is Monday, and the week ends on Sunday. Monday’s weekly index is 0, Tuesday’s is 1, and Wednesday’s is 2. Thursdays are three, Fridays are four, Saturdays are five, and Sundays are six. Using the setfirstweekday(integer) method, we may also change the first day of the week. The integer in this case represents the day’s weekly index.

In the leap year program of python we will use the isleap() method of the calendar module.

Syntax:

``isleap(year)``

isleap() method returns the boolean value depending on whether the year is leap year or not. If the year is leap year then isleap() method will return True else False.

Code for leap year program in python using calendar module

```def checkLeapYear(year):
#importing the calendar module
import calendar
#returning boolean value based on given year is leap year or not
return(calendar.isleap(year))

# Driver Code for the leap year program in python
#taking input (year) from the user
year = int(input("Enter the year: "))
if (checkLeapYear(year)):
#if checkLeapYear function returns true
print("Leap Year")
else:
#if checkLeapYear function returns false
print("Not a Leap Year")
```

OUTPUT:

``````Enter the year: 2000
Leap Year
Enter the year: 1900
Not a Leap Year``````

### 2. Leap Year Program in Python using If-else Statement

1. Take a year as input from the user.
2. Check if the year is divisible by 400, if yes return true else go to step 3.
3. Check if the year is divisible by 4, if yes go to step 4, else step 5.
4. Check if the year is divisible by 100, if no return true, else go to step 5.
5. Return false.

Flowchart for a Leap Year Program in Python
Below is the step-by-step work of the leap year program in Python.

Source Code

```def checkLeapYear(year):
if year%400==0:
# if year is divisible by 400, return True
return True
elif year%4==0:
# if year is divisible by 4,then check is it divisible by 100 or not
if year%100!=0:
#if year is not divisible by 100, then return True
return True
else:
#if year is divisible by 100, then return False
return False;

# Driver Code for the leap year program in python
#taking input (year) from the user
year = int(input("Enter the year: "))
if (checkLeapYear(year)):
#if checkLeapYear function returns true
print("Leap Year")
else:
#if checkLeapYear function returns false
print("Not a Leap Year")
```

OUTPUT:

``````Enter the year: 2000
Leap Year
Enter the year: 1900
Not a Leap Year``````

Summary

• A leap year is a year with 366 days having one extra day in February month making it a month of 29 days.
• The mathematical condition for a year to be a leap year is it should get divisible by 400 or it should get divisible by 4 but not by 100.
• We can write a leap year program in Python in two ways, one using calendar modules’ leap method and one by the simple if-else statement.
• Besides this, there are plenty of approaches to write a program to check leap year in Python. You have to practice more to figure out that.

## FAQ Related to Python Program to Check Leap Year

Q1. Does the Python programming language include a built-in function to check for leap years?
Ans. Python does not have an in-built function to check whether a year is a leap or not.

Q2. Is 2032 a leap year?
Ans. When dividing the number 2038 by 4, we obtain 508 as quotient and 0 as the remainder. Thus, it is perfectly divisible by 4, making it a leap year.

Q3. What is the list of leap years?
Ans. The complete list of leap years in the first half of the 21st century is therefore 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, and 2048.