Chapters
Python114 chapters

Exercises

Generators

3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.

Exercise 1

Write countdown as a generator that yields 3, 2, 1.

Python
# define countdown
print(list(countdown(3)))
Exercise 2

Sum the squares below 1000 without ever building a list.

Python
# print the total
Exercise 3

The second sum gives 0. Fix it so both print the same total.

Python
squares = (n * n for n in range(4))
print(sum(squares))
print(sum(squares))