當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python calendar leapdays()用法及代碼示例


Python calendar.leapdays() 方法

leapdays() 方法是 Python 中日曆模塊的內置方法。它適用於簡單的文本日曆,並在函數參數中返回兩個給定年份之間的閏日數。

模塊:

    import calendar

用法:

    leapdays(y1, y2)

參數:

  • y1:它是一個必需參數,它指定搜索應該從哪裏開始的起始年份
  • y2:right 範圍的限製,指定結束年份直到應該搜索閏年的地方。

返回值:

這個函數的返回類型是一個整數。該函數返回兩年之間給定範圍內的閏年數。

例:

# 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))

輸出

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


相關用法


注:本文由純淨天空篩選整理自 Python calendar Module | leapdays() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。