Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Examples
  3. Fibonacci Series

Loops · Python example

Fibonacci Series

Generate the first n Fibonacci numbers with parallel assignment.

Open the compilerAll examples
beginnerloopssequencesassignment

Each Fibonacci number is the sum of the two before it. Python's tuple assignment updates both variables in one step.

example.py
count = 10
a, b = 0, 1

for _ in range(count):
    print(a, end=" ")
    a, b = b, a + b

print()

How the code works

  • Initially a is 0 and b is 1.
  • Each iteration prints a, then shifts the pair forward.
  • The final print() adds a newline after the sequence.

Key ideas

  • a and b hold the two most recent values.
  • The loop counts iterations, not values, so _ is used as an unused variable.
  • end=' ' keeps the output on one line.

Try changing it

  • Store the values in a list and return them from a function.
  • Print only even Fibonacci numbers.
  • Compare a recursive implementation for small n.

Related examples

beginner

Calculate a Factorial

Multiply numbers in a loop to compute n! with a loop.

beginner

Sum of Natural Numbers

Add the first n natural numbers with a loop and compare the closed formula.

Previous exampleCalculate a FactorialNext examplePrime Number Checker

Expected output

0 1 1 2 3 5 8 13 21 34

Output is compared after trimming trailing whitespace on each line. Run the code to see it produced by CPython in your browser.

Practice next

Apply the same idea with automatic tests or browse a guided project.

ExercisesProjects