Python Programming · High School (11–12) · 1 course, 11 worked problems, 31 practice questions
Python often lets you build a list in one clear line. The list comprehension [x * x for x in range(10)] makes the squares of 0 to 9, and an if at the end filters the items. Dictionary and set comprehensions work in the same way. An iterator gives items one at a time, and every for loop uses one. A generator is a function that uses yield to hand out values one by one instead of building a whole list. It saves memory, so it suits very long or endless sequences, such as the prime numbers.
yieldChoose an answer, then check it. Each one shows a full explanation.
A list comprehension with two for parts builds a small deck of card objects.
class Card:
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __str__(self):
return self.rank + self.suit
deck = [Card(r, s) for s in "HS" for r in "AK"]
print([str(c) for c in deck], len([c for c in deck if c.suit == "S"]))
What does the program print?
An iterator is made from a string. One value is taken with next, and a list comprehension uses the rest.
it = iter("python")
first = next(it)
rest = [ch for ch in it if ch != "h"]
print(first, rest, next(it, "end"))
What does the program print?
A generator expression is tested with in, and then turned into a list.
squares = (n * n for n in range(6))
print(9 in squares, list(squares))
What does the program print?
Each one has a hint, the key idea and a full worked solution.
A generator function gives the characters of a text up to a stop character, and then ends with return.
def upto(text, stop):
for ch in text:
if ch == stop:
return
yield ch
print("".join(upto("data,science", ",")), list(upto("abc", "z")), list(upto(",x", ",")))
What does the program print?
A list comprehension and a lambda work on the words of a sentence.
words = "the quick brown fox jumps".split()
long = [w[0].upper() for w in words if len(w) > 3]
print("".join(long), sorted(words, key=lambda w: w[-1])[0])
What does the program print?
A generator function gives the capital letters of a text, as small letters. def capitals(text): for ch in text: if ch.isupper(): yield ch.lower() g = capitals("PyCharm IDE") first = next(g) rest = list(g) print(first, rest, list(g)) What does the program print?
Two generator functions work one after the other on a shopping list. def parts(text): for part in text.split(";"): yield part.strip() def numbered(items): n = 0 for item in items: if item: n += 1 yield str(n) + "." + item print(list(numbered(parts("milk; ;eggs;bread ")))) What does the program print?
A list comprehension uses a conditional expression, and filter is called with None.
vals = [x if x % 3 else 0 for x in range(1, 7)]
print(vals, list(filter(None, vals)))
What does the program print?
A nested list comprehension builds a grid, and a second one counts some of its numbers. grid = [[r * c for c in range(3) if c != r] for r in range(1, 4)] print(grid, len([x for row in grid for x in row if x % 2])) What does the program print?
A generator function gives the even numbers below n.
def evens(n):
for k in range(n):
if k % 2 == 0:
yield k
g = evens(7)
print(next(g), sum(g), list(g), sum(evens(7)))
What does the program print?