Unit 2. Developing Programming Tools
Revision Date: Jun 30, 2015 (Version 1.2)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
Overview
Session 1
Session 2
Student computer usage for this lesson is: required
In the Lesson Resources folder:
Other:
Review the string commands. As they enter the class, hand students a copy of the Warm-Up Get Ready for Lists.docx that is found in the Lesson Resources folder.
Discuss: What is a list? (A sequential collection of Python data values; each value is identified by an index.)
See the file calld LinkyListy Role Play in the Lesson Resources folder. Note: You can change student names to students within your class.
students = []; students.append("Mary"); students.append( “Jack”); students.append("Jill")
. Note: If we enclose the elements of the list in square brackets when appending multiple items, they are added as a sublist, not as individual elements.
list: print(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.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.
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/CCPS_Python/Lists/lists.html)
Demonstrate that lists are mutable with the following activity
None
for the empty stalls.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.
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.