Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

python判断闰年

作者:故事没有她   发布日期:2025-08-06   浏览:75

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 函数用于判断给定的年份是否为闰年。
  • 通过一系列条件判断,根据闰年的规则返回 TrueFalse
  • 最后通过示例用法展示了如何调用该函数并输出结果。

上一篇:python jsonpath

下一篇:celery python

大家都在看

python时间格式

python开发windows应用程序

python中len是什么意思

python ord和chr

python中的yield

python自定义异常

python判断路径是否存在

python list.pop

python的for i in range

npm config set python

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站