Python Programming: Unit Exam A
Functions and procedures, parameters, local and global variables, 2D lists, dictionaries, files, and a function to write.
Instructions
- Time allowed
- 25 minutes
- Total marks
- 25
- Calculator
- No calculator is permitted.
- Answer all the questions.
- The number of marks for each question is shown in brackets. Use it as a guide to how much to write.
- The clock at the top counts down from the real time allowed. When it reaches zero the paper is locked and marked, as it would be collected in the exam hall.
- Read every question to the end before answering. Where a question says tick two boxes, tick exactly two: ticking more than asked scores nothing. Type one word and number answers carefully, because they are marked exactly.
Questions
Answer every question, then finish the paper to have it marked. Marking as you go teaches you far less.
Tick one box.
- LC A function returns a value; a procedure does not
Both are subroutines; only a function returns a value.
def add_vat(price):
return price * 1.2
print(add_vat(50))- L60
50 x 1.2 = 60.0.
x = 5
def change():
x = 10
change()
print(x)- L5
The x inside change() is a new local variable. The global x is still 5.
Tick one box.
- LA The x inside change() is a local variable, separate from the global x
Assigning to x inside a function creates a local variable.
- L3
- L7
- L5
- L3
The first index picks the row and the second the column, both counting from 0. There are 3 rows.
- L14
- L3
Bea's value is 14, and adding Cy makes three entries.
Tick one box.
- LD "a"
"a" is append mode.
Tick one box.
- LB Anything already in the file is erased
Write mode starts the file again from empty.
def largest(numbers):
# return the largest number in the list
pass
- LDefines largest with one parameter
- LReturns the largest value, for example 9 from [3, 9, 2]
- LWorks when every number is negative, for example -2 from [-5, -2, -9]
- LFinds it without max()
Start with the first item as the best so far, then compare it with every other item. Starting from 0 fails when every number is negative.
def largest(numbers):
best = numbers[0]
for n in numbers:
if n > best:
best = n
return best- LA subroutine can be called many times, so code is not repeated
- LEach part can be tested, fixed or written separately, which makes the program easier to read and maintain
A subroutine is written once and called whenever it is needed, so the same code is never repeated. Each subroutine can also be tested and fixed on its own, and different people can write different ones, which makes a large program far easier to manage.
def count_over(scores, limit):
total = 0
for s in scores:
if s > limit:
total = total + 1
return total
print(count_over([12, 5, 18, 7, 20], 10))- L3
12, 18 and 20 are over 10: three of them.
stock = {"pens": 4, "pads": 0, "rulers": 7}
for item in stock:
if stock[item] == 0:
print(item)- Lpads
Only pads has a value of 0.
Tick one box.
- LB file.readlines()
readlines() returns a list with one item per line.
- LAny part of the program can change a global variable, so bugs are hard to track down
- LLocal variables keep subroutines independent, so they can be reused and tested on their own
Any part of the program can change a global variable, so when its value is wrong it is hard to work out which part changed it. Local variables keep each subroutine self-contained, so it can be tested and reused without side effects.
names = ["Zoe", "Amy", "Max"]
names.sort()
print(names[0])- LAmy
Sorted alphabetically the list is Amy, Max, Zoe.
Tick one box.
- LD A variable in a subroutine's definition that receives a value when the subroutine is called
Parameters receive the values passed in.
This is an original Key Stage 3 assessment. Most questions are closed, so the marking is exact, and the written answers are marked against a mark scheme in the same style as a GCSE paper, so the format is familiar long before it counts.