python判断闰年函数 python判断闰年的条件

python编程求2000到2500闰年1、定义判断闰年函数
def isLeapYear(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print(year)
else:
pass
else:
print(year)
else:
pass
2、调用函数打印
for i in range(2000, 2501):
isLeapYear(i)
3、运行结果
2000
2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048
2052
2056
2060
2064
2068
2072
2076
2080
2084
2088
2092
2096
2104
2108
2112
2116
2120
2124
【python判断闰年函数 python判断闰年的条件】2128
2132
2136
2140
2144
2148
2152
2156
2160
2164
2168
2172
2176
2180
2184
2188
2192
2196
2204
2208
2212
2216
2220
2224
2228
2232
2236
2240
2244
2248
2252
2256
2260
2264
2268
2272
2276
2280
2284
2288
2292
2296
2304
2308
2312
2316
2320
2324
2328
2332
2336
2340
2344
2348
2352
2356
2360
2364
2368
2372
2376
2380
2384
2388
2392
2396
2400
2404
2408
2412
2416
2420
2424
2428
2432
2436
2440
2444
2448
2452
2456
2460
2464
2468
2472
2476
2480
2484
2488
2492
2496
2500
python1900~2031年,输出每年的天干、地支,判断是否闰年并统计闰年的个数?# 定义天干和地支的列表tiangan = ["甲","乙","丙","丁","戊","己","庚","辛","壬","癸"]
dizhi = ["子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"]# 定义一个函数,判断是否为闰年def is_leap_year(year):if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:return True
else:return False# 初始化闰年的个数为零leap_count = 0# 遍历1900到2031年,输出每年的天干地支和是否为闰年for year in range(1900,2032):# 计算天干和地支的索引,注意要减去1,因为列表从零开始
tiangan_index = (year - 1900) % 10 - 1
dizhi_index = (year - 1900) % 12 - 1
# 输出每年的天干地支
print(f"{year}年是{tiangan[tiangan_index]}{dizhi[dizhi_index]}年", end=" ")
# 判断是否为闰年,并输出结果
if is_leap_year(year):print("是闰年")# 累加闰年的个数
leap_count += 1
else:print("不是闰年")# 输出总共有多少个闰年print(f"从1900到2031年,一共有{leap_count}个闰年")
python判断闰年用Python判断是否是闰年python判断闰年函数的三种方法python判断闰年函数:
本教程操作环境:windows7系统、python3.9版python判断闰年函数,DELL G3电脑 。
1、以下实例可以判断用户输入python判断闰年函数的年份是否为闰年:
2、也可以使用内嵌if语句来实现:
执行以上代码输出结果为:
3、其实Python的calendar库中已经封装好了一个方法isleap()来实现这个判断是否为闰年:
根据用户输入判断:
关于python判断闰年函数和python判断闰年的条件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。

    推荐阅读