输入一个数字(年份,如2000年),定义函数,判断该年份是否是闰年Python
时间: 2024-03-12 08:49:38 浏览: 33
好的,我理解了你的问题。以下是判断一个年份是否是闰年的 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(表示是闰年)或 False(表示不是闰年)。例如,要判断 2000 年是否是闰年,可以这样调用函数:
```python
>>> is_leap_year(2000)
True
```
当然,你可以传递任何需要判断的年份作为参数,该函数都会正确地返回结果。
相关问题
编写一个函数,用以判断一个任意的年份是否为闰年,然后调用这个函数找出2000-3000之间的所有闰年。
好的,根据您的要求,我可以编写一个判断闰年的函数,并且调用该函数找出2000-3000之间的所有闰年。以下是代码实现。
```python
def is_leap_year(year):
"""
判断某一年是否为闰年
"""
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
return True
else:
return False
leap_years = []
for year in range(2000, 3001):
if is_leap_year(year):
leap_years.append(year)
print("2000-3000之间的闰年为:", leap_years)
```
上述代码中,我们先定义了一个函数 `is_leap_year(year)`,该函数接受年份作为参数,用来判断该年份是否为闰年。然后,我们通过 `for` 循环遍历从 2000 到 3000 年之间的每一年,调用 `is_leap_year(year)` 函数判断是否为闰年,若是则将该年份添加到 `leap_years` 列表中。最后,我们输出 `leap_years` 列表即可找出2000-3000年之间的所有闰年。
用python输出2000到2050年之间所有的闰年年份,每行打印5个
以下是代码示例:
```
# 定义函数,判断是否为闰年
def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
return True
else:
return False
# 循环打印闰年年份
count = 0
for year in range(2000, 2051):
if is_leap_year(year):
print(year, end='\t')
count += 1
if count % 5 == 0:
print('\n')
```
输出结果如下:
```
2000 2004 2008 2012 2016
2020 2024 2028 2032 2036
2040 2044 2048
```