KS3 · Computer Science
Iterating over sequences in Python
A while loop is like a bouncer asking "is there still room?" before every person. Catch the moment the answer flips.
Computer Science · Python loops
Watch the loop keep asking
Step through. The list changes on every pass, and so does the answer in the last column.
Before you press Next: when this loop stops, what will len(names) be, and what will the condition say?
1 names = []2 while len(names) < 5:3 name = input("Enter a name: ")4 names.append(name)
| names | len(names) | len(names) < 5 |
|---|---|---|
| [] |
Output
Step 1: Start: an empty list. Nothing has been checked yet, so the last column is blank.
Press Next once for each line that runs. Watch the last column flip.
Predict, then check
A trip planner keeps picking cities. Its loop is: while "London" not in trip, pick a city from cities, append it to trip, then run cities.remove(city).
Imagine the line cities.remove(city) is deleted from the loop. What could go wrong?
Computer Science · Python strings and lists
True or False?
Pick an expression, then pick the answer Python gives.
Still to sort
True (0)
Python answers True.
False (0)
Python answers False.
Each of these is a question Python answers with True or False. Which answer does it give?
What you need to know
- Iteration just means repeating lines of code, and a while loop does it by re-checking its condition every time round.
- The indented lines run again while the condition is True, and the loop stops once it is no longer True.
- Appending inside a loop makes a list grow, so a condition like len(names) < 5 flips from True to False as it fills.
- len counts the characters in a string, spaces included, and the items in a list.
- On a string, in is True when the characters you ask about sit next to each other in it, in the same order you wrote them.
Have a goYour friend swears "hn" in "Python" is True, "because both letters are in the word". Is she right?
No. It is False.
h and n are both in Python, but in on a string needs the characters next to each other, in the same order, and an o sits between them.
- On a list, in is True when the value equals any item, and not in gives the opposite result.
- Counting starts at 0 in Python, so "Python"[1] is the second character, a lowercase y.
Have a goWork it out before you peek: what character does "Python"[3] give?
h
Counting starts at 0: [0] is P, [1] is y, [2] is t and [3] is h. Counting from 1 would land you one letter early.
- Lists are mutable: append and remove update the same list, with no new list needed.
Have a gonames holds ["Ada", "Ben", "Cleo"]. What is left after names.remove("Ben")?
["Ada", "Cleo"]
remove takes that item out of the same list. A list is mutable, so it can be updated without making a new one.
- Strings are immutable, so you can't change one character by its index. Reassign the whole variable instead.
- == tests whether two values are equal and != tests whether they are different; each gives True or False.
- A string is a data type: a collection of characters, which can be letters, spaces, punctuation, digits and other symbols. In Python you write it inside speech marks, like "Python". Careful: digits inside speech marks are text characters, not numbers.
The big picture
Python is a textual programming language, and a while loop is one way it repeats code. The indented lines run again while the condition is True, and the loop stops once it is no longer True. You can use len, in and not in, indexing, append and remove on lists and strings, but a string is immutable, so you change it by reassigning the whole variable.
Key points
Worked example
Problem
This time names already holds ["Ada", "Ben", "Cleo"]. The loop is the same: while len(names) < 5:, with names.append(name) inside, and there are plenty of names to type in. How many names get appended, and what is len(names) when the loop stops?
⚠ Watch out
Reading while as "repeat until the condition is met", or thinking a while loop runs a set number of times. The condition is the keep-going test, and the number of passes depends on when it stops being True.
Memory hook
Ask it before every pass: still True? True means go round again. False means stop.
Check yourself
A list trip holds ["Paris", "Rome"]. Is "London" not in trip True or False, and so does while "London" not in trip: go round again? Decide before you move on.
Flashcards
(15)What is iteration?
When does a while loop stop?
Does a while loop run a set number of times?
What does while len(names) < 5: mean in plain words?
What does names.append(name) do?
In the trip program, why is cities.remove(city) there?
What does len count?
When is text in a string True?
When is value in a list True?
What does not in do?
Which index is the first character of a string?
What does it mean that a string is immutable?
How do you change what a string variable holds?
Why can a list be updated without making a new list?
What do == and != test?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore KS3 Computer Science topics
How this lesson was checked. This KS3 Computer Sciencelesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 7 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.