◇ OpenCyber Academy
Bachelor's · Chapter 3 · English

Chapter 3 — Programming I

Open, free learning for anyone who wants to protect people and technology. No account is required to read this text or its worked answers.

Chapter 3 — Programming I

Lesson map

Lesson 03 published. Map, example, EX007.

flowchart TD
  N0["Learning outcomes"]
  N1["3.1 A first check"]
  N2["3.2 Lists and dictionaries"]
  N3["3.3 Clear output"]
  N4["3.4 Testing"]
  N5["3.5 Standard library"]
  N6["Building on the first check"]
  N7["Questions to carry forward"]
  N0 --> N1 --> N2 --> N3 --> N4 --> N5 --> N6 --> N7
  N7 --> EX["Worked example"]
  EX --> Q["Exercise EX007"]

Logical drawing

flowchart LR
  N0["Learning outcomes"]
  N1["3.1 A first check"]
  N2["3.2 Lists and dictionaries"]
  N3["3.3 Clear output"]
  N4["3.4 Testing"]
  N5["3.5 Standard library"]
  N6["Building on the first check"]
  N7["Questions to carry forward"]
  N0 --> N1 --> N2 --> N3 --> N4 --> N5 --> N6 --> N7 --> Q["EX007"]
[Learning outcomes]
    |
    v
[3.1 A first check]
    |
    v
[3.2 Lists and dictionaries]
    |
    v
[3.3 Clear output]
    |
    v
[3.4 Testing]
    |
    v
[3.5 Standard library]
    |
    v
[Building on the first check]
    |
    v
[Questions to carry forward]
    |
    v
[Worked example] --> [Exercise EX007]

Learning outcomes

You will (1) write small Python programs over lists and dictionaries; (2) print clear results; (3) test with known answers.

3.1 A first check

Aisha's first program reads a list of synthetic laptop records and prints those without encryption. It has three parts: data, a loop with a condition and output. Even this small check answers the question Gareth used to answer by hand.

Aisha's first version runs on a list of five synthetic laptop records typed straight into the script. The loop checks the encrypted field of each record and prints the asset tag when encryption is off. Gareth compares the printed list with his own notes, and the two agree, which gives him enough confidence to try the script on a larger export.

3.2 Lists and dictionaries

Each laptop is a dictionary with fields such as name, encrypted and last_update. The fleet is a list of dictionaries. This structure appears in most labs in the course.

A single synthetic record looks like {"name": "BHA-L03", "encrypted": True, "last_update": "2026-09-08"}, and the fleet is a list of 45 such records. Using the same field names in every lab means a function written for one check can be reused in another. Aisha keeps a short note of each field's meaning and type beside the code, because the type error in the worked example came from exactly this kind of field.

3.3 Clear output

Good output starts with a count, then lists details. "13 of 45 laptops not encrypted" is more useful than a long list with no summary.

Aisha's script prints the summary line first, for example "13 of 45 laptops not encrypted", and only then lists the 13 asset tags in order. Gareth can stop reading after the first line if he only needs the number for a manager. Sorting the asset tags makes it easy to compare two runs and spot which laptops have changed state between them.

3.4 Testing

Before trusting the program, Aisha runs it on three synthetic laptops where she knows the answer: two encrypted, one not. The program must print exactly one.

The three-laptop test is small enough that Aisha can check the expected answer by eye. She adds two further cases: an empty list, which must print "0 of 0", and a laptop whose encrypted field is the text "False", which exposed the bug in the worked example. Each test runs in under a second, so she runs all of them after every change to the script.

3.5 Standard library

Labs use only Python's standard library, so they run on any computer and add no unreviewed code to Bramley's systems.

For Bramley this rule has a practical side as well as a security one: Gareth cannot install extra packages on locked-down machines without a change request. The standard library already includes csv, json, sqlite3, statistics and collections, which together cover every lab in this course. Aisha checks each script's import lines before handing it over, so nothing outside the standard library slips in.

Building on the first check

Aisha extends her program to report two fields: encryption and days since last update. She keeps each check in its own function so that a mistake in one does not affect the other. Later chapters reuse these functions in the inventory, baseline and verification labs, so writing them carefully now saves time later.

Questions to carry forward

Aisha ends the chapter with three questions she will return to: which devices exist, how are they configured, and how quickly do they receive fixes? Each question links to a later lab, and each answer will be recorded with its date and source so that progress can be measured honestly rather than remembered.

Worked example

Aisha's first version prints nothing. She finds that the encrypted field is stored as the text "False", which Python treats as true in a condition. She converts the text to a boolean and adds a test with text input.

Synthetic case. Synthetic.

Errors and limits

Data types cause silent errors; test with realistic data.

Exercises

Synthesis

Small tested programs replace manual checking.

Continue studying — chapter 03 of 40

← Chapter 02 (draft) · Course chapter index · Chapter 04 (draft) →