AP CSP CodeX - Traversing a List Lesson 2

Traversing a List Lesson 2 Lesson Plan

Traversing a List Lesson 2

Students use for loops to traverse multiple lists and a 2D list to control the CodeX pixels.

⏰ 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 modify Answer_Bot_traversals to use a for loop, then modify Pixels1_parameters to traverse two lists at the same time and finally one 2D list, or matrix. They complete the Wrap-Up questions on the Activity Guide, and optional challenges are available if time allows.

🎯 Mission Goal: Students will traverse a single list and multiple lists using a for loop in a program.

🎯

Learning Targets

  • I can use a for loop to reduce repeated code.
  • I can traverse multiple lists using a for loop.
  • I can traverse a 2D list, or matrix, using a for loop.
💡

Key Concepts

  • Traversing a list means accessing each element in a list, in order.
  • The easiest way to traverse a list is to use a for loop.
  • You can traverse a list multiple times using a for loop.
  • You can create a 2D (or 3D) list instead of having multiple lists.
✅

Assessment Opportunities

☑️

Success Criteria

  • Use a for loop to turn on pixels
  • Traverse multiple lists in a for loop
  • Create a 2D list
  • Traverse the 2D list in a for loop
📁

Digital Resources

🧰

Classroom Materials

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

Extensions & Cross-Curricular

ExtensionStudents add a new color and brightness entry to the pix_info matrix and run the program, then explain why the for loop picks up the new entry without any change to the code.
ExtensionStudents write a function that traverses pix_info and turns on only the pixels with a brightness above 50, so they practice using a list traversal to select items.
MathStudents describe the shape of pix_info in rows and columns, then predict the value of pix_info[5][1] before running the program to check.
ScienceStudents record a small set of science data, such as temperature readings at three times of day for three days, store it as a 2D list, and traverse it with a for loop to print each reading.
Lang ArtsStudents write a short explanation of procedural abstraction and data abstraction, using their own everyday example, for a classmate who missed the lesson.
🔤

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

▾
pix_info = [ [(77, 158, 100), 75], [YELLOW, 50], [(203, 182, 6), random.randrange(100)], [RED, 50], [random.choice(COLOR_LIST), 100], [BLUE, 75], [BLACK, 0] ]Create a matrix (a list of lists).
for index in range(len(pix_info)): turn_pixels(pix_info[index][0], pix_info[index][1])For loop that traverses a matrix.
📐

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
  • Make sure students can open their Answer_Bot_traversals and Pixels1_parameters programs in the CodeSpace Sandbox. Students who need the original code can use the most recent version of each program from the class dashboard.
  • 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.
  • The lesson includes two challenges at the end. These are optional! This lesson is a little shorter than the first, so if there is time, have students try a challenge. But don't extend the lesson just to try the challenges.
  • Students modify the Answer_Bot_traversals and Pixels1_parameters programs.
  • The best experience will come from students modifying their own code. However, we want all students to be engaged, so you can give them the original code to modify if needed.
  • The most recent version of each program can be found in the class dashboard. If you are giving code to students, use the solution code from these lessons:
  • Answer_Bot_traversals: Unit 3 Traversing a List Lesson 1
  • Pixels1_parameters: Unit 3 Functions with Parameters Lesson 1
  • 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 questions in the Activity Guide.
  • What do you remember about a for loop?
  • What do you remember about traversing a list?
💻First Example

💻 Students take notes in the Activity Guide and modify the Answer_Bot_traversals program.

Teaching tip: Go through slides 3-6. They give instructions for modifying the program with a for loop to reduce repeated code. They also show a comparison of before and after the modifications.
📖Second Example - Traversing a List Review

💻 Students take notes in the Activity Guide.

Teaching tip: Go through slides 7-8. They give reasons for traversing a list.
💻Second Example - Traversing a List Example #4

💻 Students use the Pixels1_parameters program from Unit 3 Functions with Parameters Lesson 1.

Teaching tip: Go through slides 9-16. They give instructions for traversing the lists.
💻Second Example - Traversing a List Example #5

💻 Students continue with the Pixels1 program. This example introduces a matrix and how to define and traverse one.

Teaching tip: Go through slides 17-21. They give instructions for defining and traversing a matrix (a 2D list).
📝Summary Review

💻 Go through slide 22 and discuss as needed. If you are not doing the challenges, skip to slide 25 for the wrap up.

🏆Challenges (optional)

💻 Complete the challenges if you have time. They are optional. If you do not do the challenges, you can delete or ignore the space on the Activity Guide for the challenges.

Teaching tip: Go through slides 23-24. Each one is a programming challenge. They can be done in any order, or skipped.
📝Review

💻 Students do not use the computer for this activity.

Teaching tip: Complete the Wrap-Up questions on the Activity Guide.

✅ The Activity Guide and 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