Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 42

Fibonacci Series Program in Python

In this lesson: Print Fibonacci series in Python using loop, recursion and generator — code, dry run and output. Copy & run. In Hindi & English.

What is the Fibonacci series

The Fibonacci series is a sequence where each number is the sum of the two before it. It starts 0, 1, then continues 1, 2, 3, 5, 8, 13.... It is a favourite programming exercise and a beautiful pattern found in nature too.

💡 Rule: next = previous + the one before that. Start with 0 and 1.

Fibonacci with a loop

n = 8
a, b = 0, 1
for i in range(n):
    print(a, end=" ")
    a, b = b, a + b
0 1 1 2 3 5 8 13

We keep two numbers, a and b. Each round we print a, then update both so they move forward through the sequence.

Dry run

Dry Run — Fibonacci loop: a, b = b, a+b
Stepprint anew a (= b)new b (= a+b)
1011
2112
3123
4235
5358
It keeps going: 0 1 1 2 3 5 8 13 ...
💡 Understand it this way

In Fibonacci each number is the sum of the previous two. The line a, b = b, a+b at once makes a become b and b become a+b. So the series moves forward: 0,1,1,2,3,5,8...

How a, b = b, a+b works

This one line is the heart of the program. Python first works out the whole right side using the current values, then assigns both variables together:

a, b = b, a + b
# right side uses OLD a and b
# then a = old b, b = old a + old b

Because both are updated at once, you do not need a temporary variable to hold the old value — Python handles it for you.

Fibonacci with recursion

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

for i in range(8):
    print(fib(i), end=" ")
0 1 1 2 3 5 8 13

This mirrors the maths: fib(n) = fib(n-1) + fib(n-2), with base cases fib(0)=0 and fib(1)=1. It is elegant but slow for large n — use the loop for big series.

Common mistakes

  • Wrong starting values. Begin with a, b = 0, 1, or the whole series shifts.
  • Updating a and b separately. Do a, b = b, a+b in one line, or the second update uses the new a.
  • Deep recursion for big n. Plain recursion is very slow — prefer the loop.
  • Missing the base case in recursion. Without if n <= 1, it never stops.

Summary

  • Fibonacci: each number is the sum of the previous two, starting 0, 1.
  • The loop keeps two numbers and prints them in turn.
  • a, b = b, a+b updates both at once — no temp variable.
  • Recursion mirrors the maths but is slow for large n.
  • Use the loop for printing long series.

Next, try the factorial program, or practise Python loops.

Frequently Asked Questions

What is the Fibonacci series?
The Fibonacci series is a sequence where each number is the sum of the two before it, starting from 0 and 1: 0, 1, 1, 2, 3, 5, 8, 13, and so on. It appears in maths and nature.
How does a, b = b, a+b work in Python?
Python evaluates the right side first, then assigns both at once. So a becomes the old b, and b becomes the old a + b — updating both in one clean line without a temporary variable.
Should I use a loop or recursion for Fibonacci?
A loop is efficient and preferred for printing the series. Recursion is elegant and good for learning, but plain recursion is very slow for large n because it recalculates the same values many times.
Why is recursive Fibonacci slow?
Each call splits into two more calls, and many values get computed again and again. For big n this becomes extremely slow. A loop, or recursion with memoisation, fixes this.
What are the first two Fibonacci numbers?
They are 0 and 1 (some versions start at 1 and 1). From there, every next number is the sum of the previous two, so the third is 0 + 1 = 1.
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicFactorial Program Your progress: 42 / 66 (64%) · Lesson 43 / 66 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free