Pre-defined Function to Check Leap Year in Python

Pre-defined Function to Check Leap Year in Python
(Last Updated On: 19/09/2022)

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, the day is added to the end of the shortest month, February. The intercalary day, February 29, is commonly referred to as leap day.

Check Leap Year Using Pre-defined Function in Python

import calendar

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

Code Explanation: Check Leap Year Using Pre-defined Function

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 function “isleap” 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.

For all the practice Videos and Explanations on Python, please click over here. Python Practice Series.

Leave a Reply

Your email address will not be published. Required fields are marked *