用python写一个关于求闰年的代码
时间: 2023-05-13 08:04:46 浏览: 66
以下是一个用 Python 编写的求闰年的代码:
```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(2020)) # True
print(is_leap_year(2021)) # False
```
希望这个代码能够帮到你!
相关问题
用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,则表示是闰年,否则为平年。
用python写一个求输入年份是否为闰年的代码
可以使用以下代码:
```python
year = int(input("请输入一个年份:"))
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
print(year, "是闰年")
else:
print(year, "不是闰年")
```
这个代码中,首先获取用户输入的年份,然后通过判断年份是否能被4整除,并且不能被100整除,或者能被400整除,来判断是否为闰年。如果是,则输出“是闰年”,否则输出“不是闰年”。