Fibonacci series in Python
Print the Fibonacci series in Python with a for loop, a while loop, recursion and a generator, and find the nth Fibonacci number. Run every example in your browser.
Short answer
Start with a, b = 0, 1, then repeat print(a) and a, b = b, a + b. That prints 0, 1, 1, 2, 3, 5, 8, … in O(n) time. Recursion is shorter to write but very slow for large n unless you cache it with functools.lru_cache.
Method 1: the first n terms with a for loop
Each number is the sum of the two before it. a, b = b, a + b moves both values one step at the same time, so you never need a temporary variable.
n = 10
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
0 1 1 2 3 5 8 13 21 34
Method 2: every term below a limit with a while loop
When the question says “up to 50” instead of “the first 10 terms”, loop on the value, not a count.
limit = 50
a, b = 0, 1
while a < limit:
print(a, end=" ")
a, b = b, a + b
0 1 1 2 3 5 8 13 21 34
Method 3: the nth number with recursion and a cache
Plain recursion recomputes the same values again and again, so fib(40) already takes seconds. lru_cache remembers each answer, which makes it fast enough for fib(100).
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
if n < 2:
return n
return fib(n - 1) + fib(n - 2)
print(fib(10))
print(fib(100))
55 354224848179261915075
Method 4: a generator
A generator hands out one value at a time with yield, so it can describe an endless series without storing it. Wrap it in list() when you want all the values at once.
def fibonacci(limit):
a, b = 0, 1
while a < limit:
yield a
a, b = b, a + b
print(list(fibonacci(50)))
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Common mistakes
- Updating one variable at a time:
a = bthenb = a + buses the newaand gives the wrong series. Use the tuple assignmenta, b = b, a + b. - Calling plain recursive
fib(n)for n above about 35. The number of calls roughly doubles with each step, sofib(40)already makes over 300 million. Use the loop or add@lru_cache. - Mixing up “first n terms” and “terms below n”. Read the question: one loops on a counter, the other on the value.
Practice: your turn
Write solve(limit) as a generator that yields the Fibonacci numbers 0, 1, 1, 2, 3, 5, … while each value is less than limit. The tests collect it with list().
This challenge and its tests come from the PyMaster course. Press Run tests to check your function.
Input for input()
Runs real Python 3.14 in your browser with Pyodide. Your code is not uploaded. Ctrl + Enter runs the tests. Press Esc, then Tab, to leave the editor.
Hint 1
Yield a first, then advance: a, b = b, a + b.
Hint 2
The loop condition is a < limit, so the first value at or above the limit is not yielded.
Hint 3
solve(1) gives [0]; solve(2) gives [0, 1, 1].
Show a solution
def solve(limit):
a, b = 0, 1
while a < limit:
yield a
a, b = b, a + b
Questions
Does the Fibonacci series start with 0 or 1?
Both are used. Most programming courses and Python examples start with 0, 1. If your teacher starts with 1, 1, set a, b = 1, 1.
How do I print the series on one line?
Pass end=" " to print, as in the examples above, or build a list and print it once.
What is the time complexity?
The loop and the generator are O(n). Plain recursion is about O(2ⁿ), which is why it gets slow so quickly. With lru_cache it becomes O(n).
Can I get a very large Fibonacci number?
Yes. Python integers have no size limit, so the loop can compute the 1,000th term exactly.
