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 total_of(items), which adds up the numbers in any iterable without a for loop: get an iterator with iter(), then call next() in a while True loop and stop when StopIteration is raised. Print total_of([4, 5, 6]) and total_of(range(5)).
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 the iterator stops after 1 and the program prints 3, 2, 1 and then liftoff.
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.
The program should print the menu twice, once as tea 2 lines and once as a count. The count comes out as 0 items. Fix it so it prints:
tea 2 cake 3 2 items
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 count and ch each time one of them changes, once both exist.
letters = iter("abc")
count = 0
ch = next(letters)
while ch != "c":
count = count + 1
ch = next(letters)Sign 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: Loop with for: no next() call and no StopIteration.
This code works, but it drives the iterator by hand. Rewrite it with a plain for loop: no next() call and no StopIteration. 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 a class Chunks(items, size) that can be looped over to get the items in lists of size (the last chunk may be shorter). It must be reusable: looping over the same object twice gives the chunks both times. Use a separate iterator class or yield inside __iter__. The program should print:
[1, 2, 3] [4, 5, 6] [7] again: 3 chunks
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.