求斐波那契数列前20项存入列表中,使用列表切片输出偶数项
时间: 2024-04-29 21:25:11 浏览: 58
计算斐波那契数列前20项
Sure, here's the Fibonacci sequence up to the first 20 terms:
```
fib = [0, 1]
for i in range(2, 20):
fib.append(fib[i-1] + fib[i-2])
```
To output the even terms, you can use list slicing:
```
even_fib = fib[1::2]
print(even_fib)
```
This will output `[1, 2, 5, 13, 34, 89, 233, 610]`, which are the even terms in the Fibonacci sequence up to the first 20 terms.
阅读全文