📘 Lesson  ·  Lesson 74

Fibonacci Series

Fibonacci Series in Python (Loop and Recursion)

The Fibonacci series is a sequence where each number is the sum of the two before it (0, 1, 1, 2, 3, 5...).

Python Program

Python
# Using a loop
def fibonacci(n):
    a, b = 0, 1
    for _ in range(n):
        print(a, end=" ")
        a, b = b, a + b

fibonacci(8)

# Using recursion
def fib(n):
    if n <= 1:
        return n
    return fib(n-1) + fib(n-2)
print("\n5th term:", fib(5))

Expected Output

0 1 1 2 3 5 8 13 5th term: 5

How it Works

  • Loop version: keep two numbers and swap them to get the next.
  • Recursion: fib(n) = fib(n-1) + fib(n-2), with base case n <= 1.

Summary

  • The Fibonacci series is a sequence where each number is the sum of the two before it (0, 1, 1, 2, 3, 5...).
← 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.