DoAssignment study guide

A1.5 · Explain one-dimensional arrays, indexes, and bounds

Learn to explain one-dimensional arrays, indexes, and bounds through clear examples and targeted practice.

Ontario Grade 11 Computer Science

Programming Concepts and Skills

ICS3U study topic A1.5

Imagine a row of numbered lockers, each holding one item. A one-dimensional array is similar: it keeps several values in an ordered row, and an index tells you which position to refer to. The idea is useful whenever a program needs to keep related values together, such as the scores from several quizzes. This lesson focuses on the array, its indexes, and its bounds. Examples use Python 3, which numbers list indexes starting at zero; another classroom language may use different array rules.

What you will learn

  • Explain how a one-dimensional array stores an ordered group of values.
  • Explain how an index identifies a value's position in an array.
  • Identify the valid index bounds and recognize an index that is outside them.
  • Trace a short Python 3 example involving an array.

1. What an array represents

An array is a group of values stored under one name and arranged in order. In a one-dimensional array, the values form a single sequence rather than rows and columns. Each position holds one value, and positions have an order: the first, next, and last.
For example, a teacher might record three quiz scores in the sequence 72, 85, 91. These values belong together because they are all quiz scores. The whole collection can be referred to by one array name, while an individual score can be selected by its index.
Think of an array as a row of labelled spaces. The labels are indexes; the contents are the stored values. The labels do not have to match the values. If the value at index 1 is 85, that does not mean the value itself is 1.
  • A one-dimensional array is an ordered sequence of values.
  • An index identifies a position; it is not the value stored there.
  • The array's order matters because each value has a particular position.

2. Indexes and bounds

An index is a position label used to select an array element. In Python 3, list indexes start at zero. Therefore, index 0 refers to the first value, index 1 to the second, and so on. This is a Python-specific convention, not a rule that every programming language must follow.
The bounds are the limits of the valid indexes. For a Python list containing 4 values, the first valid index is 0 and the last valid index is 3. In general, if an array has nn values and uses zero-based indexes, its valid indexes run from 0 through n−1n - 1. An index below 0 or at least nn is outside these bounds.
An index must be a whole-number position that identifies an existing element. For example, a list with four values has no element at index 4: its fourth and final element is at index 3. Trying to use an index outside the bounds is an invalid access. Be careful to distinguish the number of elements from the final index.
0≤i<n0 \leq i < n
  • Python 3 list indexes begin at 0.
  • For a zero-based array with nn elements, the valid bounds are 0 and n−1n - 1.
  • The number of elements is one greater than the final valid index.

3. Reading an array and checking its positions

To work with an array, first identify its contents and order. Then identify the index you want, and check that the index lies within the bounds before selecting the value. This simple check prevents confusing an element's position with its place in everyday counting.
A useful input-process-output description is: input, a list of quiz scores; process, select the value at a stated index; output, that selected score. The list itself is the input data, and the index tells the program which part of that data to use.
The example uses Python 3 list syntax. The code is shown as text and is not being run. To trace it, match the requested index to the labelled position in the array. Classrooms may use another language, whose syntax or indexing convention can differ.
  • Write down the array's values in order before tracing an access.
  • Match the requested index to its position label.
  • Check the bounds before deciding whether an access is valid.

4. Bounds in everyday array reasoning

Bounds matter whenever a program refers to a position. If a list has one value, its only valid Python index is 0. If it has no values, there is no valid element index. In either case, the number of available elements determines which positions exist.
A safe mental check is to count the values, then label them starting with 0 when tracing Python. The last label should be one less than the count. If a requested label is not among the labels shown, the program cannot select an element there.
These ideas apply to any one-dimensional array, even when its values are words, temperatures, or other data. The array type of data does not change the meaning of an index or the need to stay within bounds.
  • An array's bounds depend on how many elements it contains.
  • Different kinds of values can be stored; indexes still identify positions.
  • Do not assume that an index equal to the element count is valid.

Hand trace for the temperature example

ActionArray or variable stateResult
Starttemperatures = [12, 15, 11, 18]Four values; valid indexes 0 to 3
Select index 2chosen = 11Index is within bounds
Print chosenchosen = 11Expected output: 11

Worked example

Select one quiz score

A Python 3 list contains the quiz scores 68, 74, and 89, in that order. What value is selected by index 1, and is that index within bounds?
Quiz scores and Python indexes
Index → stored value068174289

The index labels identify positions; the cells show the stored scores.

Python 3

