Python Practice › Basic Programs › Program 25

Fibonacci series up to 50

EasyBasic Programs · 25 of 40

Write a Python program to print the Fibonacci series between 0 and 50. Each number is the sum of the two before it.

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

Approach

  1. Start with a = 0 and b = 1.
  2. Print a, then move forward with a, b = b, a + b until a passes 50.

Solution

Python
a, b = 0, 1series = []while a <= 50:    series.append(a)    a, b = b, a + bprint(series)
Output
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Line by line

Line 1

The series starts with 0 and 1.

Line 3

The loop stops as soon as the next number would be above 50.

Line 5

Both variables update at once: the new a is the old b, and the new b is the sum of the old pair.

Advertisement

Learn the concept

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

More basic programs

← All 40 Basic Programs