Section 8.1
Lists & Traversal
Learning Goals
Students will write and evaluate expressions that use list indexing (AAP-2.N)
Students will use list traversal to access different elements in a list (AAP-2.N)
Students will implement list traversal as a new means to store and access data.
AAP-1.D: For data abstraction:
a. Develop data abstraction using lists to store multiple elements.
b. Explain how the use of data abstraction manages complexity in program code.
AAP-1.D.1: Data abstraction provides a separation between the abstract properties of a data type and the concrete details of its representation.
AAP-1.D.2: Data abstractions manage complexity in programs by giving a collection of data a name without referencing the specific details of the representation.
AAP-1.D.3: Data abstractions can be created using lists.
AAP-1.D.4: Developing a data abstraction to implement in a program can result in a program that is easier to develop and maintain.
AAP-1.D.5: Data abstractions often contain different types of elements.
AAP-1.D.6: The use of lists allows multiple related items to be treated as a single value. Lists are referred to by different names, such as array, depending on the programming language.
DAT-1.A.5: Abstraction is the process of reducing complexity by focusing on the main idea. By hiding details irrelevant to the question at hand and bringing together related and useful details, abstraction reduces complexity and allows one to focus on the idea.
Objectives and Description
The objective of this lesson is to introduce students to lists/arrays and begin teaching some of the operations associated with them. Students will learn about properly accessing elements in lists, how to combine lists, and how to traverse (iterate over) them.
Activities
Activity 8.1.1 (15 minutes)
Students and teacher will create a variable that stores a list of sounds (from the sound browser).
Pick 3 sounds to use, with no overlap in instrument type
Teacher will walk through how to access list elements for use in code, students follow along
By using the form listName[x], you can access the xth element in the list
Indices start at 0 for lists in python
Students will practice by accessing each list element and adding them to separate tracks.
example code:
fitMedia(instrumentList[2], 3, 1, 9)
this code accesses the element at index 2, places it on track 3, from measures 1 to 9
Activity 8.1.2 (45 minutes)
Teacher will walk through how to use a for loop to traverse a list
Students will create a new list within their script that contains 3 new sounds, then use the + operator to concatenate their two lists and store in a new variable
using form X.concat(Y) to append the contents of list Y to the end of list X
Students will modify their scripts to use a for loop instead of using individual fitMedia() calls to add the sounds from their concatenated list to separate tracks.
The list can be iterated over by using
Students should play elements 0-2 on measures 1-4 and elements 3-5 on measures 5-8
This can be done using conditionals within the for loop
5. Class Discussion. Have students explain how a list works, what data abstraction is, and how data abstraction manages the complexity of code in a program.