日曆模塊允許輸出類似於程序的日曆,並提供與日曆相關的其他有用功能。 “日曆”模塊中定義的函數和類使用理想化的日曆,當前的公曆日曆在兩個方向上都無限期擴展。
在Python中,calendar.isleap()是日曆模塊中提供的用於簡單文本日曆的功能。
如果年份是a年,則使用isleap()方法獲取值True,否則返回False。
用法: isleap()
參數:
year: Year to be tested leap or not.
返回:Returns True if the year is a leap year, otherwise False.
代碼1:
# Python program to explain working of isleap() method
# importing calendar module
import calendar
# checking whether given year is leap or not
print(calendar.isleap(2016))
print(calendar.isleap(2001))
輸出:
True
False
代碼2:解釋工作isleap()方法。
如果給定年份為年,則下麵的代碼將打印第4個月的日曆,否則通知年份為非leap年。
# Python code to demonstrate the working of isleap()
# importing calendar module for calendar operations
import calendar
year = 2017
# calling isleap() method to verify
val = calendar.isleap(year)
# checking the condition is True or not
if val == True:
# print 4th month of given leap year
calendar.prmonth(year, 4, 2, 1)
# Returned False, year is not a leap
else:
print("% s is not a leap year" % year)
輸出:
2017 is not a leap year