python calendar.isleap_Python calendar isleap()用法及代碼示例

日曆模塊允許輸出類似於程序的日曆,並提供與日曆相關的其他有用功能。 “日曆”模塊中定義的函數和類使用理想化的日曆,當前的公曆日曆在兩個方向上都無限期擴展。

在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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值