TwiddleLab / Courses / Code
Build with Python
Write real Python: strings, loops, dictionaries and grids, up to a tic-tac-toe bot.
Best after: How a Programmer Thinks
Lessons
- Strings Are Beads (5 min): A string is a row of characters, like beads on a thread. name[0] is the first bead.
- Text Tricks (7 min): A method is a function that belongs to a value, called with a dot: msg.upper(), msg.lower(), msg.replace(old, new).
- While: Until It's Done (7 min): A while loop repeats its body as long as its condition is True.
- And, Or, Not (7 min): a and b is True only when both are True.
- Loops Inside Loops (7 min): A nested loop is a loop inside a loop: the inner loop runs all the way through for every turn of the outer one.
- Dictionaries: Labels, Not Numbers (7 min): A dictionary stores key: value pairs: scores = {"Mia": 12}.
- Counting with a Dictionary (7 min): The counting pattern: counts[w] = counts.get(w, 0) + 1.
- Debugging Like a Detective (7 min): A logic error gives a wrong answer with no red message.
- Functions That Call Functions (7 min): Decomposition: split a big job into small named functions that call each other.
- Randomness and Simulation (7 min): import random, then random.randint(a, b) gives a whole number from a to b, both ends included.
- Grids: Lists of Lists (7 min): A grid is a list of lists: board[1] is a whole row, board[1][2] one cell.
- Tic-Tac-Toe with a Bot (7 min): Program design: plan the parts first (show, move, check), then write each as a function.
Lessons are interactive and need JavaScript. Turn it on to play them.