Skip to content
Computing · Year 8 · Python: From Blocks to Text · Autumn Term

Iteration: For Loops

Students use 'for' loops to repeat blocks of code a specific number of times or iterate through sequences.

National Curriculum Attainment TargetsKS3: Computing - Programming and DevelopmentKS3: Computing - Control Structures

About This Topic

Iteration with for loops teaches students to repeat code blocks a set number of times or process sequences in Python. They write 'for item in list:' structures to handle data like names, printing custom messages for each classmate. Using range() lets them repeat actions precisely, such as moving a turtle to form shapes. This topic transitions from block-based coding to text, making repetition efficient and readable.

Within KS3 Computing, for loops support programming aims and control structures. Students design loops for lists, compare them to manual repetition for efficiency gains, and predict nested loop outputs, like grid patterns from double iteration. These practices build algorithmic thinking, debugging skills, and data handling, essential for software development.

Active learning suits for loops perfectly. When students pair program list processors or trace nested loops on worksheets before coding, syntax feels immediate and errors teach resilience. Collaborative prediction challenges followed by live runs connect theory to results, while group debugging reinforces loop bounds. This approach makes abstract control tangible, boosts confidence, and deepens retention through hands-on iteration.

Key Questions

  1. Design a 'for' loop to process each item in a list of names.
  2. Analyze the efficiency benefits of using a 'for' loop over repeating code manually.
  3. Predict the output of a program containing a nested 'for' loop.

Learning Objectives

  • Design a Python 'for' loop to iterate through a list of strings and print a personalized message for each item.
  • Analyze the efficiency of using a 'for' loop compared to manually repeating code for a given task.
  • Predict the exact output of a program containing nested 'for' loops, including loop bounds and variable changes.
  • Compare the execution flow of a 'for' loop iterating over a list versus a 'for' loop using range().

Before You Start

Introduction to Python Variables and Data Types

Why: Students need to understand how to store and refer to data, such as names or numbers, before they can iterate over them.

Basic Python Syntax and Code Execution

Why: Familiarity with writing and running simple Python code is necessary to understand how loops affect program flow.

Lists in Python

Why: Students must know what a list is and how to create one to effectively use 'for' loops for iterating through sequences.

Key Vocabulary

for loopA control flow statement that allows code to be executed repeatedly. It is often used to iterate over a sequence (like a list, tuple, string) or a range of numbers.
iterationThe repetition of a process or utterance. In programming, it refers to executing a block of code multiple times, typically within a loop.
sequenceAn ordered collection of items, such as a list, string, or tuple, that can be iterated over by a 'for' loop.
range()A built-in Python function that generates a sequence of numbers, commonly used in 'for' loops to repeat actions a specific number of times.
nested loopA loop placed inside another loop. The inner loop completes all its iterations for each single iteration of the outer loop.

Watch Out for These Misconceptions

Common MisconceptionFor loops run forever like while loops.

What to Teach Instead

For loops iterate exactly over the sequence length or range values, then exit. Pair tracing activities on paper reveal the iteration count and natural end point, helping students visualise finite repetition before coding.

Common Misconceptionrange(5) iterates from 0 to 5 inclusive.

What to Teach Instead

range(5) produces 0,1,2,3,4 only. Prediction worksheets where students simulate loops by hand, followed by console tests, correct off-by-one errors through active verification and peer comparison.

Common MisconceptionNested for loops run one after the other sequentially.

What to Teach Instead

The inner loop completes fully for each outer iteration, multiplying runs. Drawing nested boxes or step-through group simulations clarify the structure, making output predictions accurate via collaborative modelling.

Active Learning Ideas

See all activities

Real-World Connections

  • Web developers use 'for' loops to process collections of user data, such as displaying a list of products on an e-commerce website or iterating through comments on a social media feed.
  • Game developers utilize 'for' loops to manage game elements, like updating the positions of multiple enemies on screen in a game or checking for collisions between many objects in a simulation.
  • Data analysts employ 'for' loops to process large datasets, such as calculating averages for each category in a spreadsheet or applying a specific transformation to every row of a data table.

Assessment Ideas

Exit Ticket

Provide students with a Python list of fruits, e.g., `fruits = ['apple', 'banana', 'cherry']`. Ask them to write a 'for' loop that prints 'I like [fruit name]' for each item in the list. Collect and check for correct syntax and iteration.

Quick Check

Display a simple nested 'for' loop on the board, like: `for i in range(2): for j in range(3): print(i, j)`. Ask students to write down the predicted output. Review answers as a class, discussing how the inner loop completes for each outer loop iteration.

Discussion Prompt

Pose this scenario: 'Imagine you have 100 student names and need to email each one a personalized welcome message. Would you write 100 separate print statements, or use a 'for' loop? Explain why your choice is more efficient.'

Frequently Asked Questions

How do I teach for loops to Year 8 Python beginners?
Start with familiar lists like class names, showing manual repetition first to highlight inefficiency. Model simple 'for name in names:' code live, then let students adapt it. Use turtle graphics for visual range() repeats. Build gradually to nested loops with prediction tasks for confidence.
What are common errors with nested for loops?
Students often miscount iterations or confuse loop variables, leading to wrong patterns. Indentation errors break nesting. Address with trace tables and visual aids like grids. Group challenges printing shapes help isolate issues, while live debugging builds fix-it skills quickly.
How can active learning help students master for loops?
Active methods like pair programming list iterators or whole-class output predictions make loops experiential. Students trace code by hand, run tests, and debug peers' work, turning syntax errors into learning moments. Collaborative relays reveal patterns faster than lectures, fostering logical prediction and retention through immediate feedback.
Why use for loops over copying code manually?
For loops handle varying data sizes without edits, avoid repetition mistakes, and scale efficiently. Copy-paste bloats code and hides bugs. Show side-by-side: manual greetings for 5 names versus a loop for 30. Efficiency demos and refactor challenges convince students of cleaner, maintainable code.