Python Programming · High School (11–12) · 6 courses, 59 worked problems, 195 practice questions
Programs grow larger, and a programmer needs ways to keep them organised. This area covers the tools that working programmers use every day. Classes and objects bundle data with the functions that act on it. Inheritance and special methods let new classes build on old ones and work with +, len() and print(). Files and data formats such as CSV and JSON let a program save and read real data. Comprehensions and generators give short, memory-friendly loops. Testing and debugging catch mistakes, and data structures such as stacks, queues and trees organise data.
assert and fix a bug from its tracebackChoose an answer, then check it. Each one shows a full explanation.
A program writes three lines to a file, then adds up the lines that are numbers. A line that is not a number counts as 100.
with open("n.txt", "w") as f:
f.write("3\nx\n5\n")
total = 0
with open("n.txt") as f:
for row in f:
try:
total += int(row)
except ValueError:
total += 100
print(total, f.closed)
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?
Three classes form a chain. Each one sets its own rate, and Premium extends the cost method with super().
class Base:
rate = 2
def cost(self, n):
return n * self.rate
class Premium(Base):
rate = 3
def cost(self, n):
return super().cost(n) + 1
class Gold(Premium):
rate = 5
print(Base().cost(2), Premium().cost(2), Gold().cost(2))
What does the program print?
Each one has a hint, the key idea and a full worked solution.
A film object should report its kind through a method title. But the constructor stores the film's name in capitals under that same name.
class Film:
def __init__(self, name):
self.title = name.upper()
def title(self):
return "film"
movie = Film("up")
print(movie.title())
What happens when the program runs?
A class Point takes one required parameter and one with a default value. Each option below is run on its own, after the class.
class Point:
def __init__(self, x, y=0):
self.x = x
self.y = y
Which lines run with no error? 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.
A thermometer reading should turn into text through its own special method. The method gives back the stored number as it is. class Reading: def __init__(self, degrees): self.degrees = degrees def __str__(self): return self.degrees r = Reading(7) print("Now: " + str(r)) What happens when the program runs?
A class defines __add__ but not __iadd__. A second name points to the first object before += is used.
class Vec:
def __init__(self, x, y):
self.x, self.y = x, y
def __add__(self, o):
return Vec(self.x + o.x, self.y + o.y)
def __str__(self):
return f"({self.x}, {self.y})"
a = Vec(1, 2)
b = a
a += Vec(3, 4)
print(a, b, a is b)
What does the program print?
A class uses an empty list as the default value of a parameter of __init__.
class Team:
def __init__(self, members=[]):
self.members = members
def join(self, name):
self.members.append(name)
x, y, z = Team(), Team(), Team(["Kai"])
x.join("Ana")
z.join("Ben")
print(y.members, z.members, x.members is y.members)
What does the program print?
The class Plane has two base classes, Wing and Tail. Both set the class variable mark, and only Tail has the method label.
class Wing:
mark = "w"
class Tail:
mark = "t"
def label(self):
return self.mark
class Plane(Wing, Tail):
pass
print(Plane().label(), Plane.mark)
What does the program print?
Two cars are built around the same engine object. class Engine: def __init__(self): self.rpm = 0 class Car: def __init__(self, engine): self.engine = engine e = Engine() a, b = Car(e), Car(e) a.engine.rpm = 900 b.engine = Engine() print(a.engine.rpm, b.engine.rpm, e.rpm, a.engine is e) What does the program print?