How to get the total number of leap days in the years within range in Python?



In this article we will discuss how to find the number of leap years in a specified ranges of year in python.

leapdays()

The calendar module offers more helpful calendar-related tasks in addition to allowing calendars to be produced like programmes. The idealised calendar used by the functions and classes defined in the Calendar module is the present Gregorian calendar stretched indefinitely in both directions.

For simple text calendars, the calendar module in Python provides the function calendar.leapdays().

Syntax

the syntax of the leapdays() method is as follows.

leapdays()

The above function takes two parameters; they are −

  • year1: This represents the initial year for the range.
  • year2: This represents the final year or the end of the range.

Returns

The leapdays() function takes the above parameters and return the number of leap year in a specified range of years.

Example

In the following example code, we find the number of leap days in the specified range of years is achieved using the leapdays() function which is provided by the calender module in python.

import calendar print("The number of leapdays in the range of 2011 to 2030 is:") print(calendar.leapdays(2011, 2030)) print("The number of leapdays in the range of 2001 to 2100 is:") print(calendar.leapdays(2001, 2100))

Output

The output of the above code is as follows.

The number of leapdays in the range of 2011 to 2030 is:
5
The number of leapdays in the range of 2001 to 2100 is:
24

Advertisements