CoursesAIMathCodePhysicsAbout and FAQ

TwiddleLab / Courses / Code

Build with Python

Write real Python: strings, loops, dictionaries and grids, up to a tic-tac-toe bot.

Intermediate · 12 lessons · Free, no account needed

Best after: How a Programmer Thinks

Start the course

Lessons

  1. Strings Are Beads (5 min): A string is a row of characters, like beads on a thread. name[0] is the first bead.
  2. 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).
  3. While: Until It's Done (7 min): A while loop repeats its body as long as its condition is True.
  4. And, Or, Not (7 min): a and b is True only when both are True.
  5. 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.
  6. Dictionaries: Labels, Not Numbers (7 min): A dictionary stores key: value pairs: scores = {"Mia": 12}.
  7. Counting with a Dictionary (7 min): The counting pattern: counts[w] = counts.get(w, 0) + 1.
  8. Debugging Like a Detective (7 min): A logic error gives a wrong answer with no red message.
  9. Functions That Call Functions (7 min): Decomposition: split a big job into small named functions that call each other.
  10. Randomness and Simulation (7 min): import random, then random.randint(a, b) gives a whole number from a to b, both ends included.
  11. Grids: Lists of Lists (7 min): A grid is a list of lists: board[1] is a whole row, board[1][2] one cell.
  12. 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.

Loading TwiddleLab