python判断是否闰年
时间: 2023-09-27 09:09:15 浏览: 100
Python可以通过判断年份是否能被4整除并且不能被100整除,或者能被400整除来判断是否是闰年。以下是几种不同的Python代码实现方法:
方法一:
```python
year = int(input("请输入一个年份:"))
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print(year, "是闰年")
else:
print(year, "不是闰年")
```
方法二:
```python
year = int(input("输入一个年份:"))
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} 是闰年".format(year))
else:
print("{0} 不是闰年".format(year))
else:
print("{0} 是闰年".format(year))
else:
print("{0} 不是闰年".format(year))
```
方法三(简洁优化):
```python
year = int(input("请输入一个年份:"))
if (year % 4) == 0 and (year % 100) != 0 or (year % 400) == 0:
print("{0}是闰年".format(year))
else:
print("{0}不是闰年".format(year))
```
以上是三种不同的Python代码来判断一个年份是否是闰年。你可以根据自己的喜好选择其中的一种方法来使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [python实现判断闺年完整代码](https://blog.csdn.net/qq_50942093/article/details/131295287)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [python--判断闰年](https://blog.csdn.net/weixin_55821558/article/details/124529766)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文