Unit 2. Developing Programming Tools
Revision Date: Jun 11, 2020 (Version 3.0)Summary
This lesson answers the question "What is a list and what can I do with one?". Students will find the answer to this question by exploring list operations and methods, as well as investigating how lists are modeled in real-world situations.
Outcomes
Student computer usage for this lesson is: required
In the Lesson Resources folder:
Other:
Journal:
Solicit some responses. Point out that different lists contain different kinds of information.
Say: In programming, we use data collections like lists for much the same reason as you use lists. A list is a sequential collection of data values that can be referenced using a single name and each value in the list is identified by an index. In some other programming languages, lists can be referred to by different names, such as an array. Today we are going simulate the way Python implements lists.
Optional: Review the string commands. Use the Lists.docx found in the Lesson Resources folder.
See the file called LinkyListy Role Play in the Lesson Resources folder. Note: You can change student names to students within your class.
Create a human “list of students”. See the file called LinkyListy Role Play in the Lesson Resources folder. Note: The directions below are an example. Change student names to students within your class.
Create a List of students by starting with an empty list and modify the list by following program below.
Designate a section of the board or a poster to act as the console/printer for output and a volunteer student to act as the printer driver.
Designate an area at the front of the room for the computer memory (and future students).
As students are called by the program, have each student come up to stand in front of the room where memory resides.
Have each student point to the student that follows them to form a linked list as they come to the front.
LinkyListy Role Play Program:
students = []
students.append('Joe')
students.append('Pat')
students.append('Alea')
students.append('Marta')
print(students)
#add additional commands to append or insert 4 or 5 more students
print(students)
print(len(students))
print(students[3])
students.reverse()
print(students)
print('David' in students)
students.sort()
print(students)
more = ['Tom', 'Laverne']
students = students + more
print(students)
pets = ['fish'*3,'dog']
del students[1]
del students[2:4]
students.insert(0, 'Jennifer')
students = students + pets
print(students)
print(students.index('Marta'))
1.2.2 Check for understanding:
Ask students to give an example and then explain the effect of several of the List methods. For example, students.append(“Zoe”) would add Zoe to the end of the list of students. students.insert(4, “Larry”) would add Larry at index position 4 and slide everyone else down one slot. This could be done as a placemat activity. Use different colored markers for each student to write the example. Turn placement and ask the next student to explain the effect.
Notes:
list: len(students)
print(students[2])
List Membership (in, not in): print(“Mary” in students)
Concatenation and repetition (+ *):Make a list of more Python commands on sentence strips. On back of the sentence strip, write the output or trace of the statement after it has executed onto the back of the card. Have one student hold the deck of cards and cycle through them. Ask students to read a card and predict the output. Meanwhile, students who are in the list will change positions to demonstrate the behavior within the list. The holder of the card provides feedback or congratulatoins in checking the correctness of classmate's responses.more = ["Tom", "Laverne"]
students = students + more
pets =['fish'*3,'dog']*2 # creates a list of ['fishfishfish','dog','fishfishfish','dog']
del students[2], del students[2:4]
stu = students.pop(2)
, stuLast = students.pop()
. The pop method pops (deletes and returns) an element at a given index or the last element if no index is provided.Say:
aList ← [value1, value2, value3,...] creates a new list that contains the values value1, value2, value3, and … at indices 1, 2, 3, and … respectively and assigns it to aList. |
aList ← [ ] creates a new empty list and assigns it to aList. |
aList ← bList assigns a copy of the list bList to the list aList. |
APPEND(aList, value) increases the length of aList by 1, and value is placed at the end of aList. |
REMOVE(aList, i) removes the item at index i in aList and shifts to the left any values at indices greater than i. The length of aList is decreased by 1. |
LENGTH(aList) evaluates to the number of elements currently in aList. |
List traversal - all elements: FOR EACH item IN aList { <block of statements> } The variable item is assigned the value of each element of aList sequentially, in order, from the first to last. |
Discussion: How are lists the same and how are they different from Strings?
Have the students do the Runestone lab activities for Lists to reinforce the above concepts. (http://interactivepython.org/runestone/static/thinkcspy/Lists/toctree.html)
Demonstrate that lists are mutable with the following activity
None
for the empty stalls.Discuss what kind of information can be acquired when a program processes data.
Describe some possible tables, diagrams, text, or other visual tools that could be used to communicate insight and knowledge gained from data.
Homework
Create a list of 5 students that contain the students' name, age, and hair color. Use a loop to extract the information for each student and print it out.
Teacher Note: Consider using the YumYumCupcake problem (see Formative Assessment) as part of tomorrow's opening exercises.
The first of two sessions creating and traversing lists in Python and EarSketch.
Discuss: Lists are collections of values combined into a single entity. What are some lists or other data collections used by music apps? How do the lists make the programs more appealing to their users?
Say: Today we will be learning to implement lists like those we acted out or use in our music apps.
Modify EarSketch Asg. 6.2 by adding a fourth music clip to the drums list. Modify the call to the makebeat function so it always uses the last music clip in drums. Test your program by adding a fifth music clip to drums.
The second of two sessions creating and traversing lists in Python and EarSketch.
Say: Lists are collections of values combined into a single entity. The collections of data in our lists yesterday were unchanging and we learned to access individual elements of the lists. Today we will use Python to add elements to a list and to obtain a part or slice of a list while the program is running.
Show the video: List appending and slicing.
Modify EarSketch Asg. 6.4 by adding another music clip to the clips list. Discuss with an elbow partner how the pan and vol lists change to correspond to the addition of the music clip. Make the appropriate changes to these lists in EarSketch Asg. 6.4.
In this session, we will review two topics required by the Create Performance Task - User Input and User Defined Functions
Say, we review defining our own custom functions. Once defined, we can use them like any other function.
The fundamental approach to solving large programming problems is breaking them down into small function definitions and then calling those functions to solve the original complex problem. You will use the strategy of breaking a problem down into smaller parts and implementing each part separately throughout your programming experience.
7. Chapter Seven: Defining Your Own Functions
Today we will define functions to respond to user input with dynamic lists in EarSketch.
Brainstorm some ways a user of an EarSketch program might want to affect the output produced by an Easketch program.
Choose either topic from the brainstormed list or the sample provided in User Input and Programmer Defined Functions below for students to implement. If you choose to implement suggestions brainstormed by students you may want to provide User Input and Programmer Defined Functions as an example.
Say: The exam reference sheet operations on lists include means of accessing and assigning values to a list and procedures for inserting, appending and removing list values.
Say: Students will have a copy of the entire exam reference sheet while they take the end of course exam from the College Board. The exam reference sheet also includes the FOR EACH item IN aList { }.
Have students move the user input section to a fourth user defined function. Students are to describe the algorithm used in their user input function in their own words.
Questions in the AP Classroom Question Bank may be used for summative purposes.
Sixty of the 80 questions are restricted to teacher access. The remaining 20 questions are from public resources.
Questions are identified by their initial phrases.
Two grids are shown below. Each grid contains a...
Consider the following program, which is intended
In the program below, the initial value of x is 5
The code fragment below is intended to display ...
The code fragment below is intended to display ...
yumYumCupcakes = ["chocolate mousse" *3, "vanilla creme", "strawberry fluff", "chocolate mousse"*2].
Have a customer purchase a vanilla creme cupcake if there are any, check how many chocolate mouse cupcakes are in the display case, bake some more, and add them in. Drop one cupcake on the floor and throw it away.Paired programming: Make a zoo of animals and demonstrate the use of at least 6 different list operations and methods. Try to make a story with your code.