Use for loops to repeat instructions and iterate over lists.
Lesson: For Loops
Time: 40+ mins
IntroductionIn this lesson, students will use for loops to repeat instructions and iterate over lists.
- For loop: A loop that automatically creates and updates the iterator variable. A for loop will iterate through each element in a list or each value in the object that is passed to it. The variable name entered after the "for" in the for loop will be the name of the element that is pulled from the list defined after the "in" part of the loop. The iterator variable is an element of the list rather than an index like with while loops. The “for” loop can be used to iterate on strings as well as lists. The “for” loop in Python has the following syntax:
Here is the syntax for iterating lists:
- Palidrome: A word that is spelled the same forward and back.
- Nested Loop: A loop that is contained within another loop.
- Identify differences between “while” loops and “for” loops
- Use “for” loops to iterate through lists and manipulate each element
- Count the number of shared letters in two strings and reverse strings
- Iterate over a range of numbers and on indexes
- Create programs to count calendar days depending on the year
- Use nested loops for drawing shapes and images
- Computers (1 per student) with student account access to Tynker.com
Warm-Up (5 minutes)
- It’s important for any successful programmer to develop strong perseverance skills. As a class, discuss some strategies (e.g., brainstorming with a neighbor, taking a short 1 minute break to look away from the screen, re-reading documents, breaking down problems into smaller components) that your students can use as motivation to keep working through a problem or situation despite obstacles and unexpected outcomes.
- What are some perseverance skills your students have applied so far in the Python 201 course?
Activities (35 minutes)Facilitate as students complete the For Loops modules on their own:
1. For Loops (Tutorial)
- Students will read a short document that explains “for” loops.
- Optional: Encourage students to experiment with the code in the examples. How did their changes affect the output?
- Are students struggling with their syntax for the “Try it yourself” puzzle? Direct their attention to the “for loop syntax” section.
- Tell students to click the “Next” button (located at the bottom of the document) to move on to the next module.
- In this module, students will read a short document that explains the iteration variable.
- Are students struggling with the “Add 1 to every value” puzzle? Give a hint: Tell students to include a “for” loop and one “in” operator as part of their solution.
- In this module, students will read a short document that explains how to use a “for” loop to iterate on strings as well as lists.
- Are students struggling with the “Reverse the name” puzzle? Give a hint: Tell students to use code from the “Reversing a string” example as a reference.
- Are students struggling with the “Palindrome” puzzle? Give a hint: Tell students to include an “else” statements as part of their solution.
- In this module, students will read a short document that explains how the number of elements in a list determines how many iterations there will be in a “for” loop.
- Are students struggling with the “Color the polygon” puzzle? Give a hint: Tell students to include the following as part of their solution:
t.left(360 / len(colors))
- Optional: Challenge the adventurous coder to create at least three different polygons.
- In this module, students will read a short document that explains the range function, which produces a list containing a sequence of numbers starting at 0 and ending before a given number. For example, “range(10)” will result in a sequence of numbers starting at 0 and ending at 9.
- Students will need to apply knowledge of math concepts (e.g., squared numbers and factorials) to solve three puzzles in this module. If students are struggling, encourage them to brainstorm possible solutions with a neighbor.
- In this module, students will read a short document that expands on iterating concepts learned in the previous lesson. Additionally, students will complete a DIY project where they will use nested “for” loops to draw their own spiral pattern with a Turtle object.
- Are students struggling with the “Space the quote” puzzle? Give a hint: Help students get started by providing a line or two of code. For example,
newQuote = ""
for letter in range(len(quote)):
- Remind students to click the “submit project” button when they are done with their DIY projects—that way, you can check out their fabulous creations!
- In this module, students will read a short document that explains how to apply “for” loops to situations involving calendars.
- Optional: Encourage students to experiment with the code in the provided examples. How did their changes affect the output?
- Are students struggling with the “Counting days” puzzle? Give a hint: Tell students to use the code from the example as a reference.
- In this module, students will use a nested “for” loop to print various shapes!
- Check that students are exploring the “Try This” activity, which encourages students to change the size of the rectangle.
- Are students struggling with the puzzles? Give a hint: Tell them to use a nested “for” loop and to use code from the example as a reference.
- This quiz requires students to apply concepts from this lesson to solve 3 different puzzles. There are no multiple choice questions.
Discussion Questions/Follow-Up Activities (20 minutes)Lead a discussion with your students:
- Ask students to briefly talk with a neighbor and discuss one thing they learned in today’s lesson.
- What did they enjoy most about today’s lesson?
- What did they or someone they know struggle with?
- Did they enjoy revisiting the turtle tool in some of today’s lessons? Why or why not? What are some of the polygons they created?
- CCSS-ELA: SL.8.1, RI.9-10.3, RI.9-10.6, RI.11-12.3, RI.11-12.6, L.9-10.3, L.9-10.6, L.11-12.3, L.11-12.6
- CCSS-Math: HSN.Q.A.1, HSN.Q.A.2, HSN.Q.A.3, MP.1, MP.2, MP.4
- CSTA: 2-AP-11, 2-AP-12, 2-AP-13, 2-AP-15, 2-AP-17, 3A-AP-14, 3A-AP-17, 3B-AP-11, 3B-AP-12
- CS CA: 6-8.AP.11, 6-8.AP.12, 6-8.AP.13, 6-8.AP.15, 6-8.AP.17, 9-12.AP.12, 9-12.AP.14, 9-12.AP.16
- ISTE: 1.c, 1.d, 4.d, 5.c, 5.d
For LoopsPython 201
- 9 Activities
- 1 Completion Badge