Day 41: Write Generator Function

– It produces generator object when it called.– It defines like general function using def keyword.– Its return value is using yield keyword. It yields a sequence of values instead of a single value.

How to build a generator?

A generator function is defined as you do a regular function, but whenever it generates a value, it uses the keyword yield instead of return. Let us look at the exercise in DataCamp’s tutorial which walk me through how to write generator function in Python.

The instructions as below:1. Complete the function header for the function get_lengths() that has a single parameter, input_list.2. In the for loop in the function definition, yield the length of the strings in input_list.3. Complete the iterable part of the for loop for printing the values generated by the get_lengths() generator function. Supply the call to get_lengths(), passing in the list lannister.