These questions follow Lists. Every one of them uses the same
starting list, so keep it in a file and edit as you go:
marks = [78, 91, 46, 63]
Positions and bounds
What does each print? marks[0], marks[3], marks[-1],
len(marks).
What happens when you run print(marks[4]), and why is 4 a
reasonable-looking mistake?
Starting from the original list each time, give the list after
marks.append(70), then after marks.insert(1, 55), then after
marks.remove(46) β applied one after the other.
Loops over lists
Write the loop that totals the marks and prints the average to one
decimal place.
Write a linear search that reports the position of
"Fifteen Dogs" in
titles = ["Birdie", "Fifteen Dogs", "Indian Horse"], and prints
-1 when the title is not there.
Count how many of weights = [6.5, 3.0, 11.2, 7.0] are above
6.0.
Find the largest value in weights without using max().
Challenge. Build a new list holding only the passing marks
(50 or more) from marks, then print how many there are and what
they are.
Answers
Answer 1
78, 63, 63, 4. Positions run 0 to 3, so marks[3] and
marks[-1] are the same element reached from opposite ends, and
len counts elements rather than naming the last position.
Answer 2
IndexError: list index out of range
Four elements, positions 0 to 3. 4 looks right because the list has
four marks in it β but counting from zero means the last position is
always len minus one.
Answer 3
[78, 91, 46, 63, 70], then [78, 55, 91, 46, 63, 70], then
[78, 55, 91, 63, 70]. append adds to the end, insert puts the
value at the position given and shifts everything after it along, and
remove deletes the first matching value β not a position.
Answer 4
total = 0for mark in marks: total = total + markprint(f"Average: {total / len(marks):.1f}")
For the original four marks that prints Average: 69.5. Dividing by
len(marks) rather than by 4 means the code still works when the
list changes, which it always does.
Answer 5
titles = ["Birdie", "Fifteen Dogs", "Indian Horse"]target = "Fifteen Dogs"position = -1for index in range(len(titles)): if titles[index] == target: position = indexprint(position)
This prints 1. Looping over range(len(titles)) gives you
positions rather than values, which is what a search has to report.
Starting at -1 is the convention for βnot foundβ, so the answer is
still meaningful when nothing matches.
Answer 6
weights = [6.5, 3.0, 11.2, 7.0]above = 0for weight in weights: if weight > 6.0: above = above + 1print(above)
3. A counter is an accumulator that grows by one instead of by the
value β same skeleton, different update.
Answer 7
highest = weights[0]for weight in weights: if weight > highest: highest = weightprint(highest)
11.2. Starting from weights[0] rather than from 0 is the part
that matters: a list of negative numbers would defeat a starting
value of zero, and the program would report a maximum that is not in
the list.
Answer 8
passing = []for mark in marks: if mark >= 50: passing.append(mark)print(f"{len(passing)} passing: {passing}")
3 passing: [78, 91, 63]
Building a second list leaves the original untouched, which is
usually what you want β the caller may still need the full set, and a
function that quietly destroys its input is a hard bug to find later.