python使用 while 循环输出 2000 年到 2100 年之间的闰年 闰年:400 的倍数 或者 是 4 的倍数不是 100 的倍数
时间: 2024-02-11 21:05:47 浏览: 38
好的,我来给你写一个示例代码:
```python
year = 2000
while year <= 2100:
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
print(year)
year += 1
```
代码中,我们使用 `while` 循环遍历 2000 年至 2100 年之间的所有年份。通过判断一个年份是否为闰年,来决定是否输出该年份。其中,判断闰年的规则是:能被 400 整除的年份,或者能被 4 整除但不能被 100 整除的年份,都是闰年。
相关问题
python使用 while 循环输出 2000 年到 2100 年之间的闰年 闰年:400 的倍数 或者 是 4 的倍数不是 100 的'倍数
好的,那我来为您演示一下相应的代码:
```python
year = 2000
while year <= 2100:
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
print(year)
year += 1
```
代码中,我们使用 `while` 循环遍历 2000 年至 2100 年之间的所有年份。通过判断一个年份是否为闰年,来决定是否输出该年份。其中,判断闰年的规则是:能被 400 整除的年份,或者能被 4 整除但不能被 100 整除的年份,都是闰年。
python输出公元2000年到3000年之间的闰年
Python可以通过以下代码输出公元2000年到3000年之间的闰年:
```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
leap_years = []
for year in range(2000, 3001):
if is_leap_year(year):
leap_years.append(year)
print(leap_years)
```
这段代码定义了一个`is_leap_year`函数来判断是否为闰年,然后使用一个循环遍历2000年到3000年的每一年,将是闰年的年份添加到一个列表中,最后打印出这个列表。