Computational thinking

Decompose a problem, spot the pattern, write the steps.

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:

  1. Start at the first name.
  2. Compare it to the target.
  3. If it matches, stop with yes.
  4. If names remain, go to the next.
  5. 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.