S3U

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
5 questions0m 0s
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?