Iterators and generators · 8 / 15 Exercise Easy

Fibonacci with a generator

Objective

Create a Python program with a generator that produces the first terms of the Fibonacci sequence.

Instructions

  • Define def fibonacci(n), which yields the first n terms: start with a = 0 and b = 1, and in each turn yield a and do a, b = b, a + b.
  • Read an integer n (at least 1).
  • Show the terms on one line, separated by spaces.
Input
10
Output
0 1 1 2 3 5 8 13 21 34

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise