Python calendar leapdays() Method with Example

Python calendar.leapdays() Method: In this tutorial, we will learn about the leapdays() method of calendar module in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 24, 2023

Python calendar.leapdays() Method

The calendar.leapdays() method is an inbuilt method of the calendar module, it returns the number of leap days between the two given years in the function argument.

Module

The following module is required to use leapdays() method:

import calendar

Syntax

The following is the syntax of leapdays() method:

leapdays(y1, y2)

Parameter(s)

The following are the parameter(s):

  • y1: It is a required parameter, which specifies the starting year from where the search should start
  • y2: right limit of the range, which specifies the ending year till where leap years should be searched.

Return Value

The return type of this function is an integer. The function returns the number of leap years in the given range between the two years.

Example of calendar.leapdays() Method in Python

# Python program to illustrate the 
# use of leapdays() method
  
# importing calendar module 
import calendar 
  
y1 = 2000
y2 = 2025
print("Number of leap years in the range:", calendar.leapdays(y1, y2))
print()

# years can be negative as well
y1 = -10
y2 = 1000
print("Number of leap years in the range:", calendar.leapdays(y1, y2))
print()

# Checking if the result is True
y1 = 2000
y2 = 2020
count = 0
for i in range(y1,y2):
    if calendar.isleap(i):
        count+=1
        print(i)

print("Leap year count through iterating:", count)
print("Leap year count through function:", calendar.leapdays(y1, y2))

Output

Number of leap years in the range: 7

Number of leap years in the range: 245

2000
2004
2008
2012
2016
Leap year count through iterating: 5
Leap year count through function: 5



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.