Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Leap Year Check

Python Leap Year Check

Document Object Reference Manual Python3 Examples

The following example determines whether a year entered by the user is a leap year:

Example (Python 3.0+)

-- coding: UTF-8 --

Filename : test.py

author by : www.runoob.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

)

)

A century year divisible by 400 is a leap year

else

:

print

(

"

{0} is not a leap year

"

.

format

(

year

)

)

else

:

print

(

"

{0} is a leap year

"

.

format

(

year

)

)

A non-century year divisible by 4 is a leap year

else

:

print

(

"

{0} is not a leap year

"

.

format

(

year

)

)

We can also implement this with nested if statements:

The output of the code above is:


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

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

Document Object Reference Manual Python3 Examples