The Fibonacci series is a sequence where each number is the sum of the two numbers before it. It starts with 0 and 1: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 and so on. In this lesson you will print the first n Fibonacci numbers, print all Fibonacci numbers up to a limit, and see a recursive version.

Run the code in our online Python compiler. The compiler does not take keyboard input, so the examples use fixed values.

 

Program 1: first n terms using a loop

n = 10
a, b = 0, 1
series = []

for _ in range(n):
    series.append(a)
    a, b = b, a + b

print(series)

Output:

[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

How it works

a and b hold two consecutive Fibonacci numbers. In each round, the program saves a, then moves forward with a, b = b, a + b. This line assigns both values at the same time: the new a becomes the old b, and the new b becomes the sum of the old a and b.

Round

Saved value (a)

New a

New b

Start

 

0

1

1

0

1

1

2

1

1

2

3

1

2

3

4

2

3

5

The saved values (0, 1, 1, 2, …) are the Fibonacci numbers.

 

Program 2: all Fibonacci numbers up to a limit

limit = 100
a, b = 0, 1
series = []

while a <= limit:
    series.append(a)
    a, b = b, a + b

print(series)

Output:

[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

Use a while loop when you know the largest value you want, and a for loop when you know how many terms you want.

 

Program 3: using recursion

def fib(n):
    if n < 2:
        return n
    return fib(n - 1) + fib(n - 2)

print([fib(i) for i in range(10)])

Output:

[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

fib(n) is defined as fib(n – 1) + fib(n – 2), with fib(0) = 0 and fib(1) = 1 as the base cases. This version is easy to read, but it repeats the same calculations many times, so it becomes very slow for large n. Use the loop version for big values.

 

Loop or recursion?

Method

Speed

Best for

Loop

Fast

Real programs and large n

Recursion

Slow for large n

Learning how recursion works

 

Take the number of terms from the user (on your computer)

n = int(input('How many terms? '))

Then use n in Program 1. Our online compiler does not support input() yet, so use a fixed value there.

 

Common mistakes

  • Updating a and b one after the other. Writing a = b and then b = a + b gives wrong values, because a has already changed. Use a, b = b, a + b.
  • Starting from 1, 1. Some books start the series at 1, 1. This lesson starts at 0, 1, so check which one your syllabus wants.
  • Using the recursive version for large n. It can take a very long time.

 

Practice questions

  1. Print the first 15 Fibonacci numbers.
  2. Print only the even Fibonacci numbers below 1,000.
  3. Find the sum of the first 10 Fibonacci numbers.
  4. Check whether a given number is a Fibonacci number.

 

FAQs

What are the first 10 Fibonacci numbers?

0, 1, 1, 2, 3, 5, 8, 13, 21 and 34.

Does the Fibonacci series start at 0 or 1?

Both are used. This lesson starts at 0, 1, which is the most common form in programming courses.

Which is faster, the loop or the recursive version?

The loop. The simple recursive version repeats work and slows down quickly as n grows.