Skip to content
Computing · Secondary 3 · Programming with Python · Semester 1

Iteration: For Loops

Students will use 'for' loops to iterate over sequences (like strings and lists) and perform repetitive tasks a known number of times.

MOE Syllabus OutcomesMOE: Programming - S3

About This Topic

In Secondary 3 Computing under the MOE curriculum, students master 'for' loops to iterate over sequences like strings and lists, handling repetitive tasks a known number of times. They construct loops using range() for counting or directly unpacking items, process each element with operations such as summing values or modifying characters, and compare this to manual repetition for efficiency. This directly supports key questions on loop purpose, construction, and advantages.

The topic strengthens computational thinking by linking iteration to data processing in Python programs, preparing students for complex algorithms in later units. It emphasizes indentation for block structure, variable scope within loops, and accumulation patterns like building new lists, all aligned with MOE programming standards.

Active learning benefits this topic greatly since students run code instantly in Python environments to observe iterations step-by-step. Collaborative debugging in pairs reveals syntax errors and logic flaws quickly, while sharing varied solutions builds peer evaluation skills and deepens understanding of loop behavior.

Key Questions

  1. Explain the purpose of a 'for' loop in automating repetitive tasks.
  2. Construct a 'for' loop to process each item in a given list.
  3. Compare the efficiency of using a 'for' loop versus manual repetition.

Learning Objectives

  • Explain the purpose of a 'for' loop in automating repetitive tasks.
  • Construct a 'for' loop to process each item in a given list or string.
  • Compare the efficiency of using a 'for' loop versus manual repetition for a given task.
  • Analyze the output of a 'for' loop given specific input data.
  • Create a Python program that utilizes a 'for' loop to solve a simple problem.

Before You Start

Introduction to Python Variables and Data Types

Why: Students need to understand how to store and manipulate data using variables and recognize basic data types like strings and integers.

Basic Python Operators

Why: Understanding operators (e.g., arithmetic, comparison) is necessary for performing operations within the loop body.

Lists in Python

Why: Familiarity with lists is crucial as they are a primary data structure students will iterate over using 'for' loops.

Key Vocabulary

for loopA control flow statement that allows code to be executed repeatedly. It iterates over a sequence (like a list or string) or a specified range of numbers.
iterationThe process of repeating a set of instructions or steps, typically within a loop, until a specific condition is met or a sequence is fully processed.
sequenceAn ordered collection of items, such as a string (sequence of characters) or a list (sequence of elements).
iterableAn object that can be looped over, meaning it can return its members one at a time. Lists, strings, and range objects are common iterables in Python.
range()A built-in Python function that generates a sequence of numbers, often used to control the number of times a 'for' loop executes.

Watch Out for These Misconceptions

Common MisconceptionFor loops run infinitely like while loops without conditions.

What to Teach Instead

For loops iterate a fixed number of times based on sequence length or range, ending automatically. Pair debugging activities help students trace executions and see finite outputs, correcting overgeneralization from prior loop types.

Common MisconceptionLoop variable retains final value after loop ends, usable anywhere.

What to Teach Instead

The variable holds the last item's value post-loop, but scope limits reuse. Group code reviews expose errors from assuming persistence, as students test and discuss variable states collaboratively.

Common MisconceptionFor loops only work with numbers, not strings or lists.

What to Teach Instead

They iterate any sequence, unpacking items directly. Hands-on challenges processing mixed data types let students experiment and confirm versatility through immediate feedback.

Active Learning Ideas

See all activities

Real-World Connections

  • Software developers use 'for' loops extensively when processing batches of data, such as iterating through customer records to send personalized emails or analyzing sensor readings from IoT devices.
  • Data analysts in marketing firms employ 'for' loops to process large datasets of consumer behavior, identifying patterns or calculating metrics for thousands of individual transactions efficiently.
  • Game developers utilize 'for' loops to update the state of multiple game objects simultaneously, like moving all enemy characters across the screen or checking for collisions with every player projectile.

Assessment Ideas

Quick Check

Present students with a short Python code snippet containing a 'for' loop and a list. Ask them to write down the exact output the code will produce. For example: `my_list = ['apple', 'banana', 'cherry']; for fruit in my_list: print(fruit.upper())`

Exit Ticket

Provide students with a scenario, such as 'Calculate the sum of all even numbers from 1 to 20'. Ask them to write a 'for' loop in Python that solves this problem and briefly explain why a 'for' loop is suitable here.

Discussion Prompt

Pose the question: 'Imagine you need to print the first 100 positive integers. How would you do this without a 'for' loop? Now, how would you do it with a 'for' loop? Discuss the advantages of the 'for' loop approach in terms of readability and effort.'

Frequently Asked Questions

How do you teach for loops versus manual repetition in Python?
Start with a simple task like printing list items manually, then show bloated code. Introduce for loop syntax, have students rewrite and compare line counts and run times. This visual contrast highlights automation, reinforced by class timing exercises for engagement.
What are common errors with for loops in Secondary 3?
Frequent issues include wrong indentation causing syntax errors, off-by-one in range(), or modifying lists during iteration. Address via live coding demos and peer reviews, where students spot and fix errors in real time, building debugging confidence.
When should students use for loops over while loops?
Use for loops for known iteration counts over sequences, like lists or range(). While suits unknown repetitions with conditions. Class debates on scenarios, followed by coding both for a task, clarify choices through comparison of code simplicity and reliability.
How can active learning help students master for loops?
Active approaches like pair programming and live editing in IDEs let students iterate, test, and debug instantly, seeing loop traces. Group challenges processing real data, such as class rosters, connect abstract syntax to outcomes. Sharing solutions fosters discussion of efficiencies, turning passive syntax memorization into skill mastery.