Display Fibonacci Series in Python

Beginner
⏱️ 8 min read
📚 Updated: May 2026
🎯 2 Code Examples
Iteration

What you’ll learn

  • The standard definition with seeds 0 and 1.
  • A clean n-term printer using two variables.
  • A second variant: print values up to a ceiling, plus live preview and edge cases.

Overview

Fibonacci is a classic state-update pattern: keep two previous terms, print one, then move forward.

Two programs

First n terms and values ≤ 100.

Live preview

Browser widget uses BigInt for exact terms.

Rigor

Handles nonpositive counts and off-by-one confusion.

Prerequisites

for loops, while loops, and integer addition in Python.

  • Function definitions with def and output with print().
  • Basic understanding that Python integers can grow very large.

What is the Fibonacci series?

Start with 0 and 1. Every next value is the sum of the previous two.

Recurrence: Fk = Fk-1 + Fk-2.

Seeds0, 1
Example0, 1, 1, 2, 3, 5...
Growth∼ φk

Golden ratio bound

Fk ∼ φk / √5, so values increase quickly.

First ten terms

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

Intuition

5 + 813
Rule
sum of previous two
(a,b)shift
Update
(a,b) ← (b,a+b)

Takeaway: two variables are enough to stream the full series.

Live preview

Print first n terms using exact BigInt.

Try 1, 15, or 80. Capped at 500 terms in this widget.

Live result
Press “Print series”.

Algorithm

Goal: print first n terms (or terms up to max value).

Initialize

Set a = 0, b = 1.

Iterate and shift

Print a, compute next = a + b, then update a, b = b, next.

📜 Pseudocode

Pseudocode
function print_first_n_fibonacci_terms(n):
    a = 0
    b = 1
    repeat n times:
        output a
        next = a + b
        a = b
        b = next
1

First n terms (n = 10)

Interview-style first n terms printer with clean comma formatting.

python
def display_fibonacci_terms(n: int) -> None:
    if n <= 0:
        print("Need a positive term count.")
        return

    first, second = 0, 1
    parts = []
    for _ in range(n):
        parts.append(str(first))
        first, second = second, first + second

    print(f"Fibonacci series up to {n} terms: " + ", ".join(parts))

terms = 10
display_fibonacci_terms(terms)

Explanation

We keep two rolling values and build output in a list for cleaner formatting with ", ".join(...).

2

All terms ≤ 100

Same recurrence, different stop rule: continue while current term stays within the limit.

python
def display_fibonacci_until(max_val: int) -> None:
    if max_val < 0:
        print("max_val must be nonnegative.")
        return

    a, b = 0, 1
    parts = []
    while a <= max_val:
        parts.append(str(a))
        a, b = b, a + b

    print(f"Fibonacci numbers <= {max_val}: " + " ".join(parts))

display_fibonacci_until(100)

Explanation

The first Fibonacci number greater than 100 is 144, so the printed list ends at 89.

Going faster (optional)

Matrix exponentiation. Finds one large term in O(log n) arithmetic steps.

Fast doubling. Common in competitive programming for huge indices (often modulo values).

Interview: for printing many terms, the iterative two-pointer loop is best.

❓ FAQ

Each term is the sum of the previous two. With seeds 0 and 1, it starts 0, 1, 1, 2, 3, 5, 8, 13, ...
Yes, some books do that. The recurrence is the same; indexing is shifted.
Python integers are arbitrary precision, so values can grow far beyond 32-bit and 64-bit limits.
For printing many terms, iterative two-variable update is best. Naive recursion repeats work and is much slower.
That is valid as an empty series, or you can print a friendly message based on your requirement.
O(n) updates and O(1) extra space, ignoring output storage.

🔄 Input / output examples

Change terms in Example 1 or max_val in Example 2.

n (terms)Last printed
10
21
1034
507778742049

Edge cases and pitfalls

Python avoids fixed-width overflow, but clarity and stop conditions still matter.

Terms

n ≤ 0

Handle as empty/error depending on requirement.

Formatting

Comma placement

Using join() avoids trailing comma issues.

Recursion

Naive recursive fib

Recomputes work exponentially; avoid for long series output.

Indexing

Seed convention

State clearly whether sequence starts with 0,1 or 1,1.

⏱️ Time and space complexity

TaskTimeExtra space
First n terms (iterative)O(n)O(1)
All terms ≤ MO(k) where k terms printedO(1)
Naive fib(n) recursionO(φn)O(n)

For interview printing tasks, the iterative approach is the right default.

Summary

  • Core update: print a, then a, b = b, a + b.
  • Variants: fixed number of terms or stop at a max value.
  • Watch-outs: nonpositive input, indexing convention, and avoiding naive recursion.
Did you know?

The ratio of consecutive terms Fk+1/Fk approaches the golden ratio φ = (1+√5)/2 as k grows.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful