当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python calendar isleap()用法及代码示例


Python calendar.isleap() 方法

isleap() 方法是 Python 中日历模块的内置方法。它适用于简单的文本日历并检查给定的年份是否为闰年。如果年份是闰年则返回真,否则返回假。

模块:

    import calendar

用法:

    isleap(year)

参数:

  • year: 要检查的年份

返回值:

该函数根据年份是否为闰年返回真或假。

例:

# Python program to illustrate the 
# use of isleap() method
  
# importing calendar module 
import calendar 
  
print("Is year 2020 a leap year:", calendar.isleap(2020))
print()

print("Is year 1993 a leap year:", calendar.isleap(1993))
print()

# Print leap if year is a leap year else print not leap
x = calendar.isleap(1234)
if x == True:
    print("leap")
else:
    print("not leap")

输出

Is year 2020 a leap year:True

Is year 1993 a leap year:False

not leap

例:

# Python program to illustrate the 
# use of isleap() method

# Printing leap years between a range  

# importing calendar module 
import calendar 

print("Leap years between 1900 to 2000...")  
for year in range(1900, 2000+1):
  if calendar.isleap(year):
    print(year)

输出

Leap years between 1900 to 2000...
1904
1908
1912
1916
1920
1924
1928
1932
1936
1940
1944
1948
1952
1956
1960
1964
1968
1972
1976
1980
1984
1988
1992
1996
2000


相关用法


注:本文由纯净天空筛选整理自 Python calendar Module | isleap() Method with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。