Python Programming · Middle Stage (6–8) · 1 course, 14 worked problems, 50 practice questions
A function is a named block of code that does one job. You define it with def, give it parameters for its inputs, and send back a result with return. For example, def kinetic_energy(m, v): can return 0.5 * m * v ** 2. Once it is written, a function can be called many times with different values, so you do not repeat code. Variables made inside a function are local, and they vanish when the function ends. Short functions with clear names make a program easier to read, test and fix.
return valueChoose an answer, then check it. Each one shows a full explanation.
A function looks for the first vowel of a word. It returns from inside its loop, or after it. def first_vowel(word): for ch in word: if ch in "aeiou": return ch return "-" print(first_vowel("rhythm"), first_vowel("strong")) What does the program print?
A function changes a string in a loop, but its result is not what the student expected. def stretch(s, n): for i in range(n): s = s + s[-1] result = stretch("ab", 2) print(result) What does the program print?
A function pads a word with a fill character up to a given width. It is defined like this, and then one call below is run. def pad(text, width, fill="*"): return text + fill * (width - len(text)) Which calls 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 function prints the length of a tuple, but it has no return statement. The program prints what the call gives back.
def show(v):
print(len(v), end=" ")
result = show((1, 2, 3))
print(result)
What does the program print?
A function writes into a dictionary that it is given. It has no return statement.
def register(book, name):
book[name] = len(book) + 1
book = {}
register(book, "Ann")
register(book, "Ben")
register(book, "Ann")
print(book)
What does the program print?
A function should give back a list without the first x in it, and leave the list it was given as it was. Its first line is missing, marked ____.
def without(v, x):
____
out.remove(x)
return out
data = [1, 2, 1]
print(without(data, 1), data)
Which line fills the gap so that the output is exactly [2, 1] [1, 2, 1]?
Two short functions are defined. One gives back a slice, and the other adds an item to a list.
def head(v, n=2):
return v[:n]
def bump(v):
v.append(len(v))
return len(v)
Which of these comparisons are True? Choose two.
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 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?
Each one has a hint, the key idea and a full worked solution.