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.

`` 8 ABCDEFGH 5 XYZQR ``

Given this source file

![](examples/advanced/rows.txt)

using this code

![](examples/advanced/rows_to_records.py)

we can create this file:

![](examples/advanced/records.txt)

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


examples/generators/yield_outside_any_function.py
print("one")
yield "ok";
print("two")

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