如何使用 Python 检查闰年?
闰年是四年才出现一次。一般年份,如果它能被 4 整除,则称作闰年,而百年份,它应该能被 400 整除。以下 Python 程序演示了如何判断年份是否为闰年
示例
yr=int(input('enter year')) if yr%100==0: #century year if yr%400==0: print ('{} is leap year'.format(yr)) else: print ('{} is not leap year'.format(yr)) else: if yr%4==0: print ('{} is leap year'.format(yr)) else: print ('{} is not leap year'.format(yr))
输出
enter year2012 2012 is leap year enter year2018 2018 is not leap year 2000 is leap year enter year1900 1900 is not leap year
广告