Python Fibonacci Series Program

Beginner
6 min read
Updated: Oct 2026
2 programs
Live preview

What Is the Fibonacci Series?

The Fibonacci series starts with seeds 0 and 1. Every next term is the sum of the previous two: 0, 1, 1, 2, 3, 5, 8, 13, …

Remember
F(0) = 0
F(1) = 1
F(k) = F(k − 1) + F(k − 2)   for k ≥ 2

Example: first 10 terms
  0, 1, 1, 2, 3, 5, 8, 13, 21, 34
kF(k)How
00seed
11seed
210 + 1
552 + 3
93413 + 21

How to Print Fibonacci Series in Python

Four clear steps — then pick a fixed term count or a value ceiling.

  1. State the seeds: a = 0, b = 1.
  2. Print (or store) the current term a.
  3. Advance with a, b = b, a + b.
  4. Stop after n terms, or while a <= max_val.
MethodIdeaTimeBest for
First n termsLoop n times with two-variable shiftO(n)Interviews, classic prompt
Until max valueWhile a <= max_val, same shiftO(k)List all terms within a limit

Pseudocode

Pseudocode
function PrintFirstN(n):
    if n <= 0:
        return empty

    a = 0
    b = 1
    repeat n times:
        output a
        next = a + b
        a = b
        b = next

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Seedsa, b = 0, 1
Advancea, b = b, a + b
n termsfor _ in range(n): ...
Until maxwhile a <= max_val: ...
First ten0, 1, 1, 2, 3, 5, 8, 13, 21, 34
AvoidNaive recursion to print many terms
Format", ".join(parts) — no trailing comma

Live Preview

Enter how many terms to print. Exact values via BigInt in the browser.

Whole numbers from 0 to 100 in this preview. Tap a chip or type a value.

Result
n = 10
First 10 terms: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Worked Walkthrough — First 6 Terms

Start at (0, 1), print a, then shift with a, b = b, a + b.

Calculation
Start: a = 0, b = 1

Step 1: print 0  →  a, b = 1, 0+1 = (1, 1)
Step 2: print 1  →  a, b = 1, 1+1 = (1, 2)
Step 3: print 1  →  a, b = 2, 1+2 = (2, 3)
Step 4: print 2  →  a, b = 3, 2+3 = (3, 5)
Step 5: print 3  →  a, b = 5, 3+5 = (5, 8)
Step 6: print 5  →  a, b = 8, 5+8 = (8, 13)

Printed: 0, 1, 1, 2, 3, 5

Python Programs

Start with a fixed term count for interviews. Switch the stop rule when you need every term up to a maximum value.

Example 1 — First n Terms

Two rolling variables stream the series. Collect strings, then join for clean commas.

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

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

    print(f"First {n} terms: " + ", ".join(parts))


display_fibonacci(10)

How It Works

  1. Reject n <= 0 so the empty case is explicit.
  2. Seed a, b = 0, 1 — the usual interview convention.
  3. Each loop: append a, then set a, b = b, a + b.
  4. Repeat exactly n times. For n = 10 you get 0 through 34.
  5. ", ".join(parts) formats the list without a trailing comma.

Example 2 — All Terms ≤ max_val

Same update rule; stop when the current term would exceed 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 <= {max_val}: " + ", ".join(parts))


display_fibonacci_until(100)

How It Works

  1. Validate max_val >= 0.
  2. Seed a, b = 0, 1 again.
  3. While a <= max_val, append a and advance (a, b).
  4. The next Fibonacci after 89 is 144, so the list stops at 89 for 100.
  5. Same recurrence as Example 1 — only the stop condition changes.

Edge Cases & Pitfalls

Check these before calling the solution done.

n ≤ 0

Empty or message

Decide up front: empty output or a clear error string.

Seeds

Say 0, 1 (or 1, 1)

Mixing conventions without stating them causes off-by-one debates.

Recursion

Do not print with naive fib(n)

Exponential recomputation. Iterate for series; memoize only for a single nth term.

Formatting

Trailing commas

Collect terms, then join — do not print a separator after every term.

Time and Space Complexity

ProgramTimeExtra space
First n terms (iterative)O(n)O(1)
Until max valueO(k) terms printedO(1)
Naive recursive fib(n)O(φn)O(n) stack

For interview printing tasks, the iterative loop is the right default. Extra space above ignores the output list or string size.

Key Takeaways

  • Definition: each term is the sum of the previous two (seeds 0, 1).
  • Core update: a, b = b, a + b.
  • Stops: fixed n terms, or while a <= max_val.
  • Test set: n=10 → 0…34; ≤100 ends at 89; avoid naive recursion.

One line: print a, then shift with a, b = b, a + b until you hit n or the max.

Frequently Asked Questions

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. The recurrence is the same; only the indexing shifts. State your seed convention before coding.
No. Naive recursion recomputes work exponentially. Use a two-variable iterative loop to print many terms.
No. Python integers grow as needed, so large Fibonacci terms stay exact.
Treat it as an empty series, or print a clear message — pick one rule and stick to it.
O(n) updates with O(1) extra space for the iterative method (ignoring output storage).

Next: GCD

Find the greatest common divisor with the Euclidean algorithm.

GCD tutorial →

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.

11 people found this page helpful