Python Programming · High School (11–12) · 2 courses, 16 worked problems, 52 practice questions
A class is a blueprint, and an object is a thing made from it. A class Ball might store a mass and a speed, and have a method that finds the kinetic energy. In Python you write class Ball: and an __init__ method that sets up each new object. Inside the class, self stands for the object being used. Attributes hold the data, and methods are functions that belong to the class. Classes keep related data and actions together, so a large program is easier to read and change. Games, simulations and many libraries are built this way.
__init__ method and attributesself to work on an object's dataChoose an answer, then check it. Each one shows a full explanation.
A shop keeps its tax rate in the class body, and a method adds that tax to a price. class Shop: tax = 5 def total(self, price): return price + tax s = Shop() print(s.tax, Shop.tax) print(s.total(100)) Which of these describes the run of this program?
A class defines __eq__, and two objects with the same value are made.
class Coin:
def __init__(self, value):
self.value = value
def __eq__(self, other):
return self.value == other.value
a = Coin(5)
b = Coin(5)
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.
A parent and a child class each have a private class variable with the same name. class Vault: __limit = 3 def limit(self): return self.__limit class Big(Vault): __limit = 9 print(Big().limit(), hasattr(Big, "__limit"), sorted(k for k in vars(Big) if "limit" in k)) What does the program print?
A program reads the name of a method from the keyboard and calls it. The user types last and presses Enter.
class Tool:
def first(self, s):
return s.split()[0]
def last(self, s):
return s.split()[-1]
cmd = input().strip()
t = Tool()
if hasattr(t, cmd):
print(getattr(t, cmd)("big data"))
else:
print("no " + cmd)
What does the program print?
This program should print 3 words. It stops with TypeError instead.
class Sentence:
def __init__(self, text):
self.words = text.split()
def count():
return len(self.words)
s = Sentence("to be free")
print(s.count(), "words")
Which one change makes the program print 3 words?
A method of an object is kept in a variable, and called through that variable. class Builder: def __init__(self): self.parts = [] def add(self, s): self.parts.append(s) return len(self.parts) b = Builder() push = b.add push("x") push("y") print(push("z"), "".join(b.parts), Builder().parts) What does the program print?
A class has one class variable, and its constructor makes one instance variable.
class Box:
size = 1
def __init__(self, item):
self.item = item
b = Box("pen")
Which of these give 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.
The file app.ini holds two lines, mode=dark and port=80, each with a new line at its end. A class reads the file in its constructor.
class Config:
def __init__(self, name):
self.data = {}
with open(name) as f:
for line in f:
key, value = line.strip().split("=")
self.data[key] = value
def get(self, key, default="?"):
return self.data.get(key, default)
c = Config("app.ini")
print(c.get("mode"), c.get("port") * 2, c.get("user"))
What does the program print?
A method of a class is a generator. The object's text is changed after the generator is made, but before it is read. class Poem: def __init__(self, text): self.text = text def lines(self): for n, line in enumerate(self.text.split("/"), 1): yield str(n) + ":" + line.strip() p = Poem("roses / violets /sugar") g = p.lines() p.text = "new" print(list(g), len(list(p.lines()))) What does the program print?
A shop gives each new order the next serial number, kept on the class. class Order: made = 0 def __init__(self): Order.made += 1 self.serial = Order.made first, second, third = Order(), Order(), Order() print(second.serial, first.made, Order.made) What does the program print?
Each one has a hint, the key idea and a full worked solution.