Python Leap Year Check
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
:
(
"
{0} is a leap year
"
.
format
(
year
)
)
A century year divisible by 400 is a leap year
else
:
(
"
{0} is not a leap year
"
.
format
(
year
)
)
else
:
(
"
{0} is a leap year
"
.
format
(
year
)
)
A non-century year divisible by 4 is a leap year
else
:
(
"
{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 不是闰年
Python3 Examples