Python Programming · High School (9–10) · 8 courses, 111 worked problems, 237 practice questions
After the basics, a programmer needs more ways to store data and more ways to solve problems. This area builds them. Tuples and sets hold fixed or unique collections, and dictionaries store values under keys, like meanings under words in a glossary. Modules and libraries let you reuse code that others have written. Text processing works with words and sentences, and errors and exceptions help a program deal with bad input. Searching and sorting, and recursion, are classic ways to solve problems. Essentials projects join these skills in longer programs.
try and exceptChoose an answer, then check it. Each one shows a full explanation.
A recursive function calls itself twice for each number that is two or more, and adds one each time. def calls(n): if n < 2: return 1 return calls(n - 1) + calls(n - 2) + 1 print(calls(4)) What does the program print?
A function adds results to a list that it is given. Part way through, one of the results fails, and the caller handles the error. def fill(target, values): for v in values: target.append(10 // v) return len(target) data = [] try: n = fill(data, [5, 2, 0, 1]) except ZeroDivisionError: n = -1 print(n, data) What does the program print?
Three small functions are defined. Two of them call themselves, and one uses a loop.
def a(n):
if n == 0:
return 0
return n + a(n - 1)
def b(n):
total = 0
for k in range(n):
total += k
return total
def d(n):
if n <= 1:
return n
return n * d(n - 1)
Which of these expressions are True? Choose every correct option.
One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.
One function calls another. The inner one reads past the end of a list, and neither function has a try.
def inner(items):
print("in", end=" ")
return items[3]
def outer(items):
v = inner(items)
print("out", end=" ")
return v
try:
print(outer([1, 2, 3]))
except IndexError:
print("caught")
What does the program print?
A function tries to add one to the first item of a tuple. If that fails, it builds a new tuple instead. def bump(t): try: t[0] += 1 except TypeError: t = (t[0] + 1,) + t[1:] return t p = (1, 2) q = bump(p) print(p, q, p is q) What does the program print?
A function adds up points for the letters of a word. Letters that are not in the dictionary get a default value. def score(word, values): total = 0 for ch in word: total += values.get(ch, 1) return total pts = {"a": 2, "z": 10} print(score("pizza", pts), len(pts)) What does the program print?
A function asks a tuple three questions and returns the three answers together. def info(t): return len(t), t.count(2), t.index(2) print(info((2, 4, 2, 6))) What does the program print?
A function reads from a box, which may be a dictionary, a list or a tuple. Its first except names the family LookupError, and its second names Exception.
def fetch(box, k):
try:
return box[k]
except LookupError:
return "missing"
except Exception:
return "bad"
print(fetch({"x": 1}, "y"), fetch([1, 2], -3), fetch((5, 6), "0"))
What does the program print?
A recursive function works on a shorter and shorter slice of a list. def walk(v): if len(v) == 1: return v return walk(v[1:]) + [v[0]] print(walk([1, 2, 3, 4])) What does the program print?
A program removes names from a list. One of the names is not in the list. names = ["Ali", "Bea", "Cal"] for target in ["Bea", "Dev"]: try: names.remove(target) print(target, "out", end="; ") except ValueError: print(target, "not found", end="; ") print(len(names)) What does the program print?
Each one has a hint, the key idea and a full worked solution.