Python Code for Fibonacci Series: Easy Examples, Explanation & How to Run

Python code for fibonacci series: The Fibonacci series is one of the most popular topics in programming and is often used to teach logic building in Python. If you’re learning programming or preparing for coding interviews, understanding how to generate the Fibonacci sequence is essential.

Python code for fibonacci series

In this article, we’ll explore multiple ways to write Python code for Fibonacci series, including beginner-friendly methods and optimized approaches.

Article NamePython Code for Fibonacci Series: Easy Examples, Explanation & How to Run
Publish Date4/5/2026
What isPython code for fibonacci series
RequirementsPython installed (3.x recommended)
AuthorCodeswithsam

What is Fibonacci Series?

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones.

Example:

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

Formula:

F(n) = F(n-1) + F(n-2)

Method 1: Fibonacci Series Using Loop (Beginner Friendly)

This is the easiest and most recommended method for beginners.

✅ Python Code:

# Fibonacci series using loop

n = int(input("Enter the number of terms: "))

a, b = 0, 1

print("Fibonacci Series:")

for i in range(n):
    print(a, end=" ")
    a, b = b, a + b

📖 Explanation:

  • We initialize two variables a = 0 and b = 1
  • Loop runs n times
  • Each iteration updates values

✔ Output:

Enter the number of terms: 5
Fibonacci Series:
0 1 1 2 3

🔁 Method 2: Using Recursion

Recursion is a method where a function calls itself.

✅ Python Code:

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

terms = int(input("Enter number of terms: "))

print("Fibonacci Series:")

for i in range(terms):
    print(fibonacci(i), end=" ")

⚠️ Note:

  • This method is slower for large inputs
  • Useful for understanding concepts

⚡ Method 3: Optimized Fibonacci Using Memoization

To improve recursion efficiency, we use caching.

✅ Python Code:

from functools import lru_cache

@lru_cache(maxsize=None)
def fibonacci(n):
    if n <= 1:
        return n
    return fibonacci(n-1) + fibonacci(n-2)

terms = int(input("Enter number of terms: "))

for i in range(terms):
    print(fibonacci(i), end=" ")

🚀 Benefit:

  • Much faster than normal recursion
  • Avoids repeated calculations

🧮 Method 4: Fibonacci Using While Loop

Another beginner-friendly approach.

✅ Python Code:

n = int(input("Enter number of terms: "))

a, b = 0, 1
count = 0

while count < n:
    print(a, end=" ")
    temp = a + b
    a = b
    b = temp
    count += 1

💡 Method 5: Fibonacci Using List

If you want to store values:

✅ Python Code:

n = int(input("Enter number of terms: "))

fib = [0, 1]

for i in range(2, n):
    fib.append(fib[i-1] + fib[i-2])

print(fib[:n])

How to Run This Code (Beginner Guide)

Follow these simple steps:

🧾 Step 1: Install Python

Download and install Python from the official website.

🧾 Step 2: Open Editor

Use any of these:

  • VS Code
  • PyCharm
  • Notepad

🧾 Step 3: Save File

Save file as:

fibonacci.py

🧾 Step 4: Run Code

Open terminal and type:

python fibonacci.py

📊 Comparison of Methods

MethodDifficultySpeedBest For
LoopEasyFastBeginners
RecursionMediumSlowConcept learning
MemoizationMediumVery FastOptimization
While LoopEasyFastBeginners
List MethodEasyFastData storage

Final Thoughts

The Fibonacci series is a foundational concept in programming. Whether you use loops, recursion, or optimized methods, mastering this topic will strengthen your problem-solving skills in Python.

For beginners, start with the loop method. As you grow, explore recursion and optimization techniques.

Important Links

Our WebsiteCodeswithsam.com
Join TelegramClick Here

If we made a mistake or any confusion, please drop a comment to reply or help you in easy learning.

Thanks! 🙏 for visiting Codeswithsam.com ! Join telegram (link available in bottom) for source code files , pdf and
Any Promotion queries 👇
info@codeswithsam.com

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top