📜  Python日历模块 | isleap() 方法

📅  最后修改于: 2022-05-13 01:54:56.045000             🧑  作者: Mango

Python日历模块 | isleap() 方法

日历模块允许像程序一样输出日历,并提供与日历相关的附加有用功能。 Calendar 模块中定义的函数和类使用理想化的日历,当前的公历在两个方向上无限扩展。

在Python中, calendar.isleap()是 calendar 模块中提供的用于简单文本日历的函数。

如果年份是闰年, isleap()方法用于获取值 True,否则为 False。

Syntax: isleap()
Parameter: 
year: Year to be tested leap or not.

Returns: 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 个月的日历,否则通知年份不是闰年。

# 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