Fibonacci Sequence in Python
0, 1, 1, 2, 3, 5, 8... Generate the sequence with a loop and two variables, then run it online.
- How each Fibonacci number is built
- Updating two variables at once with a, b = b, a + b
- Collecting the first n terms
Each term adds the two before it
The problem
Many things grow by building on what came before: rabbit populations in a classic puzzle, spirals in sunflowers, steps you can climb one or two at a time. The Fibonacci sequence is the simplest model of that pattern, and computing it teaches you how to carry values from one loop round to the next.
Think of it like this
Picture two numbers written on a slip of paper: the current one and the next one. Each round you read out the current number. Then the next number becomes the current one, and the new next number is the two old numbers added together. You never need more than the last two numbers.
a, b = 0, 1📝 Write down the first two numbersa, b = b, a + b🔄 Next becomes current; the new next is the sum of the twofor _ in range(count):🔁 Repeat for as many terms as you needprint(a, end=" ")📣 Read out the current numberThe Fibonacci sequence starts with 0 and 1, and every following term is the sum of the two terms before it.
Key points
a, b = 0, 1stores the first two terms of the sequence.print(a, end=" ")prints the current term and stays on the same line.a, b = b, a + bcomputes both new values from the old ones before assigning either of them.for _ in range(count):repeats ten times;_is the usual name for a loop variable you do not use.
When should you use it?
- Practicing loops that carry state from one round to the next.
- Understanding sequences where each value depends on earlier values, a first step toward dynamic programming.
- Classic interview and exam exercises.
When not to use it
- Computing a single huge term such as the 10,000th: there are faster formulas than looping through every term.
- Recursion without caching:
fib(n - 1) + fib(n - 2)recomputes the same values again and again and becomes very slow.
Example code
count = 10
a, b = 0, 1
for _ in range(count):
print(a, end=" ")
a, b = b, a + b
Result of running the code
0 1 1 2 3 5 8 13 21 34
Code explanation
a and b always hold two neighboring terms of the sequence, starting with 0 and 1. Each round prints a and then moves one step along the sequence with a, b = b, a + b. Python evaluates the right side first, using the old values, and only then assigns both names, which is why no temporary variable is needed.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Updating the variables one at a time
After a = b, the old a is lost, so b gets the wrong value.
a = b
b = a + b
a, b = b, a + b
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Fibonacci Number Return the n-th Fibonacci number. Easy
- Sum Even Fibonacci Terms Add the even Fibonacci terms that do not exceed a limit. Easy