Python collections and flow

This note has an emphasis on how lists, dictionaries, loops, booleans, and conditionals combine in practical code.

1. Lists – storing ordered collections

A list is an ordered sequence of items.

Syntax

friends = ["Tommy", "Isabel", "Daniel"]

Key points

friends[0]   # "Tommy"
friends[1]   # "Isabel"
friends[2]   # "Daniel"

Common operations

friends.append("Otto")   # add item
friends.remove("Tommy")  # remove item
len(friends)             # number of items

2. Dictionaries – storing labelled data

A dictionary stores data as key–value pairs.

Think: label β†’ value

Syntax

person = {
    "age": 28,
    "favourite_colour": "red"
}

Key points

person["age"]   # 28

Add or update

person["city"] = "London"

3. Booleans – true or false values

A boolean is either:

True
False

Used for:

Examples

5 > 3        # True
10 == 7      # False

Store in variables

is_raining = True

4. Comparisons – creating booleans

Comparison operators:

Example:

age = 28
age > 18     # True
age == 18    # False

5. Conditionals – making decisions

An if statement runs code only when a condition is true.

Syntax

if condition:
    do_something()

Example

time_to_complete = 3

if time_to_complete <= 5:
    print("Do it now")

With an alternative

if time_to_complete <= 5:
    print("Do it now")
else:
    print("Do it later")

Key rules:

6. Loops – repeating actions

A for loop repeats code for each item in a list.

Syntax

for item in list_name:
    do_something(item)

Example

tasks = ["Email boss", "Write report", "Buy milk"]

for task in tasks:
    print(task)

7. Combining everything – the standard pattern

A common structure is a list of dictionaries, processed by a loop, filtered by an if condition that evaluates to a boolean, with results stored in another list.

Step 1 – store tasks as a list of dictionaries

tasks = [
    {"name": "Email boss", "time": 3},
    {"name": "Write report", "time": 60},
    {"name": "Buy milk", "time": 5}
]

Step 2 – create an empty list for later tasks

tasks_for_later = []

Step 3 – loop through tasks

for task in tasks:
    print(task["name"])

Step 4 – conditional inside the loop

for task in tasks:
    if task["time"] <= 5:
        print("Do now:", task["name"])
    else:
        tasks_for_later.append(task)

Step 5 – inspect the result

print(tasks_for_later)

Mental model

This nesting is the main conceptual shift:

One-page syntax reference

List

items = ["a", "b", "c"]
items[0]
items.append("d")
len(items)

Dictionary

person = {"age": 28}
person["age"]
person["city"] = "London"

Boolean and comparisons

True
False
5 > 3
10 <= 10
"vegan" == "vegan"
"vegetarian" != "vegan"

Conditional

if condition:
    do_this()
else:
    do_that()

Loop

for item in items:
    print(item)

Combined pattern

later = []

for item in items:
    if condition:
        action
    else:
        later.append(item)

Common pitfalls

Bracket types

Indentation matters

Wrong:

if x > 5:
print("Hi")

Correct:

if x > 5:
    print("Hi")

Assignment vs comparison

x = 5      # assign
x == 5     # compare