scores = [68, 74, 89]
selected = scores[1]
print(selected)
  1. Label the positions
    Python 3 uses zero-based indexes, so the first score is at index 0, the second at index 1, and the third at index 2.
  2. Check the requested index
    There are three scores, so the valid indexes are 0, 1, and 2. Index 1 is within those bounds and identifies the second score.
  3. Read the selected value
    The value in the second position is 74, so selecting index 1 gives 74.
Answer: Index 1 is valid and selects 74.
Check: The selected value is the second item, not the item whose value happens to be 1.

Worked example

Recognize an index outside the bounds

A Python 3 list stores four team names in order: Cedar, Maple, Birch, and Pine. Is index 4 valid, and what is the last valid index?
Four team names
Index → stored value0Cedar1Maple2Birch3Pine

The four existing positions end at index 3; index 4 has no corresponding element.

  1. Count the elements
    The list contains four names. With Python's zero-based indexes, their positions are labelled 0, 1, 2, and 3.
  2. Compare with the bounds
    Index 4 is not one of the labels assigned to a name. The last valid index is 3, so index 4 is outside the bounds.
  3. State what can be selected
    The final name, Pine, is at index 3. There is no fifth list element at index 4.
Answer: Index 4 is invalid for this list; the last valid index is 3.
Check: There are four elements but the last index is 3, because Python indexes start at 0.

Worked example

Trace a value chosen by an index

A Python 3 list contains the temperatures 12, 15, 11, and 18. The program selects the value at index 2 and prints it. Determine the expected output and verify that the access is within bounds.
Temperature values
Index → stored value012115211318

Index 2 labels the third value, 11.

Python 3

temperatures = [12, 15, 11, 18]
chosen = temperatures[2]
print(chosen)
  1. Inspect the list
    The input list has four values. Its Python indexes are 0, 1, 2, and 3, so index 2 is within the valid bounds.
  2. Trace the selection
    Starting at index 0, the values are 12, 15, then 11 at index 2. The selected variable therefore holds 11.
  3. Determine the printed value
    The print statement displays the value held by the selected variable. Tracing the statements gives the expected output 11; this is a hand trace, not a claim that the code was executed.
Answer: The access is valid, and the expected output is 11.
Check: Index 2 means the third position in a zero-based Python list.

Common mistakes and how to avoid them

Treating the first Python index as 1.
Correction: For Python 3 lists, the first index is 0. Label the positions before selecting a value.
Assuming the number of elements is also the last valid index.
Correction: For a zero-based Python list with four elements, indexes run from 0 to 3; index 4 is outside the bounds.
Confusing an index with the value stored there.
Correction: An index is a position label. Look at the element in that position to find its value.

Lesson summary

  • A one-dimensional array is an ordered sequence of values under one name.
  • An index identifies an element's position.
  • Python 3 list indexes start at 0.
  • For a zero-based array of nn elements, the valid indexes are 0 through n−1n - 1.
  • Check that an index is within the array's bounds before selecting its value.

Check your understanding

Question 1

A Python 3 list has the values 9, 4, and 7. What value is at index 0?
  1. 9
  2. 4
  3. 7
  4. There is no valid index 0
Show answer and explanation
9
Python indexes start at 0, so index 0 identifies the first value, 9.

Question 2

A Python 3 list contains five elements. Which is its last valid index?
  1. 3
  2. 4
  3. 5
  4. 6
Show answer and explanation
4
The five positions are indexed 0, 1, 2, 3, and 4. Therefore, 4 is the last valid index.

Question 3

A Python 3 list has two elements. Is index 2 valid?
  1. Yes, because there are two elements
  2. Yes, because indexes start at 1
  3. No, because the valid indexes are 0 and 1
  4. No, because a list cannot have index 0
Show answer and explanation
No, because the valid indexes are 0 and 1
Two elements have Python indexes 0 and 1. Index 2 is outside the bounds.

Key terms

One-dimensional array
An ordered sequence of values arranged as one list of positions.
Index
A position label used to identify an element in an array.
Bounds
The limits of the valid indexes for an array.
Element
One value stored at a position in an array.

Continue through ICS3U

View the complete ICS3U Ontario Grade 11 Computer Science curriculum and lessons

About this lesson and its review

Published by DoAssignment. This reviewed lesson follows Ontario Grade 11 Computer Science (ICS3U), expectation A1.5. It is a study resource, not an official curriculum publication.

Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.

Official curriculum reference

Report a correction or ask a question