Python Program for calendar leapdays() Method with Examples

Calendar Module:

The calendar module allows you to output calendars like a program and includes extra calendar-related operations. Calendar module functions and classes make use of an idealized calendar, the current Gregorian calendar extended in both directions indefinitely.

leapdays() Method:

calendar.leapdays() is a function in Python’s calendar module for creating simple text calendars.

The leapdays() method returns the number of leap years in a given range of years.
This function works for time spans of a century change.

Syntax:

leapdays(year1, year2)

Parameter Values: 

year1, year2: These are required. They are numbers. They are the years to get the number of leap years(range).

Return Value: The number of leap years in a given range is returned.

Examples:

Example1:

Input:

Given lower limit year = 2013
Given upper limit year = 2025

Output:

The number of leap years in a given range{ 2013 , 2025 } = 3

Example2:

Input:

Given lower limit year = 1999
Given upper limit year = 2002

Output:

The number of leap years in a given range{ 1999 , 2002 } = 1

Program for calendar leapdays() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import calendar module using the import keyword.
  • Give the lower limit year as static input and store it in a variable.
  • Give the upper limit year as static input and store it in another variable.
  • Pass the given lower and upper limits as the arguments to the leapdays() function to get the number of leap years in a given range.
  • Store it in another variable.
  • Print the number of leap years in a given range.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the lower limit year as static input and store it in a variable.
gvn_yr1 = 2013
# Give the upper limit year as static input and store it in another variable.
gvn_yr2 = 2025
# Pass the given lower and upper limits as the arguments to the leapdays() function
# to get the number of leap years in a given range.
# Store it in another variable.
rslt = calendar.leapdays(gvn_yr1, gvn_yr2)
# Print the number of leap years in a given range.
print(
    "The number of leap years in a given range{", gvn_yr1, ",", gvn_yr2, "} = ", rslt)

Output:

The number of leap years in a given range{ 2013 , 2025 } = 3

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import calendar module using the import keyword.
  • Give the lower limit year as user input using the int(input()) function and store it in a variable.
  • Give the upper limit year as user input using the int(input()) function and store it in another variable.
  • Pass the given lower and upper limits as the arguments to the leapdays() function to get the number of leap years in a given range.
  • Store it in another variable.
  • Print the number of leap years in a given range.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the lower limit year as user input using the int(input()) function and store it in a variable.
gvn_yr1 = int(input("Enter some random year = "))
# Give the upper limit year as user input using the int(input()) function and store it another variable.
gvn_yr2 = int(input("Enter some random year = "))
# Pass the given lower and upper limits as the arguments to the leapdays() function
# to get the number of leap years in a given range.
# Store it in another variable.
rslt = calendar.leapdays(gvn_yr1, gvn_yr2)
# Print the number of leap years in a given range.
print(
    "The number of leap years in a given range{", gvn_yr1, ",", gvn_yr2, "} = ", rslt)

Output:

Enter some random year = 1999
Enter some random year = 2002
The number of leap years in a given range{ 1999 , 2002 } = 1