Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Computational Thinking · Grade 6-8

Array

An ordered collection of values stored together under a single name and accessed by their numeric index position, so you can store, retrieve, and process many related values with loops and index lookups.


Quick Answer

An ordered collection of values stored together under a single name and accessed by their numeric index position, so you can store, retrieve, and process many related values with loops and index lookups. Use an array when you have a list of things of the same kind, such as test scores, items in a cart or the pixels in one row of a picture, and want to reach any one of them by its position. Counting starts at 0: in scores = [95, 87, 92], scores[0] is 95 and scores[2] is 92.

Why This Matters

Once values sit in an array, a few lines of code can handle a thousand of them: a loop adds every score, finds the highest, or checks each item in a cart. Without arrays you would need a separate variable for every value (score1, score2, score3 and so on) and new code each time the list grew. Most array bugs come from one counting rule: the last index is one less than the length.

Intuitive Explanation

Suppose a teacher records three quiz scores. Without an array you would write score1 = 95, score2 = 87, score3 = 92: three names, three lines, and a fourth name the moment a fourth quiz is added. An array puts all of them under one name, scores = [95, 87, 92], and the position number in square brackets picks one out. scores[1] is 87. Change it with scores[1] = 90 and the other two values stay exactly where they were.

Positions start at 0, not 1. The first value is scores[0] and the last is scores[2], so an array of length 3 has indexes 0, 1 and 2. In general the last index is the length minus 1. Asking for scores[3] asks for a fourth box that does not exist; Python stops with an 'index out of range' error, and JavaScript quietly hands back undefined.

Because the position is just a number, a program can compute it. A loop with i going 0, 1, 2 reads scores[i] each time, which is how one short piece of code totals a list of any size. That is the difference from a single variable such as highest = 95: a variable holds one value at a time, even if that value changes. A phone's contact list can look like an array too, but you find a contact by name, not by position number. That is a lookup table (often called a dictionary), a different structure.

Think of it like this. An array behaves like the wall of numbered mailboxes in an apartment lobby: one building address, one box per apartment, and the mail carrier goes straight to box 4 without opening boxes 0 to 3. Two differences: an array's numbering starts at 0, and there is no box past the last one.

Core idea. Arrays store multiple related values under one name, making it easy to iterate over them all.


When to Use

Reach for an array when several values of the same kind belong together and you want them by position: the fifth song in a playlist, the first and last item in a cart, every score in a class. Words like list, each and first/last are strong signals. If there is only one value that changes, a plain variable is enough. If you look things up by a name rather than a number (a phone number by person), use a dictionary instead.

Tip. Ask: Are many values of the same kind kept under one name and picked out by a position number?

How to Recognize It

Before choosing an array, count how many values you need to keep and check whether their order matters.

  1. Are there several values of the same kind, like ten scores or five names?

    Yes suggests an array. A single value, such as one score, belongs in a Variable.

  2. Do you need to reach a value by its position, such as the first or the third?

    Yes means use an index, which starts at 0 in Python and JavaScript, so the third item is at index 2.

  3. Is the question about doing the same thing to every value in turn?

    Yes means Iteration is working on the array, usually with a loop that visits each index.

  4. Are the items the letters of a single piece of text?

    Yes points to a String, which is also indexed from 0 but holds characters, and in Python you cannot change one of them in place.

Array vs Variable vs String vs Iteration

Arrays sit beside variables, strings and loops in almost every program, so their jobs blur. Ask whether you are holding many values in numbered slots, one value, a piece of text, or repeating an action.

Array

Meaning
An array holds many values in order under one name, each reached by its index.
Key test
Many separate values, numbered from 0, under one name.
Formula
A[0],A[1],…,A[n−1]
Example
marks = [7, 9, 6], so marks[1] is 9 and len(marks) is 3.

Variable

Meaning
A variable holds a single value under a name.
Key test
One value, with no index.
Example
best = 9 keeps only the top mark.

String

