用python实现判断闰年
时间: 2023-09-18 17:13:18 浏览: 66
可以使用以下代码实现判断闰年的功能:
```python
def is_leap_year(year):
"""
判断是否为闰年
:param year: 年份
:return: True or False
"""
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
return True
else:
return False
```
使用方法:
```python
print(is_leap_year(2000)) # True
print(is_leap_year(2004)) # True
print(is_leap_year(2100)) # False
print(is_leap_year(2021)) # False
```
其中,判断闰年的规则为:
1. 能被4整除但不能被100整除的年份是闰年。
2. 能被400整除的是闰年。
因此,可以使用上述代码来判断一个年份是否为闰年。
相关问题
用python实现判断是不是闰年
你好,根据我的算法,以下是判断是否为闰年的 Python 代码:
```python
def is_leap_year(year):
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
return True
else:
return False
```
你可以输入年份,然后调用这个函数来判断是否为闰年。
Python实现闰年判断
判断闰年的方法通常有两种:
1. 能被4整除但不能被100整除的年份为闰年(如2004年就是闰年);
2. 能被400整除的年份也是闰年(如2000年是闰年,1900年不是闰年)。
以下是Python实现闰年判断的代码示例:
```python
def is_leap_year(year):
"""判断是否为闰年"""
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
return True
else:
return False
```
这里定义了一个判断是否为闰年的函数,传入一个年份参数,返回一个布尔值,True表示是闰年,False表示不是闰年。