DCubes
Python module

Lesson 4

Types and collections

Strings, numbers, booleans, and list/dict/tuple/set — the shapes data rows take in Python.

beginner30 minRun on your machine

What you will be able to do

  • Use str, int, float, and bool in simple expressions
  • Build and index lists and dicts like a row of data
  • Know when tuple and set help

Why this matters for data work

A CSV row becomes a list or a dict. JSON objects are nested dicts and lists. Before pandas, you must be fluent in these shapes — they are what APIs and database drivers hand you.

Concepts

Type Example Data use
str "SKU-100" IDs, names, categories
int 3 counts, quantities
float 19.99 rough money/measures (Decimal later in serious finance)
bool True flags
list ["a", "b"] ordered rows, sequences
dict {"sku": "X"} named fields
tuple (1, "a") fixed pairs; dict keys
set {"a", "b"} unique membership

Indexing — items[0], row["sku"]. Slicing — items[0:2].

None — missing value in Python before you model NULL in SQL.

Practice on your machine

cd ~/dcubes/python-lab
source .venv/bin/activate
cat > types_lab.py << 'EOF'
order = {
    "order_id": "ORD-1",
    "quantity": 2,
    "unit_price": 12.5,
    "tags": ["retail", "priority"],
}

line_total = order["quantity"] * order["unit_price"]
print(order["order_id"], line_total)
print(order["tags"][0])
print("retail" in order["tags"])

stores = {"STR-01", "STR-02", "STR-01"}
print(stores)

pair = ("STR-01", 120)
print(pair[0], pair[1])
EOF
python types_lab.py
ORD-1 25.0
retail
True
{'STR-01', 'STR-02'}
STR-01 120

(Set print order may vary.)

What you should see

  • Dict field access and a computed line total.
  • List membership with in.
  • Set dedupes "STR-01".
  • Tuple indexes like a lightweight row.

Common mistakes

  • Mutable default traps (later, in functions) — do not use def f(x=[]).
  • Float for money in production ledgers — fine for labs; warehouses often use numeric types.
  • Assuming JSON keys are always present — use .get("key") when data is messy.

Next

Control flow and functions