Skip to content
S3U
Login Create Account

Science / Programming basics / Grade 8 / sc870

Programming Basics: Read and Update a List

Use a stated position convention and distinguish values from positions.

All worksheets

Watch the lesson

5 questions0m 0s

Learn first

Programs follow precise instructions

A program is a set of instructions. A variable stores a value. A condition chooses between paths. A loop repeats steps. Output is the result produced.

How to work it out Trace one instruction at a time. Keep a record of values and check exactly when a condition or repeated step is evaluated.

A separate example In a separate trace, start with n = 2, add 3, then double n. The two instructions happen in order: 5 is an intermediate value before the final result.

Learn the skill

For this worksheet only, list positions start at 1. SET items TO [4, 7, 2] creates three ordered values. items[2] means the second value. SET items[2] TO 9 replaces that value, not the whole list. Programming languages can use different indexing rules.

Worked example

Initially items[1] is 4 and items[2] is 7. Replacing position 2 with 9 gives [4, 9, 2]. There are still three items.

This worksheet uses positions starting at one. The original list is four, seven, two. Replacing the value at position two with nine produces four, nine, two and keeps the length three.
This worksheet uses positions starting at one. The original list is four, seven, two. Replacing the value at position two with nine produces four, nine, two and keeps the length three.
Question 1 What is items[2] before any update?
Question 2 What is the list after replacing position 2 with 9?
Question 3 How many entries are present after the replacement?
Question 4 Which position is outside this three-entry list?
Question 5 A language uses zero-based indexing. Where is its first item?