AP® Computer Science Principles review sheet from Aim for Five (aimforfive.com/csp/units/3/3-2)
Unit 3 · Topic 3.2
3.2 Data Abstraction
A list lets one variable hold a whole collection of values in order. This topic covers lists, elements and indexes (which start at 1 on the exam), strings as sequences of characters, and why using a list is called data abstraction: it makes programs simpler to write and to change.
Key terms
- list
- element
- index
- data abstraction
- string
- managing complexity
Lists, elements and indexes
A list is an ordered sequence of values. Each value in it is an element, and each element has a position number called its index. On the exam, the first element is at index 1, the second at index 2, and so on up to the length of the list.
aList ← [value1, value2, value3] creates a list and stores it in aList. aList ← [] creates an empty list. aList[i] gets the element at index i, and aList[i] ← x replaces it.
scores ← [88, 92, 75]
DISPLAY(scores[1])
DISPLAY(scores[3])
scores[2] ← 95
DISPLAY(scores[2])
This displays 88 75 95.
Any index below 1, or bigger than the number of elements, is an error that stops the program. colors[0] would stop the program, because the exam's lists have no index 0.
Other languages may call lists arrays, and many of them (Python, JavaScript, Java) start counting at 0. Videos and apps you practice with may do this, but on the exam, always start at 1.
Copying a list
aList ← bList gives aList a copy of bList. After that, changing one doesn't change the other:
original ← [5, 10, 15]
backup ← original
original[1] ← 100
DISPLAY(backup[1])
DISPLAY(original[1])
This displays 5 100. The backup kept the old first element.
Strings are sequences too
A string is an ordered sequence of characters, like "pizza" or "AP CSP 2027!". Spaces and punctuation are characters too. Like list elements, the characters in a string can be referred to by position, starting at 1: in "pizza", position 1 is "p" and position 5 is "a". You'll work with strings more in 3.4.
Data abstraction: why lists matter
Data abstraction means giving a collection of data a single name so you can use it without dealing with every individual piece. A list is the main way to do this on the exam.
Compare storing 30 quiz scores in 30 variables (score1, score2, ..., score30) with storing them in one list called scores. With separate variables, finding the average means writing out all 30 names, and adding a 31st score means changing the code in several places. With a list, one loop handles any number of scores, and adding a score is a single APPEND.
That's how a list manages complexity: the program is easier to develop (one loop instead of 30 lines) and easier to maintain (the code doesn't change when the number of items changes). A list can also hold different types of values, and it lets many related items be treated as one value, for example passed to a procedure in one argument.
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Index or error?
The list
pets ← ["cat", "dog", "fish", "bird"]has been created. What is the value of each expression, or does it cause an error? (a)pets[2](b)pets[LENGTH(pets)](c)pets[5]Show the solutionHide the solution
- Step 1: (a) Index 2 is the second element: "dog".
- Step 2: (b) LENGTH(pets) is 4, so this is pets[4], the last element: "bird". This pattern always gets the last element.
- Step 3: (c) The list has 4 elements, so index 5 is beyond the end. The program shows an error and stops.
Answer: (a) "dog" (b) "bird" (c) an error; the program stops.
- Example 2
Explaining how a list manages complexity
A music app stores a user's playlist as a list called
songs. Explain how using the list manages complexity in the program.Show the solutionHide the solution
- Step 1: Name the alternative: without a list, each song would need its own variable (
song1,song2, ...). - Step 2: Say what's harder without it: the code to play, shuffle or count songs would have to mention every variable by name, and a playlist can be any length, so you'd have to change the code whenever a user added or removed songs.
- Step 3: Say what the list makes possible: one loop works through every song no matter how many there are, and APPEND or REMOVE changes the playlist without changing any code.
Answer: Storing songs in one list means a single loop can process the whole playlist, so the code doesn't need a separate variable for each song and doesn't have to change when songs are added or removed.
- Step 1: Name the alternative: without a list, each song would need its own variable (
Common mistakes
- Starting at index 0. On the exam,
aList[1]is the first element andaList[0]is an error. - Saying a list manages complexity just because it "stores data" or "keeps things organized." Explain what would be harder without it, like needing many separate variables or rewriting code when the number of items changes.
- Thinking
aList ← bListlinks the two lists. It makes a copy.
On the exam
- Written Response 2(c) can ask how the list in your Create task manages complexity. Name what the list stores, describe how the program would have to be written without it, and explain why that would be harder to write or to change.
- Watch for index questions where the answer choices include an off-by-one value from counting at 0.
Connected topics
Videos
Check yourself
3 questions on 3.2 Data Abstraction. Pick an answer to see if you got it, and why.
What is displayed when the following code segment is run?
colors ← ["red", "blue", "green", "gold"]
DISPLAY(colors[2])
DISPLAY(colors[LENGTH(colors)])
A teacher's program stores quiz scores in 30 separate variables, named score1 through score30. The programmer rewrites it to store the scores in one list named scores. Which of the following is the most important benefit of this change?
A program stores a student's record with the statement record ← ["Ana", 16, true]. The list holds the student's name, age and whether the student has a parking permit. Which statement about this list is accurate?
0 of 3 answered