JIT — Intro to Data Science · Python Lab · Problem 6 of 30

A list of marks

One box holding many marks — and the counting that starts at zero, which is where almost every list mistake begins.

Six marks, one box

Problem 3 needed three boxes for three subjects. Six subjects would need six boxes, and a class of two hundred students would need two hundred. That does not scale. A list holds many values in a single box, in a fixed order, and lets you reach any one of them by its position.

New word — list Several values inside square brackets, separated by commas: marks = [78, 65, 80]. The order you write them in is the order they stay in. A list can hold numbers, text, or anything else.
New word — index The position of one item, written in square brackets after the list: marks[0].
Counting starts at zero, not one. So the first item is marks[0] and the third is marks[2]. This trips up nearly everybody at first, and it does not announce itself — asking for the wrong position gives you a real mark belonging to the wrong student.

Negative numbers count backwards from the end, so marks[-1] is the last item however long the list is.
New word — slice Two positions with a colon between them: marks[0:3] hands back a smaller list. It starts at the first position and stops just before the second, so 0:3 gives you three items — positions 0, 1 and 2. The end is never included.

Where the counting lands

Position from the front012345
The mark there786580925488
Position from the back-6-5-4-3-2-1
New word — len and append len(marks) counts the items. marks.append(70) adds one to the end — and unlike the string methods in problem 5, this one changes the list itself rather than handing back a new one. After appending, the list is longer and len reports a bigger number.

What you are building

All marks: [78, 65, 80, 92, 54, 88] How many students: 6 First student: 78 Last student: 88 First three: [78, 65, 80] After adding one more: [78, 65, 80, 92, 54, 88, 70] How many students now: 7
Coming later Reaching items one at a time by position only works while the list is short. Walking through every item, however many there are, is what a loop does — problems 13 to 15.

Build the programme

Your plan

The steps still to place

The whole programme is laid out below, in order. Five pieces are missing. Every one of them is a place where a small slip gives you a real-looking number belonging to the wrong student.

Leave nothing on "choose…". A wrong pick does not always cause an error — sometimes it just prints something you did not expect, which is the harder kind of mistake to spot.

Run and read

Here is the whole programme. The list of marks is yours to change — type any marks separated by commas, or pick a suggestion. Watch what happens to the pieces that were pulled out by position.

Your programme

Output

Nothing has run yet.