Fibonacci terms up to n terms

EasyFunction Programs · 20 of 39

Write a Python function that returns the first n Fibonacci numbers starting 1, 1.

Advertisement
Input
9
Output
1 1 2 3 5 8 13 21 34

Approach

  1. Build a list in a loop.

Solution

Python
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
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More function programs

← All 39 Function Programs