Meaning
A string is text, a sequence of characters that can also be read by index.
Key test
The items are the characters of one piece of text.
Example
word = 'code', so word[0] is 'c'.

Iteration

Meaning
Iteration repeats a block of code, often once for each item in an array.
Key test
The action is repeated; the array is only what it acts on.
Example
for m in marks: total = total + m adds up all the marks.

Formula & Notation

Worked Examples

Example 1 — Read and change items by index

Easy

cart = ['apple', 'bread', 'milk', 'eggs']. What are len(cart), cart[1] and cart[len(cart) - 1]? Then the program runs cart[2] = 'juice'. What does cart hold now?
Show the steps and the answer
  1. Number the positions from 0: apple is 0, bread is 1, milk is 2, eggs is 3.

    An index counts steps from the start, so the first item is 0 steps away.

  2. Count the items: len(cart) = 4.

    Length counts items starting from 1, so it is always one more than the last index.

  3. Read cart[1] = 'bread' and cart[len(cart) - 1] = cart[3] = 'eggs'.

    len(cart) - 1 reaches the last item whatever the length is, which is safer than typing 3.

  4. Apply cart[2] = 'juice': the box at index 2 now holds 'juice' instead of 'milk'.

    Assigning to one index replaces just that value; the other three items and the length do not change.

Answer: len(cart) = 4, cart[1] = 'bread', and the last item is 'eggs'. After the change, cart = ['apple', 'bread', 'juice', 'eggs'].

Takeaway: An index is the number of steps from the start, so the last index is always the length minus 1.

Example 2 — A running total is not an array

Standard

A program runs total = 0, then total = total + 95, total = total + 87, total = total + 92. Ana says total is an array because it holds three scores. Is she right? What does total hold at the end, and can the program still get the score 87 back?
Show the steps and the answer
  1. Trace total: 0, then 95, then 95 + 87 = 182, then 182 + 92 = 274.

    Writing the value after each line shows that total only ever holds one number.

  2. Look for an index: there is no total[0] or total[1], only the name total.

    Without positions there is nothing to select, so the earlier values are not stored anywhere.

  3. Ask whether 87 can be recovered: no, because each assignment overwrote the value before it.

    A variable keeps its latest value and forgets the rest; an array keeps every value side by side.

  4. To keep the scores, store them as scores = [95, 87, 92] and add them with a loop; scores[1] is then still 87.

    The array keeps the individual scores and the loop can still produce the total, 274.

Answer: No. total is a single variable that ends at 274, and 87 is lost. Storing scores = [95, 87, 92] keeps all three, with scores[1] = 87.

Takeaway: Several values passing through one name is a variable being updated. An array holds them all at once, each at its own index.

Example 3 — Find the mistake: a loop that runs off the end

Application

temps = [12, 15, 11, 18, 14] holds five days of noon temperatures in °C. Maya wants the total and writes: total = 0, then for i from 1 to 5: total = total + temps[i]. She says: "There are 5 days, so i goes from 1 to 5." Find the mistake and correct it.
Show the steps and the answer
  1. Trace her loop: i = 1 adds 15, i = 2 adds 11, i = 3 adds 18, i = 4 adds 14, then i = 5 asks for temps[5].

    Writing out each value of i shows exactly which boxes the loop touches.

  2. Notice that temps[5] does not exist: five items use indexes 0 to 4.

    Maya treated the length, 5, as the last index. Python stops with an index error; JavaScript adds undefined and the total becomes NaN.

  3. Notice that temps[0] = 12, the first day, was never added.

    Starting at 1 skips the first box, the other half of the same off-by-one slip.

  4. Fix the range: for i from 0 to 4, written as i < len(temps).

    Testing i < len(temps) works for a list of any length and never reaches the missing box.

  5. Re-trace: 12 + 15 + 11 + 18 + 14 = 70.

    All five readings are now counted exactly once.

Answer: Loop i from 0 to 4 (i < len(temps)). The total is 70, an average of 70 ÷ 5 = 14 °C.

