python判断闰年

Python012

python判断闰年,第1张

用Python判断是否是闰年的三种方法:

本教程操作环境:windows7系统、python3.9版,DELL G3电脑。

1、以下实例可以判断用户输入的年份是否为闰年

2、也可以使用内嵌if语句来实现

执行以上代码输出结果为:

3、其实Python的calendar库中已经封装好了一个方法isleap()来实现这个判断是否为闰年:

根据用户输入判断:

def year (y)

If y % 100 = 0 and y % 400 = 0 :

Print " y , 是闰年"

Elif y %100 != 0 and y % 4 = 0 :

Print " y , "是闰年"

Else :

Print " y , "不是闰年"

Return

year ( raw_ input ("请输入年份:" )。