def is_leap_year(year):
"""
判断给定的年份是否为闰年。
闰年的判断规则:
1. 如果年份能被400整除,则是闰年;
2. 如果年份能被100整除但不能被400整除,则不是闰年;
3. 如果年份能被4整除但不能被100整除,则是闰年;
4. 其他情况都不是闰年。
参数:
year -- 需要判断的年份 (int)
返回:
True -- 如果是闰年
False -- 如果不是闰年
"""
if (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0):
return True
else:
return False
# 示例用法
year = 2020
if is_leap_year(year):
print(f"{year} 是闰年")
else:
print(f"{year} 不是闰年")
解释说明:
is_leap_year
函数用于判断给定的年份是否为闰年。True
或 False
。上一篇:python jsonpath
下一篇:celery python
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站