A compact, keep-open syntax reference for the subset the early builds use. Lookup, not explanation.
x = 5 # int
r = 0.1 # float
s = "text" # str
flag = True # bool
nothing = None
xs = [1, 2, 3]
xs.append(4)
xs[0]; xs[-1]; len(xs)
cell = (4, 0)
row, col = cell
d = {"a": 1}
d["b"] = 2
d["a"]; "a" in d; d.items()
blocked = {(1, 1), (2, 2)}
(1, 1) in blocked
xs[0]; xs[-1]
xs[1:3]; xs[:2]; xs[2:]
if x > 0:
...
elif x < 0:
...
else:
...
for v in xs: ...
for i in range(5): ...
for i, v in enumerate(xs): ...
while not done: ... # break / continue
def f(a, b=1):
return x, y, z
x, y, z = f(2)
import numpy as np
import random
from math import sqrt
f"episode {ep}: {total:.2f}"
[v * v for v in range(5)]
[v for v in xs if v % 2 == 0]
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:
...
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
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
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()
print("Q row:", Q[s]).print(a.shape).ModuleNotFoundError means the package is not installed in this Python).