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)
nameslen(names)len(names) < 5
[]

Output

 

Step 1: Start: an empty list. Nothing has been checked yet, so the last column is blank.

1 / 12

Press Next once for each line that runs. Watch the last column flip.

Step 1 of 12: Start: an empty list. Nothing has been checked yet, so the last column is blank..

Watch out: Only the last column decides whether there is another pass.

Computer Science · Python loops

What does this loop actually do?

A program sets done = False, then reaches the line while done == False: with some indented lines under it. Somewhere inside those lines, the variable done gets changed.

Which idea is closest to what you think the loop does?
How sure are you?

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

How long is that string?

What does len("Oak National Academy") give? Commit to a number.

Your estimate

20 characters

0 characters40 characters

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.

9 of 9 still to sort.

Each of these is a question Python answers with True or False. Which answer does it give?

Computer Science · Python strings

Which line breaks the program?

A learner wants word to hold "bat" instead of "cat". One line stops the program with an error. Which one?

A learner's program — which line goes wrong?

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

1A while loop repeats its indented lines while the condition is True and stops once it is no longer True.
2The condition is re-checked at the start of every pass, so changing the data (like appending to a list) can change the answer.
3len counts characters in a string, spaces included, and items in a list.
4in and not in give True or False, and indexing starts at 0.
5Lists are mutable. Strings are immutable, so update one by reassigning the whole variable.

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?
Repeating lines of code.
When does a while loop stop?
On the first check where its condition comes out as False.
Does a while loop run a set number of times?
No. It re-checks its condition each pass, so the number of passes depends on when the condition stops being True.
What does while len(names) < 5: mean in plain words?
Keep going while the list holds fewer than five items.
What does names.append(name) do?
It adds name as a new item to the list names, so the list gets longer.
In the trip program, why is cities.remove(city) there?
It takes the picked city out of cities, so the same city cannot be picked again.
What does len count?
The characters in a string (spaces too) or the items in a list.
When is text in a string True?
When its characters sit next to each other in the string, in the same order.
When is value in a list True?
When value equals any one item in the list.
What does not in do?
It gives the opposite of in: True when in would be False.
Which index is the first character of a string?
0. Python counts from zero.
What does it mean that a string is immutable?
You cannot change it in place. Assigning to word[0] gives an error.
How do you change what a string variable holds?
Reassign the whole value to the variable, which overwrites it.
Why can a list be updated without making a new list?
A list is mutable, so its values can be changed in place.
What do == and != test?
== is True when two values are equal. != is True when they are different.

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 posted

More KS3 Computer Science topics

See the full KS3 Computer Science curriculum →

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.

Helpful guides for parents and students