Strings, lists and records · GCSE · OCR J277 2.2.3, AQA 8525 3.2.6, Edexcel 1CP2 6.3.1 · about 15 min
Making, indexing and changing a list; append, remove, in, split and join.
[1 mark]What does this program print?
legs = [20, 15, 30] print(legs[0], legs[-1], len(legs))
20 30 3
Index 0 is the first item, -1 the last, and len counts the items.
[1 mark]What does this program print?
notes = [262, 294, 330] notes[0] = 523 notes.append(349) print(notes)
[523, 294, 330, 349]
Assigning to an index replaces that item; append adds one to the end.
[1 mark]legs = [20, 15, 30]. What happens with print(legs[3])?
[1 mark]Why use an array rather than separate variables such as reading1, reading2, reading3?
[1 mark]What does this program print?
sides = "30,20".split(",")
print(sides)
print(int(sides[0]) + int(sides[1]))['30', '20'] 50
split makes a list of strings; int is needed before adding them.
[1 mark]What does this program print?
colours = ["red", "green"]
print("green" in colours, colours.index("green"))True 1
in asks whether an item is in the list, and index gives its position.
Start with notes = [262, 294, 330]. Change the first note to 523, add 349 to the end, print how many notes there are as 4 notes, then play every note in the list for 0.3 seconds. Change the list with list operations, not by typing a new list.
# the two lines every program starts with: the commands, then the robot from bugbot import * connect() notes = [262, 294, 330]
The hint students can ask for: A list can be changed in place: replace one item by its position, and add another on the end. Then loop over the list to play it.
from bugbot import *
connect()
notes = [262, 294, 330]
notes[0] = 523
notes.append(349)
print(len(notes), "notes")
for note in notes:
tone(note, 0.3)
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.