A leap year is a year that is completely divisible by 4 except the century year (a year that ended with 00). A century year is a leap year if it is divisible by 400. Here, a year is provided by the user and we have to check whether the given year is a leap year or not. This problem, we will solve in two ways first by using the calendar module and second by simply checking the leap year condition.
year年是除世纪年份(以00结尾的年份)外可完全除以4的年份。 如果世纪年可以被400整除,则它是a年。在此,用户提供了年份,我们必须检查给定的年份是否是is年。 这个问题,我们将以两种方式解决,一种是通过使用日历模块,另一种是通过简单地检查the年条件。
1)使用日历模块 (1) By using the calendar module)
Before going to solve the problem, initially, we learn a little bit about the calendar module. Calendar module is inbuilt in Python which provides us various functions to solve the problem related to date, month and year.
在开始解决该问题之前,我们首先了解一下日历模块。 Calendar模块内置于Python中,它为我们提供了各种功能来解决与日期,月份和年份有关的问题。
Program:
程序:
# importing the module
import calendar
# input the year
year=int(input('Enter the value of year: '))
leap_year=calendar.isleap(year)
# checking leap year
if leap_year: # to check condition
print('The given year is a leap year.')
else:
print('The given year is a non-leap year.')
Output
输出量
RUN 1:
Enter the value of year: 2020
The given year is a leap year.
RUN 2:
Enter the value of year: 2021
The given year is a non-leap year.
2)只需检查the年状况 (2) By simply checking the leap year condition)
As we know the condition to check the given year is a leap year or not. So, here we will implement the condition and try to write the Python program.
众所周知,检查给定年份的条件是否为a年。 因此,这里我们将实现条件并尝试编写Python程序。
Program:
程序:
# input the year
y=int(input('Enter the value of year: '))
# To check for non century year
if y%400==0 or y%4==0 and y%100!=0:
print('The given year is a leap year.')
else:
print('The given year is a non-leap year.')
Output
输出量
RUN 1:
Enter the value of year: 2020
The given year is a leap year.
RUN 2:
Enter the value of year: 2000
The given year is a leap year.
翻译自: https://www.includehelp.com/python/check-the-given-year-is-a-leap-year-or-not.aspx