Starter quiz
- The process of repeating a sequence of instructions within a program loop is the definition of which term?
- iteration ✓
- sequence
- selection
- a while loop
-
- ______ are organised collections of data.
- 'Data structures' ✓
- What is the term for a variable that is used to signal a change in a program's state?
- 'flag' ✓
- A while loop will continue to iterate until ...
- a counter reaches a certain value.
- a condition is met. ✓
- the program crashes.
-
- What would be displayed if this code was run? ```count = 1 while True: print(count) count = count + 1```
- 1 2 3 4 5
- The code will not run.
- The code will print 1, 2, 3 ... and so on until the program is stopped manually. ✓
-
- What item is held in rolls[3] of this list? ```rolls = [1, 4, 3, 6]```
- 1
- 4
- 3
- 6 ✓
-
Exit quiz
- Which type of loop is used in Python for count-controlled iteration?
- 'for' ✓
- What will the following code display? ```for count in range (1,8): print(count)```
- 01234567
- 1234567 ✓
- 12345678
- 012345678
-
- A ______ loop is used for condition-controlled iteration.
- 'while' ✓
- What does the `len()` function do?
- prints a list or string
- sorts a list or string
- iterates through a list or string
- returns the length of a list or string ✓
-
- What would be the output of this code when it is run? ```rolls = [1, 4, 3, 6] count = 0 for dice in rolls: count = count + 1 print(count)```
- 0
- 1
- 4 ✓
- 3
-
- What would be the output of this code when it is run? ```rolls = [1, 4, 3, 6] count = 0 for dice in rolls: if dice >= 3: count = count + 1 print(count)```
- 1
- 3 ✓
- 2
- 0
-
Worksheet
Loading worksheet ...
Presentation
Loading presentation ...
Video
Lesson Details
Key learning points
- Count-controlled iteration is implemented using for loops in Python.
- The range() function can be used with a for loop to specify the number of times that a for loop should iterate.
- For loops can be used to iterate through each item held in a list to inspect each item in turn.
Common misconception
A for loop is the same as a while loop as they are both designed to repeat instructions. You should pick one and stick to it for all your programs.
Unlike a while loop, a for loop is count-controlled, meaning it will repeat a set number of times when the loop begins. In contrast, a while loop repeats instructions for as long as its condition is true. This can vary each time the loop runs.
Keywords
Iteration - the process of repeating a sequence of instructions within a program loop
For loop - a count-controlled loop used to repeat a specific block of code a known number of times
+