Tooling reference

Python cheatsheet

A compact, keep-open syntax reference for the subset the early builds use. Lookup, not explanation.

Who this is for. This page assumes you can already program in some language and shows you how Python spells it, for the subset the early Build Track milestones use (up to about B5). It does not teach programming from scratch and is not a general Python course. It is an optional reference. If you have never programmed, use a dedicated beginner Python resource first, then come back.
Last reviewed: 2026. The examples here are short and illustrative; exact output depends on your Python version and setup. For anything beyond this subset, the official Python tutorial is the durable source.

Variables and common literals

x = 5            # int
r = 0.1          # float
s = "text"       # str
flag = True      # bool
nothing = None

Lists

xs = [1, 2, 3]
xs.append(4)
xs[0]; xs[-1]; len(xs)

Tuples

cell = (4, 0)
row, col = cell

Dictionaries

d = {"a": 1}
d["b"] = 2
d["a"]; "a" in d; d.items()

Sets

blocked = {(1, 1), (2, 2)}
(1, 1) in blocked

Indexing and slicing

xs[0]; xs[-1]
xs[1:3]; xs[:2]; xs[2:]

Conditionals

if x > 0:
    ...
elif x < 0:
    ...
else:
    ...

Loops

for v in xs: ...
for i in range(5): ...
for i, v in enumerate(xs): ...
while not done: ...   # break / continue

Functions and multiple returns

def f(a, b=1):
    return x, y, z

x, y, z = f(2)

Imports

import numpy as np
import random
from math import sqrt

f-strings

f"episode {ep}: {total:.2f}"

List comprehensions

[v * v for v in range(5)]
[v for v in xs if v % 2 == 0]

Reading a text file

with open("corpus.txt", encoding="utf-8") as f:
    text = f.read()      # encoding="utf-8" keeps non-English text intact

with open("corpus.txt", encoding="utf-8") as f:
    for line in f:
        ...

Strings and adjacent pairs

for ch in s: ...           # iterate characters
"".join(list_of_chars)      # characters back to a string
zip(seq, seq[1:])          # adjacent pairs: (s0,s1), (s1,s2), ...
max(d, key=d.get)          # dict key with the largest value
from collections import Counter
Counter(zip(seq, seq[1:])).most_common(1)   # most frequent pair

numpy patterns

import numpy as np
np.zeros((5, 4)); np.ones(3)
a.shape; a.reshape(2, 10)
a[0, 1]; a[0]; a[1:3]
a + b; a * 2
a.sum(); np.max(a); np.argmax(a)
a @ b; np.dot(a, b)        # dot product / matmul
np.linalg.norm(v)          # vector length
M.mean(axis=0)             # column means
M @ q                      # (N,D) matrix times (D,) vector -> (N,) scores
np.argsort(scores)         # sort indices (ascending)
X, Y = np.meshgrid(xs, ys) # 2D coordinate grid from two 1D arrays

matplotlib

import matplotlib.pyplot as plt
plt.scatter(xs, ys); plt.plot(xs, ys)
plt.contour(X, Y, Z, levels=30)   # contour lines (plt.contourf to fill)
plt.colorbar()                    # optional scale
plt.xlabel("x"); plt.ylabel("y"); plt.title("t")
plt.savefig("plot.png"); plt.show()

Debugging reminders

Everything here also appears, with a sentence of context, in Python basics. If an entry needs explaining, read it there.