Takeaway: A list of length n uses indexes 0 to n − 1, so start at 0 and stop while i < n.

Where You'll See It

  • Egg carton slots

    A dozen eggs sit in numbered slots; you grab 'the egg in slot 3' the same way you read an array by its index position.

  • Lockers in a hallway

    Each locker has a number and holds one item; an array is a row of numbered storage spots you reach by index.

  • A playlist of songs

    Songs are stored in order under one playlist name, and you can jump to song number 5 or loop through them all, just like iterating an array.

Common Mistakes

  1. The slip: The first item in an array is at index 1.

    Why it's tempting: We count things starting at 1 in everyday life, so item one feels like index 1.

    Instead: In Python, JavaScript, Java and C, arrays are zero-indexed: the first element is at index 0, so a 5-element array uses indices 0 through 4.

  2. The slip: An array of length 5 has a valid index 5.

    Why it's tempting: Length 5 makes index 5 feel like it should exist.

    Instead: Length 5 means valid indices are 0 to 4; index 5 is out of bounds and causes an error, since the last valid index is length minus 1.

  3. The slip: The array's length equals the last index.

    Why it's tempting: Both are numbers tied to the array's size, so they feel like the same number.

    Instead: For a zero-indexed array, the last valid index is length minus 1, not the length itself.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. `scores = [95, 87, 92, 78]`. What are `scores[0]`, `scores[2]` and `len(scores)`?

    Hint: Indexes start at 0. Length counts every value.

  2. `days = ["Mon", "Tue", "Wed", "Thu", "Fri"]`. What is the last valid index, and what is `days[len(days) - 1]`?

    Hint: Find the length first, then subtract 1.

  3. Trace: `a = [3, 8, 1]`, then `a[1] = a[0] + a[2]`, then `a.append(10)`. What is `a` now?

    Hint: Read `a[0]` and `a[2]` before you change `a[1]`.

  4. Trace: `nums = [4, 7, 2, 9]`, `total = 0`, then `for n in nums: total = total + n`. What is `total`, and what is the average of the list?

    Hint: Add each value in turn, then divide by how many values there are.

  5. A program keeps `temperature = 72` and later sets `temperature = 75`. Is this an array or a single variable?

    Hint: How many values are stored at the same time? Is there an index?

  6. A list `items` has 5 elements. A student writes `items[5]` to get the last one. What goes wrong, and what is correct?

    Hint: List the valid indexes for 5 elements.

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

Why do arrays start counting at 0?

The index tells the computer how far to move from the start of the array. The first item is at the start, so it is 0 steps away; the next is 1 step away, and so on. To find marks[2], the computer jumps 2 slots past the start, which is quick however long the array is. That is also why the last index is the length minus 1.

Do all languages count from 0?

No. Python, JavaScript, Java and C count from 0, but Scratch lists start at item 1, and so do Lua and MATLAB. When you move from Scratch to Python, the same list of three names goes from items 1, 2 and 3 to indexes 0, 1 and 2. Check which rule your language uses before you trust an index.

What is the difference between an array and a list?

In everyday talk the words are used loosely. Strictly, a classic array, as in C or Java, has a fixed length and holds one type of value, so the computer can lay it out in one block of memory. A Python list can grow, shrink and mix types, such as [3, 'cat', True]. Both are reached by index in the same way.

Can an array hold other arrays?

Yes, and an array of arrays works like a grid. In grid = [[1, 2, 3], [4, 5, 6]], grid[1] is the whole second row, [4, 5, 6], and grid[1][2] is 6: row 1, then position 2 in that row. Games use this to store boards such as tic-tac-toe, and pictures are stored as rows of pixels in the same way.

Learning Path

← Before

VariableData Types
Array

You are here

Start with Variable (a name that holds one value) and Data Types (what kind of value it is). An array adds many values under one name, reached by an index that starts at 0. Next, Iteration walks through an array with a loop, Searching finds a value in it, and Sorting puts its values in order.

See Also