python用逻辑表达式判断是否为闰年
时间: 2023-09-27 13:09:04 浏览: 63
闰年的判断规则为:每四年一闰,每百年不闰,四百年又闰。
以下是Python实现代码:
```python
def is_leap_year(year):
"""
判断是否为闰年
"""
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
```
使用方法:
```python
>>> is_leap_year(2020)
True
>>> is_leap_year(2000)
True
>>> is_leap_year(1900)
False
>>> is_leap_year(2021)
False
```
相关问题
用python写出判断年份是否为闰年的代码
当然可以!以下是一个简单的 Python 代码来判断一个年份是否为闰年:
```python
def is_leap_year(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False
year = int(input("请输入一个年份:"))
if is_leap_year(year):
print(year, "是闰年")
else:
print(year, "不是闰年")
```
你可以将要判断的年份作为输入,然后运行这段代码来判断该年份是否为闰年。代码中的函数 `is_leap_year()` 接受一个年份作为参数,并返回 True 或 False 来表示是否为闰年。
用Python实现 判断是否为闰年
判断是否为闰年的Python代码如下所示:
```
def is_leap_year(year):
if year % 4 != 0:
return False
elif year % 100 != 0:
return True
elif year % 400 != 0:
return False
else:
return True
```
可以调用这个函数,并传入一个年份,它会返回一个布尔值,表示这个年份是否为闰年。