當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。