日历模块允许输出类似于程序的日历,并提供与日历相关的其他有用功能。 “日历”模块中定义的函数和类使用理想化的日历,当前的公历日历在两个方向上都无限期扩展。
在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