Skip to the page
Year 9 · Autumn 1 · Python II: lists and strings

append, remove, len and in

PracticalPresent ▶

What this lesson covers

Topic 14 Programming further

KS3 Computing · Real programs keep data in lists, take text apart as strings, split work into subroutines, check their input, save to files, and get tested on purpose.

ProgrammingCambridge Lower Secondary Computing 0860

You will learn to

  • Use append to add each item the user types, until they type 'stop'.
  • Use len to count the items, and in to check if an item is there.
  • Use remove to delete one item, and explain the error if it is not in the list.
  • Predict what each line will print before running it.

Success criteria

  • I can build a list from input that stops when I type 'stop'.
  • I can check if 'milk' is in the list and print True or False.
  • I can write my prediction down before each run, then check it.

Key words

Say each word, then read what it means. Every one is in the glossary too.

list
One name holding many values in order. Also called an array.
index
An item's position in a list, counting from 0.
Subroutine
A named block of code you can call when needed.
Function
A subroutine that returns a value.
Parameter
A placeholder in a subroutine for a value passed in.
Validation
Checking input is reasonable before using it.
Boundary data
Test data at the very edge of what's allowed.

The explanation

Read it here, or open the study page below to go one step at a time with things to try.

Lists

A list (or array) stores many values under one name, in order.

scores = [12, 45, 33, 50]
Index0123
Value12453350
?Why?

Why start at 0? The index means 'how many steps from the start'. The first item is 0 steps away.

  • scores[0] → 12
  • scores[-1] → 50 (the last one)
  • scores.append(28) adds to the end
  • len(scores) → how many items
  • scores[10] → error: no item there

Study it

Open the study page and go one step at a time.

Check yourself

One question at a time. The reason shows after each answer.

Saved on this device.