Python Programming · Middle Stage (6–8) · 13 courses, 178 worked problems, 429 practice questions
Python is a programming language with clear, readable code, used in science, data work and the web. This area starts from the very first program and builds the core skills. It begins with getting started with Python, then variables and data types, operators and expressions, and input and output. Next come conditionals, which let a program choose, and loops, which repeat steps. Strings, lists and functions follow. Each idea is used in short programs about real things, such as a speed calculator or a list of marks, and mini projects bring the ideas together.
if, for and while to control what a program doesChoose an answer, then check it. Each one shows a full explanation.
A program calls a function on its first line. The def statement of that function comes after the call.
print(double(4))
def double(n):
return n * 2
What happens when the program runs?
A function uses a local variable named sum, the same name as a built-in function. After the call, the program uses the built-in sum().
def total(values):
sum = 0
for v in values:
sum += v
return sum
print(total([1, 2, 3]), sum([4, 5]))
What happens when the program runs?
The def of price uses the name rate, which gets its first value only on a later line. Between two calls, the program changes rate.
def price(qty):
return qty * rate
rate = 3
a = price(2)
rate = 5
print(a, price(2))
What does the program print?
Two functions share a global variable. The first call is made before the variable exists, inside a try statement.
def setup():
global level
level = 1
def up():
global level
level += 1
return level
try:
print(up())
except NameError:
setup()
print(up(), level)
What happens when the program runs?
A function deletes an item of a list by its index, and returns the new length. It is called twice, and each result is printed on the same line. def remove_at(items, i): del items[i] return len(items) data = [10, 20, 30] print(remove_at(data, -1), end=" ") print(remove_at(data, 2), end=" ") print(data) What happens when the program runs?
A function builds a list from one column of a table that is a list of lists. Then the program changes both the new list and the table. def column(grid, k): return [row[k] for row in grid] g = [[1, 2, 3], [4, 5, 6]] c = column(g, 1) c[0] = 99 g[1][1] = 0 print(c, g[0][1]) What does the program print?
A function builds the initials of a name, which has two spaces after its first word. Then the program splits a text on one space. def initials(name): out = "" for word in name.split(): out += word[0].upper() return out print(initials("ada king lovelace"), len("ada king".split(" "))) What happens when the program runs?
A function sorts the list it is given, and then returns a slice of it. The caller changes the slice. def top_two(scores): scores.sort(reverse=True) return scores[:2] marks = [55, 90, 72, 81] best = top_two(marks) best.append(0) print(marks[0], len(marks), best) What does the program print?
Some list methods change the list and give back None. A function such as sorted() gives back a new list instead.
nums = [3, 1, 2]
a = nums.sort()
b = sorted(nums, reverse=True)
c = nums.append(4)
Which of these expressions are True after the code has run? 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.
In the middle of a for loop, the function tally puts 10 into i. It returns a sum and i together.
def tally(n):
acc = 0
for i in range(n):
if i == 2:
i = 10
acc += i
return acc, i
print(tally(5))
What does the program print?
Each one has a hint, the key idea and a full worked solution.