python判断闰年def
时间: 2023-10-08 16:13:41 浏览: 50
可以使用以下代码来判断闰年:
```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`。判断闰年的规则是:能被4整除但不能被100整除的年份为普通闰年,能被400整除的为世纪闰年。
相关问题
python判断闰年
判断闰年的方法有多种,以下是其中两种方法:
方法一:使用年份除以4、100、400的余数规则来判断是否为闰年。
```python
def is_leap_year(year):
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
return True
else:
return False
```
方法二:使用calendar模块中的isleap()函数判断是否为闰年。
```python
import calendar
def is_leap_year(year):
return calendar.isleap(year)
```
使用示例:
```python
year = int(input("请输入年份:"))
if is_leap_year(year):
print("{}年是闰年".format(year))
else:
print("{}年不是闰年".format(year))
```
Python 判断闰年
判断闰年的方法如下:
```python
def is_leap_year(year):
"""
判断是否为闰年
:param year: 年份,整数
:return: 是闰年返回 True,否则返回 False
"""
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
return True
else:
return False
```
其中,满足以下条件之一的年份为闰年:
- 能被 4 整除但不能被 100 整除;
- 能被 400 整除。
使用示例:
```python
year = 2020
if is_leap_year(year):
print(f"{year}年是闰年")
else:
print(f"{year}年不是闰年")
```
输出:
```
2020年是闰年
```