Head office, Rana Pratap Bagh, Delhi011 2721 1512+91 97116 99511

Free lesson with a practice sheet

Lists and arrays

A list or array stores many values under one name; learn about index numbers starting at 0, and loop through a list to add or find things.

Get the whole book: Computers and coding for classes 9 and 10 (6 lessons, free to print)

Let us begin

The big idea

A class has 40 students. Making 40 separate variables for their marks would be painful. Instead, a program keeps all the marks in one list, also called an array. Each value sits in a numbered slot so the program can reach any of them.

A list is a row of values stored under one name. We write it with square brackets: marks = [72, 85, 90, 64]. This list has four items. Its length is 4.

Each slot has an index number. In most programming languages the first slot is index 0, not 1. So marks[0] is 72, marks[1] is 85, marks[2] is 90 and marks[3] is 64. The last index is always one less than the length. Asking for marks[4] is an error because there is no such slot.

You can change one slot: marks[1] = 88 replaces 85 with 88. You can add to the end: append 70 makes the list [72, 88, 90, 64, 70] with length 5.

The real power comes from loops. To add all the marks: total = 0, then FOR i FROM 0 TO length - 1: total = total + marks[i]. To find the largest: biggest = marks[0], then for each item, IF item > biggest THEN biggest = item. The same short loop works for a list of 4 or 4,000.

An array of four marks with index numbersFour boxes in a row hold the values 72, 85, 90 and 64. Above each box its index 0, 1, 2 and 3 is written. The whole row is labelled marks, length 4. Below, a note shows marks[2] is 90 and that marks[4] does not exist.marks length = 4 index 0 index 1 index 2 index 3 72 85 90 64 marks[2] is 90 marks[4] does not exist: the last index is 3, one less than the length. One name, many values, each with an index
Index numbers start at 0. The last index is one less than the length.

Worked examples

See how it works

  1. Reading by index

    fruits = ["mango", "apple", "banana", "guava"]. fruits[0] is "mango", fruits[2] is "banana", and fruits[3] is "guava". The length is 4 and the highest valid index is 3.

  2. Adding up a list

    marks = [72, 85, 90, 64]. Trace total: start 0; i = 0, total = 72; i = 1, total = 157; i = 2, total = 247; i = 3, total = 311. The sum is 311, and the average is 311 divided by 4 = 77.75.

  3. Finding the largest

    nums = [14, 3, 27, 9]. biggest = 14. Compare 3: not bigger. Compare 27: bigger, so biggest = 27. Compare 9: not bigger. Final answer 27.

  4. Counting matches

    temps = [31, 35, 29, 36, 33]. Count the days above 32: count = 0; 31 no; 35 yes, count 1; 29 no; 36 yes, count 2; 33 yes, count 3. Three hot days.

Try it at home

An egg-tray array

You will need

  • An empty egg tray or a row of six cups
  • Small slips of paper or beans
  • A pencil and paper

What to do

  1. Write the index numbers 0, 1, 2, 3, 4, 5 on paper slips and place one in front of each cup. The first cup is 0.

  2. Put a different number of beans in each cup, for example 3, 7, 2, 5, 1, 4. This is your list: beans = [3, 7, 2, 5, 1, 4].

  3. Ask: what is beans[2]? Count the beans in the cup labelled 2 (answer 2). What is beans[5]? (answer 4).

  4. Now find the largest by looping: start with cup 0 as biggest and compare each next cup in turn, saying aloud whether you change your answer.

  5. Add up all the cups one at a time, writing the running total after each cup.

Practice sheet

Check what you learned

Write your answers in a notebook, or print this sheet and write on it. When you finish, mark your work with the answer key.

Practice sheet: Lists and arrays (Classes 9-10, Computers and coding)

  1. colours = ["red", "green", "blue"]. What is colours[0]?
  2. What is the length of the list above, and what is its highest valid index?
  3. nums = [5, 9, 2, 8]. What is nums[3]?
  4. After nums[1] = 10, what is the list?
  5. Add up [4, 11, 6, 9]. What is the total?
  6. What is the largest value in [17, 42, 8, 35]? Show the comparisons.
  7. Why is asking for marks[4] an error when the list has 4 items?
  8. How many items does a list with highest index 9 contain?
  9. In pseudocode, write a loop that counts how many items in a list are equal to 0.
  10. Why is a list better than 40 separate variables for 40 marks?

Answer key

Answers: Lists and arrays

Do the practice sheet first, then check here. If an answer is different from yours, read the lesson again and try once more.

  1. "red"
  2. Length 3, highest valid index 2.
  3. 8
  4. [5, 10, 2, 8]
  5. 30
  6. 42: start 17; 42 is bigger, so biggest = 42; 8 is not; 35 is not.
  7. Indexes run from 0 to 3, so there is no slot numbered 4.
  8. 10 items (indexes 0 to 9).
  9. count = 0; FOR i FROM 0 TO length - 1: IF list[i] = 0 THEN count = count + 1.
  10. One short loop can handle every mark, however many there are, and the list can grow or shrink.

What next

Keep learning

Get the whole book

All 6 computers and coding lessons for classes 9 and 10 on one page, with every practice sheet and answer key, ready to print.

Computers and coding for classes 9 and 10

All 136 lessonsAll printable books

Find out more

Want to learn more about computers and coding?

These are free, well-known places to explore. Ask a grown-up before you sign up for anything or type your name anywhere.

More places to learn, and free reading ideas

WhatsApp us