Docsity

Topic 2: Python Loops (Iteration) — Thinking Like a Programmer, Lecture notes of Computer Science

Toggle sidebar
bg1
Topic 2: Loops — Thinking Like a Programmer (Python)
1. What Problem Do Loops Solve?
Don't start with syntax. Start with the problem.
What if a task needs to happen 10 times?
What if it needs to happen 10,000 times?
How can we avoid repeating the same code?
How do we decide when repetition should stop?
Key idea:
A loop allows a program to repeat instructions without writing the same instructions again.
2. Recognising Repetition in a Problem
Before writing code, ask: “What part of this problem is repeating?”
Example 1:
Display the numbers from 1 to 10.
Think:
1. Start at 1.
2. Display the number.
3. Move to the next number.
4. Stop after 10.
Then convert that thinking into Python.
3. for Loops — When You Know What to Iterate Over
Cover/ Example 2:
for number in range(1, 11):
print(number)
Then explain how to think about it, rather than just what each word means.
What is changing?
Where does it start?
Where does it stop?
pf3
pf4
pf5
pf8

Partial preview of the text

Download Topic 2: Python Loops (Iteration) — Thinking Like a Programmer and more Lecture notes Computer Science in PDF only on Docsity!

Topic 2: Loops — Thinking Like a Programmer (Python)

1. What Problem Do Loops Solve?

Don't start with syntax. Start with the problem.  What if a task needs to happen 10 times?  What if it needs to happen 10,000 times?  How can we avoid repeating the same code?  How do we decide when repetition should stop? Key idea: A loop allows a program to repeat instructions without writing the same instructions again.

2. Recognising Repetition in a Problem

Before writing code, ask: “What part of this problem is repeating?” Example 1: Display the numbers from 1 to 10. Think:

  1. Start at 1.
  2. Display the number.
  3. Move to the next number.
  4. Stop after 10. Then convert that thinking into Python.

3. for Loops — When You Know What to Iterate Over

Cover/ Example 2: for number in range( 1 , 11 ): print(number) Then explain how to think about it, rather than just what each word means.  What is changing?  Where does it start?  Where does it stop?

 How many times does the body execute?

4. Understanding range()

Break this into/ Example 3: range(stop) range(start, stop) range(start, stop, step) Include off-by-one errors, because these are important programming-thinking mistakes. Example 4: range( 1 , 10 ) does not include 10. Ask: “If I need 1–10, what should my stopping value be?”

5. while Loops — When the Ending Point Depends on a Condition

Example 5: number = 1 while number <= 10 : print(number) number += 1 Focus on the programmer's reasoning: Initial state → condition → action → state change → condition again This is a much better way to understand while loops than memorising syntax.

6. Choosing the Correct Loop

Give yourself decision questions:

for number in range( 1 , 21 ): if number % 2 == 0 : print(number) Then ask:  What is being repeated?  What is being tested?  What happens when the condition is true?  What happens when it is false?

9. Counters and Accumulators

These are important programming patterns.  Counter: count = 0 for number in numbers: if number > 10 : count += 1  Accumulator: total = 0 for number in numbers: total += number Then explain the underlying pattern: Initialise → repeat → update → use result

10. Tracing a Loop

This is a very useful “thinking like a programmer” section. Given/ Example 7: total = 0

for number in range( 1 , 4 ): total += number Trace it manually: Step number total Start — 0 1 1 1 2 2 3 3 3 6 Then ask: What will the program output? This helps you understand code rather than simply memorising it.

11. Infinite Loops

Show the mistake/Example 8: number = 1 while number <= 10 : print(number) Then ask: What changes between iterations? Nothing. Therefore, the condition remains true. Fix/Example 9: number = 1 while number <= 10 : print(number) number += 1

12. Nested Loops

Introduce the idea:

14. Common Mistakes

Problem Typical cause How to think about it Infinite loop Condition never becomes false What changes each iteration? Wrong number of repetitions Incorrect range() What values are produced? Off-by-one Incorrect start/stop Is the stop value included? Unexpected output Incorrect indentation Which statements belong inside the loop? Loop stops too early Incorrect break Under what condition should it stop?

15. Mini Programming Challenges

Instead of just giving examples, finish with problems where you must design the solution. Level 1  Print numbers 1–20.  Print only even numbers.  Calculate the sum from 1–100. Level 2  Count how many numbers in a list are greater than 50.  Find the largest number without using max().  Keep asking for a number until the user enters a positive number. Level 3  Create a number guessing game.  Create a simple menu that keeps running until the user chooses "Exit".  Process a list of marks and calculate how many students passed. For each challenge, use: Problem → Plan → Pseudocode → Python → Test → Debug

That is much closer to “thinking like a programmer” than simply listing Python commands.