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. Watch when shout actually runs.
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.
parcels holds (name, kilograms) pairs. Use sum with a generator expression to set total to the weight of the parcels heavier than 1 kg, and count to how many there are. Print 3 heavy parcels, 13.5 kg.
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 ___ with a generator expression so the program prints Needs help: True when any score is below 50, and All passed: False.
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 how many cities have long names, and then the names. It fails with TypeError: object of type 'generator' has no len(). Fix it so it prints:
2 ['Barcelona', 'Singapore']
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 total each time one of them changes, once both exist.
total = 0
for n in (x * x for x in range(1, 4)):
total = total + 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: sum takes a generator expression, not a list comprehension.
This code works, but it builds a whole list just to add it up. Rewrite the sum call to take a generator expression instead of a list comprehension. 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.
Each line of raw is sensor,reading, but some lines are blank and some readings are ERR. Build a pipeline of generator expressions: clean the lines, skip blank ones, split them, keep only readings that are numbers, and convert them to float. Collect the readings in a list values, then print the count, the average to one decimal place, and the first reading above 30 (or None):
5 readings average 24.9 first above 30: 31.2
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.