Latest web development tutorials

Python judgment leap year

Document Object Reference Examples Python3

The following examples are used to determine the user to enter the year is a leap year:

# -*- coding: UTF-8 -*-

# Filename : test.py
# author by : www.w3big.com

year = int(input("输入一个年份: "))
if (year % 4) == 0:
   if (year % 100) == 0:
       if (year % 400) == 0:
           print("{0} 是闰年".format(year))   # 整百年能被400整除的是闰年
       else:
           print("{0} 不是闰年".format(year))
   else:
       print("{0} 是闰年".format(year))       # 非整百年能被4整除的为闰年
else:
   print("{0} 不是闰年".format(year))

We can also use the embedded if statement to achieve:

Execute the above code output results:

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

Document Object Reference Examples Python3