Python Leap Year Determination

Document 对象参考手册Python3 Examples

The following example is used to determine whether the year entered by the user is a leap year:

Example (Python 3.0+)

# -*- coding: UTF-8 -*- # Filename : test.py # author by : www.example.com year = int(input("Enter a year:")) if (year % 4) == 0: if (year % 100) == 0: if (year % 400) == 0: print("{0} is a leap year".format(year)) # Century years divisible by 400 are leap years else: print("{0} is not a leap year".format(year)) else: print("{0} is a leap year".format(year)) # Non-century years divisible by 4 are leap years else: print("{0} is not a leap year".format(year))

We can also use nested if statements to implement it:

The output after executing the above code is:

输入一个年份: 2000
2000 是闰年
输入一个年份: 2011
2011 不是闰年

Document 对象参考手册Python3 Examples

Other Extensions