用python判断是否是闰年
时间: 2023-05-22 15:06:37 浏览: 55
判断是否是闰年的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
```
这个函数接收一个年份作为参数,返回一个布尔值表示该年份是否是闰年。如果是闰年,返回True;否则返回False。
相关问题
Python 判断是否是闰年 代码
以下是一个简单的 Python 判断是否是闰年的代码示例:
```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))
```
上述代码中,首先通过 `input` 函数获取用户输入的年份,然后使用 if 语句判断该年份是否为闰年。判断闰年的方法是,如果该年份能被4整除但不能被100整除,或者能被400整除,则为闰年。最后根据判断结果输出相应的提示信息。
Python判断是否为闰年
在 Python 中,判断一个年份是否为闰年的标准通常遵循以下规则:
1. 如果年份能被4整除但不能被100整除,那么它是闰年。
2. 但是如果年份能被400整除,那么它也是闰年。
这是 Python 中的常见实现:
```python
def is_leap(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 = 2000
print(f"{year} 是闰年: {is_leap(year)}") # 输出: True
year = 1900
print(f"{year} 是闰年: {is_leap(year)}") # 输出: False
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)