Python Practice › Function Programs › Program 20
Fibonacci terms up to n terms
Write a Python function that returns the first n Fibonacci numbers starting 1, 1.
Input
9
Output
1 1 2 3 5 8 13 21 34
Approach
- Build a list in a loop.
Solution
def fibonacci(n): series = [] a, b = 1, 1 for _ in range(n): series.append(a) a, b = b, a + b return series print(*fibonacci(9))Output
1 1 2 3 5 8 13 21 34
Learn the concept
New to this topic? Read the lesson, then come back.