Exercise: generator

Take the two generator examples (increment number and Fibonacci) and change them to provide infinite iterations. Then try to run them in a for loop. Just make sure you have some other condition to leave the for-loop.

## Exercise: Binary file reader {id: exercise-binary-file-reader}

Create a generator that given a filename and a number n will return the content of the file in chunks of n characters.

## Exercise: File reader with records {id: exercise-file-reader-with-records}

In a file we have "records" of data. Each record starts with three bytes in which we have the length of the record. Then the content.


Given this source file


using this code


we can create this file:


The exercise is to create an iterator/generator that can read such a file record-by-record.

## yield outside of functions {id: yield-outside-of-functions}

* It has no meaning there

yield "ok";

  File "examples/generators/yield_outside_any_function.py", line 2
    yield "ok";
SyntaxError: 'yield' outside function