用于检查闰年的 Python 程序
这个范例演示了python程序如何判断给定的年份是平年还是闰年
闰年正好可以被4整除,除了世纪年(以00结尾的年份)。世纪年只有在完全可以被400整除时才是闰年。例如
2017 is not a leap year
1900 is a not leap year
2012 is a leap year
2000 is a leap year
源码
# Python program to check if year is a leap year or not year = 2000 # To get year (integer input) from the user # year = int(input("Enter a year: ")) # divided by 100 means century year (ending with 00) # century year divided by 400 is leap year if (year % 400 == 0) and (year % 100 == 0): print("{0} is a leap year".format(year)) # not divided by 100 means not a century year # year divided by 4 is a leap year elif (year % 4 ==0) and (year % 100 != 0): print("{0} is a leap year".format(year)) # if not divided by both 400 (century year) and 4 (not century year) # year is not leap year else: print("{0} is not a leap year".format(year))
输出
2000 is a leap year
您可以更改源代码中的year的值,然后再次运行它以测试此程序。
相关文章