Python Practice › Loop Programs › Program 56

Fibonacci numbers up to 55

EasyLoop Programs · 56 of 90

Write a Python program to print the first 11 Fibonacci numbers (0 to 55).

Advertisement
Output
0 1 1 2 3 5 8 13 21 34 55

Approach

  1. Build a list starting [0, 1] and append the sum of the last two.

Solution

Python
fib = [0, 1]while len(fib) < 11:    fib.append(fib[-1] + fib[-2])print(*fib)
Output
0 1 1 2 3 5 8 13 21 34 55

Line by line

Line 3

fib[-1] and fib[-2] are the last two values.

Line 4

*fib passes the items separately, so they print with spaces.

Advertisement

Learn the concept

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

More loop programs

← All 90 Loop Programs