Computer science starts before code. Decompose a problem into pieces, spot a pattern, then write steps a machine can repeat.
Goal
Turn “who is in this list?” into a loop with a printed answer.
Decompose
A kiosk wants to know whether "Kisumu" is on today’s city list.
Pieces:
- Start at the first name.
- Compare it to the target.
- If it matches, stop with yes.
- If names remain, go to the next.
- If none remain, answer no.
cities = ["Nairobi", "Mombasa", "Kisumu", "Nakuru"]
target = "Kisumu"
found = False
for city in cities:
if city == target:
found = True
break
print("found", found)The pattern
The same steps work for any list and any target. That is an algorithm — next chapter names it. Here, notice the pattern: walk, compare, stop.
def contains(items, target):
for item in items:
if item == target:
return True
return False
print(contains(["A", "B", "C"], "B"))
print(contains(["A", "B", "C"], "D"))What you ignore
You do not care how Python stores the list in RAM yet. You care about the steps. Representation and complexity come later.
Tip
If you cannot say the steps in plain language, do not start coding. Write the five-line recipe first.