日曆模塊允許輸出類似於程序的日曆,並提供與日曆相關的其他有用函數。 “日曆”模塊中定義的函數和類使用理想化的日曆,當前的公曆日曆在兩個方向上都無限期擴展。
在Python中,calendar.leapdays()是日曆模塊中提供的用於簡單文本日曆的函數。
leapdays()方法用於獲取指定年份範圍內的of年數。
用法: leapdays() 參數: year1, year2: years to get the number of leap years. 返回:Returns number of leap years in a specified range.
代碼1:
# Python program to explain working of leapdays() method
# importing calendar module
import calendar
# checking number of leap years in range
print(calendar.leapdays(2016, 2022))
print(calendar.leapdays(2001, 2003))
輸出:
2 0
代碼2:解釋工作leapdays()
方法。
如果在給定範圍內找到任何leap年,則以下代碼將打印leap年數和上一last年的1st-month日曆,否則通知沒有年份為is年。
# Python code to demonstrate the working of leapdays()
# importing calendar module for calendar operations
import calendar
year1 = 2005
year2 = 2025
# calling leapdays() method to verify
val = str(calendar.leapdays(year1, year2))
print("Number of leap years found is % s" % val)
count = 0
# checking the condition is True or not
for year in range(year1, year2):
val = calendar.isleap(year)
if val == True:
lyear = year
count += 1
if count >= 1:
# print 1th month of first leap year
calendar.prmonth(lyear, 1, 2, 1)
# Returned False, year is not a leap
else:
print("No leap year found")
輸出:
Number of leap years found is 5 January 2024 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
相關用法
- Python calendar isleap()用法及代碼示例
- Python calendar firstweekday()用法及代碼示例
- Python calendar setfirstweekday()用法及代碼示例
- Python calendar itermonthdays()用法及代碼示例
- Python calendar iterweekdays()用法及代碼示例
- Python calendar itermonthdays2()用法及代碼示例
- Python calendar monthdatescalendar()用法及代碼示例
- Python calendar yeardayscalendar()用法及代碼示例
- Python calendar yeardays2calendar()用法及代碼示例
- Python calendar yeardatescalendar()用法及代碼示例
- Python calendar monthdayscalendar()用法及代碼示例
- Python calendar monthdays2calendar()用法及代碼示例
- Python calendar pryear()用法及代碼示例
- Python calendar prmonth()用法及代碼示例
- Python calendar formatyear()用法及代碼示例
注:本文由純淨天空篩選整理自Shivam_k大神的英文原創作品 Python calendar module | leapdays() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。