Fibonacci Series in Python Using For Loop W3Schools Guide

Logan Jul 13, 2026

The Fibonacci series is a captivating arithmetic sequence where each number is the sum of the two preceding ones, usually starting with 0 and 1. Understanding how to generate this sequence in Python is a fundamental skill that every programmer should possess. In this article, we'll explore how to create the Fibonacci series using a simple for loop, drawing inspiration from the coding wisdom shared on W3Schools.

Fibonacci in python
Fibonacci in python

Before we dive into the code, let's understand the reasoning behind the Fibonacci sequence. The sequence starts with 0 and 1, as these are the two smallest numbers that can be summed to produce another number in the sequence (0+1=1). From there, each subsequent number is the sum of the preceding two (1+1=2, 1+2=3, and so on).

Fibonacci Series in Python
Fibonacci Series in Python

Understanding For Loops in Python

To generate the Fibonacci series, we'll use a for loop, which is a control flow statement that executes a block of code a specified number of times. In Python, for loops are defined by the syntax 'for X in Y: Z', where 'X' is a variable, 'Y' is an iterable, and 'Z' is the block of code to be executed.

Fibonacci Sequence in Python
Fibonacci Sequence in Python

In our case, we want to generate the first 'n' numbers in the Fibonacci sequence. So, our for loop will iterate 'n' times, with each iteration calculating the next number in the sequence.

Initializing the First Two Numbers

Fibonacci sequence
Fibonacci sequence

Since the Fibonacci sequence starts with 0 and 1, we need to initialize two variables to store these first two numbers. We'll call these variables 'a' and 'b', with 'a' starting at 0 and 'b' at 1.

In our for loop, we'll keep track of these two variables, updating them in each iteration to contain the most recent two numbers in the sequence.

Calculating and Printing the Next Number

Build a Python Fibonacci Sequence Generator
Build a Python Fibonacci Sequence Generator

Within our for loop, we'll calculate the next number in the sequence by adding 'a' and 'b'. After the calculation, we'll update 'a' and 'b' so that they contain the last two numbers (old 'a' and the just-calculated sum).

Finally, we'll print the calculated number. This will give us the desired output, with the numbers in the Fibonacci sequence printed one by one.

'Fibonacci()' Function in Python

a screen shot of a web page with the text fibonaci sequence in python
a screen shot of a web page with the text fibonaci sequence in python

As good programmers, we want to encapsulate our code in a reusable function. Let's define a function called 'fibonacci()' that takes one argument 'n', which represents how many Fibonacci numbers we want to generate.

Within this function, we'll use a for loop with the structure described above. We'll initialize 'a' and 'b', then use the for loop to calculate and print 'n' numbers in the Fibonacci sequence. Here's what the code might look like:

Fibonacci Pattern Explained | Easy Number Patterns & Golden Ratio Math Guide
Fibonacci Pattern Explained | Easy Number Patterns & Golden Ratio Math Guide
How To Code Fibonacci Sequence In Python Using Recursion | Python for Beginners Tutorials
How To Code Fibonacci Sequence In Python Using Recursion | Python for Beginners Tutorials
a computer screen with an image of a fibon and the text, which is written in
a computer screen with an image of a fibon and the text, which is written in
a screen shot of a web page with the text fibonaci sequence in python
a screen shot of a web page with the text fibonaci sequence in python
How to use while loops in Python
How to use while loops in Python
Python For Loop Explained 🐍 | Complete Beginner Cheat Sheet
Python For Loop Explained 🐍 | Complete Beginner Cheat Sheet
FIBONACCI SERIES USING RECURSION
FIBONACCI SERIES USING RECURSION
a screen shot of some type of programming program
a screen shot of some type of programming program
Python Loops Explained (For Loop & While Loop Guide)
Python Loops Explained (For Loop & While Loop Guide)
While loop in Python Easy to learn 🤩
While loop in Python Easy to learn 🤩
python Loops for loop and while loop
python Loops for loop and while loop
Power Function in Python Using a Loop
Power Function in Python Using a Loop
Fibonacci Series in C++
Fibonacci Series in C++
the program is displayed in two different languages
the program is displayed in two different languages
Shortcut to learn Python.[Cheatsheet]
Shortcut to learn Python.[Cheatsheet]
Loops in Python 💻
Loops in Python 💻
What is Fibonacci Series?
What is Fibonacci Series?
Code Fibonacci Rectangles in JavaScript | Our Family Code
Code Fibonacci Rectangles in JavaScript | Our Family Code
Python Learning Path for Beginners 2026 🚀
Python Learning Path for Beginners 2026 🚀
18 Python while Loop Examples and Exercises
18 Python while Loop Examples and Exercises

```python def fibonacci(n): a, b = 0, 1 for i in range(n): print(a) a, b = b, a + b ```

To demonstrate how to use this function, let's generate the first 10 Fibonacci numbers:

```python fibonacci(10) ```

This code will output:

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

As you can see, the function works as expected, generating the first 10 numbers in the Fibonacci sequence.

Now that we've explored how to generate the Fibonacci series using a for loop in Python, you might also want to learn about other methods, such as using recursion or list comprehension. The Fibonacci sequence has many applications in computer science and mathematics, and understanding how to generate it is a great first step in exploring these applications.