Lab, Iterators and Generators
7 tasks, easy to hard: Predict, then run, Write it, Complete it, Fix it, Trace it, Refactor it, Mini challenge. Hints come one at a time and you can see each answer. Lab work is saved as practice and earns no XP.
Open the lessonYou can read every task below. Running, checking and saving them needs a free ID.
7 tasks
Task 01 .Type what the program will print, then run it to compare.
Type exactly what this program prints.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 02 .Solve it from the prompt, starting from an empty editor.
Write a generator function countdown(n) that yields n, n - 1 and so on down to 1. Print list(countdown(5)), then use a for loop over countdown(2) to print each number on its own line.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 03 .Fill the marked gaps in the starter code.
Replace ___ so flatten hands out every item of every inner list, and the program prints ['a', 'b', 'c', 'd'].
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 04 .The starter code has a bug. Repair it so the checks pass.
multiples(step, limit) should hand out every multiple of step up to limit, but it fails with TypeError: 'int' object is not iterable. Fix it so the program prints [3, 6, 9, 12].
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 05 .Fill the table with the variable values, step by step.
Each row shows n and best each time one of them changes, once both exist.
def readings():
yield 5
yield 3
yield 8
best = 0
for n in readings():
if n > best:
best = nSign in or create a free ID to fill in and check this table.
Task 06 .Rewrite working code to the stated rule, keeping the same output. Rule: initials yields each pair of initials: no result list, no append and no return.
This code works, but initials builds a whole list before returning it. Rewrite initials as a generator function with yield, with no list and no append. The output must not change.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 07 .A short task that combines this lesson with earlier ones.
Write three generator functions and chain them: parse(lines) yields (name, score) with the score as an int, skipping blank lines and lines without exactly one :; passed(pairs, mark) yields only pairs with a score of at least mark; numbered(pairs) yields strings like 1. Mei 92, numbering from 1. Print every numbered line for a pass mark of 60:
1. Mei 92 2. Tom 61 3. Ola 78
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.