Beginner Python Exercises

Fibonacci Sequence in Python

0, 1, 1, 2, 3, 5, 8... Generate the sequence with a loop and two variables, then run it online.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • How each Fibonacci number is built
  • Updating two variables at once with a, b = b, a + b
  • Collecting the first n terms
First ten terms
0
1
1
2
3
5
8
13
21
34
13 = 5 + 8: every term is the sum of the two before it.

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.

From two numbers on a slip to code
a, b = 0, 1📝 Write down the first two numbers
a, b = b, a + b🔄 Next becomes current; the new next is the sum of the two
for _ in range(count):🔁 Repeat for as many terms as you need
print(a, end=" ")📣 Read out the current number
Only the last two values are needed to make the next one.
In one sentence

The Fibonacci sequence starts with 0 and 1, and every following term is the sum of the two terms before it.

Key points

  1. a, b = 0, 1 stores the first two terms of the sequence.
  2. print(a, end=" ") prints the current term and stays on the same line.
  3. a, b = b, a + b computes both new values from the old ones before assigning either of them.
  4. 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

Run code →
main.py
count = 10
a, b = 0, 1

for _ in range(count):
    print(a, end=" ")
    a, b = b, a + b
Get a Run button for this example

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.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Updating the variables one at a time

After a = b, the old a is lost, so b gets the wrong value.

Problem
a = b
b = a + b
Fix
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.

All challenges