AP CSP CodeX - Traversing a List Lesson 3

Traversing a List Lesson 3 Lesson Plan

Traversing a List Lesson 3

Students traverse a list to check if an item is in the list, and to filter a list into a sub-list.

⏰ 45 min 🎯 Grades 9-12 💻 CodeSpace 🎮 CodeX 🐍 Python
View Lesson Outline
📋

Overview

Students work on computers in the CodeSpace Sandbox. This lesson is not part of a mission. Students create three new programs: Class_Schedule, Number_Sorter and Animal_Sorter. Each program is fairly short. Students traverse a list to check if an item is in the list, fill lists using append() and input from the Console panel, and filter a list to create a sub-list. They complete the Wrap-Up questions on the Activity Guide.

🎯 Mission Goal: Students will traverse a list to check if an item is in the list, and to filter the list.

🎯

Learning Targets

  • I can use the console panel to input data.
  • I can traverse a list and check if a given answer is in the list.
  • I can create a list by appending random numbers.
  • I can create a list by appending input from the user.
  • I can filter a list to create a sub-list.
💡

Key Concepts

  • An empty list can be filled with data by using the append() function.
  • The console panel can be used for input as well as output.
  • The input() function returns a string. If an integer is needed, it must be converted using int().
  • The display.print() function uses strings. To print an integer, convert it using str().
✅

Assessment Opportunities

☑️

Success Criteria

  • Use input() and the console panel to fill a list
  • Fill an empty list using append()
  • Traverse a list and check if an item is in the list
  • Traverse a list to filter by a condition, creating a sub-list
  • Traverse a list and filter by checking each item, comparing it to a second list
📁

Digital Resources

🧰

Classroom Materials

  • ▸CodeX
  • ▸USB-C cable
  • ▸Computer
🚀

Extensions & Cross-Curricular

ExtensionStudents change Number_Sorter to keep the numbers greater than the chosen number, or only the numbers between two chosen numbers, then compare the results with their original program.
ExtensionStudents change Animal_Sorter to also build a second sub-list of the animals the user entered that are not in the first list, then display both sub-lists on the CodeX.
MathStudents predict how many of 20 random numbers from randrange(50) will be less than 20, run Number_Sorter several times, and compare their prediction with the results.
ScienceStudents make a first list of animals in one classification, such as mammals or birds, then enter a mixed set of animals and use Animal_Sorter to filter out the ones that belong to that classification.
Lang ArtsStudents write a short explanation for a classmate who missed the lesson, describing the difference between checking if an item is in a list and filtering a list to make a sub-list, with an example of each.
🔤

Vocabulary

▾
Traverse - Traveling or traversing through a list one element at a time, in order, starting with index 0 (first element) and going through to the last element (index len-1).
Data abstraction - The technique that simplifies complex data, making data easier to manage and use.
Matrix - A list of lists, or a 2D list. Each element in the list is a list itself.
Sequence - Executing code line by line, one after another, in order.
Selection - Decision points in code; a condition (this isn't in the documentation but is used in AP CSP).
Iteration - The repeating portion of an algorithm; code that repeats until a given condition is met or a specified number of times.
🐍

New Python Code

▾
def check_schedule(course): for item in schedule: if item == course: display.print("On the schedule")For loop to look for an item in a list.
def check_schedule(course): if course in schedule: display.print(course) display.print("On the schedule") def check_schedule(course): if course in schedule: display.print(course) display.print("On the schedule") else: display.print(course) display.print("Not on schedule")Specialized loop to look for an item in a list.
course = input("Enter a class: ")Input a string value on the Console.
number = int(input("Enter a class: "))Input an integer value on the Console.
def fill_list(): for i in range(20): number = random.randrange(50) numlist.append(number)Fill an empty list with random numbers.
def filter_list(): for item in numlist: if item < 20: filteredlist.append(item)Filter a list by creating a sub-list of items that meet a criteria.
def print_list(): display.print("From your random") display.print("list of "+str(len(numlist))) display.print("numbers, "+str(len(filteredlist))) display.print("were less than ") display.print(str(20)) display.print() display.print(filteredlist)Print a message with a list on the CodeX display.
def filter_list(): filteredlist = [] for item in my_animals: if item in farm_animals: filteredlist.append(item) display.print("Farm animals you listed:") display.print(filteredlist)Filter a list by comparing it to another list. Then print the list.
📐

Standards

▾

CSTA Standards - Grades 9-10

3A-CS-03 3A-AP-13 3A-AP-14 3A-AP-15 3A-AP-16 3A-AP-17 3A-AP-18

CSTA Standards - Grades 11-12

3B-AP-10 3B-AP-11 3B-AP-14 3B-AP-15 3B-AP-17 3B-AP-22 3B-AP-23

AP CSP Framework

AAP-1.D - Develop data abstraction using lists to store multiple elements.
AAP-2.N - Write expressions that use list indexing and list procedures.
AAP-2.O - Write iteration statements to traverse a list.
Computational Thinking Practice 3.B - Use abstraction to manage complexity in a program.
Computational Thinking Practice 3.C - Explain how abstraction manages complexity.
Computational Thinking Practice 4.C - Identify and correct errors in algorithms and programs, including error discovery through testing.
📝
Preparing for the Lesson
  • Prepare the Activity Guide to give to students. It is used for notes during the lesson and for the Wrap-Up questions at the end.
  • If you have a word wall, or another form of vocabulary presentation, prepare the terms.

🧑‍🏫
Teacher Notes
  • Students complete this lesson on the computer, using the Sandbox in CodeSpace for programming. This lesson is not part of a mission.
  • Students create three new programs. Each program is fairly short. The programs are:
  • Class_Schedule
  • Number_Sorter
  • Animal_Sorter
  • Solution code for all finished programs is provided.
  • Follow the slides for instructions and guidance.
🗺️

Lesson Outline

This outline gives one way to present the lesson.

🗣️Warm-up

Review loops and list traversal with students. Slide 2: Students answer the questions in the Activity Guide.

  • Discuss how you create a list and access an element in the list.
  • Discuss how you traverse a list.
  • Discuss the structure of a for loop.
Teaching tip: Go through slides 3-4. You can review verbally, or have students write answers and then do a think-pair-share.
💻Example #6 - Class_Schedule

💻 Students create a new program and can take notes in the Activity Guide.

Teaching tip: Go through slides 5-11. They give step-by-step instructions for creating a new program that traverses a list to see if a specific value is in the list. It also shows inputting data on the Console.
💻Example #7 - Number_Sorter

💻 Students create a new program and can take notes in the Activity Guide.

Teaching tip: Go through slides 12-23. They give step-by-step instructions for creating a new program that creates a sub-list from items in a list that meet a criteria. It will also fill an empty list with random numbers. The last step will display a message on the display screen and show the sub-list.
💻Example #8 - Animal_Sorter

💻 Students create a new program and can take notes in the Activity Guide.

Teaching tip: Go through slides 24-28. They give instructions for defining the two lists and then traversing the second list to create a sub-list.
📝Review

💻 Slides 29-31: Students do not use the computer for this activity.

Teaching tip: Review the ways to traverse a list. Then students complete the Wrap-Up questions on the Activity Guide.

✅ The Activity Guide and three programs are complete and ready to turn in.

🧑‍🤝‍🧑Wrap-Up

✅ Formative assessment:

  • "Wrap Up" questions at the end of the Activity Guide
  • Daily reflection journal or Google Form
  • Class discussion on what they learned about lists
  • Exit ticket