检查给定年份是否为a年的Python程序

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值