Day 2 ยท Labeled boxes and forks in the road

Variables, Types & Control Flow

You will be able to
  • Create variables and explain binding a name vs the value it points to
  • Identify and convert between int, float, str, bool, and None
  • Write branching logic with if/elif/else, including compound conditions
  • Repeat work with while and for/range, controlling loops with break and continue
  • Format output with f-strings and read user input with input()
Today's ~125 minutes
Spaced-rep warm-up: Day 1 flashcards10 min
Concept study: ELI5 + tech, with the cells-vars visualizer20 min
Guided: boxes lab, advice machine, loops45 min
Practice: the guessing game20 min
Project: choose-your-path quiz20 min
Quiz + flashcards10 min

Builds on: Day 1 โ€” Setup, REPL vs scripts, running programs

The analogy

Picture a warehouse of boxes, each with a name label stuck on the front. A variable is the label, not the box: age = 30 writes "age" on a sticky label and slaps it on a box containing 30. Later, age = 31 peels the label off and sticks it on a different box โ€” the old box is unchanged, the label just moved. Boxes come in kinds โ€” whole numbers (int), decimals (float), text (str), yes/no switches (bool), and a special "nothing here" marker (None) โ€” and the kind decides what you can do: you can add number boxes, but adding a number box to a text box makes Python object.

Now the second half: a program is normally a straight road, executed top to bottom. Control flow adds forks and roundabouts. if is a fork โ€” "if it's raining, take the left path, else the right." while and for are roundabouts โ€” "keep circling until the condition says stop" or "go around once per item." With labels for memory and forks for decisions, your programs stop being recited lines and start being behavior.

Why this matters on the job

Every system you will ever build is variables plus branches plus loops wearing increasingly fancy costumes. The agent loop you build on Day 120 is literally a while-loop with an if inside: "while the task is not done, decide the next action." Retry logic on Day 107 is a for-loop with a break. On customer calls, an FDE constantly sight-reads unfamiliar code โ€” "what does this branch do when the value is None?" โ€” and today's material is exactly that reading skill. Types matter early too: half of all beginner bugs (and plenty of production ones) are a str pretending to be an int.

Watch it happen

Labeled boxes โ€” names point at values, they don't contain them

step 1 / 6
x
10
int

x = 10 sticks the label "x" onto an int object. The name is a sticky note, not a box that owns the number.

Guided practice

guided 1

Boxes lab โ€” bindings, types, conversions

15 min
  1. Create boxes.py in week-01 and work through the starter code, running after each numbered section (comment out later sections until you get there).
  2. Section 1: predict each printed type before running. type(x) tells you what kind of box a name points to.
  3. Section 2: the rebinding demo โ€” convince yourself the label moved and no box changed.
  4. Section 3: fix the deliberately broken conversion line so it prints the right total. This is the input()-returns-str trap; you are meeting it today so it cannot ambush you on Day 7.
  5. In one comment at the bottom, answer: what type does input() return, always?
๐Ÿ python โ€” editable, runs in your browser
Ctrl/โŒ˜+Enter runs ยท Tab indents ยท numpy/pandas/sklearn auto-load on import (torch and network calls need a local run)
guided 2

Forks in the road โ€” an advice machine

15 min
  1. Create advice.py. It reads a temperature and prints clothing advice. Type the starter code and run it a few times with different numbers.
  2. Trace the flow: exactly ONE branch runs โ€” Python checks conditions top to bottom and takes the first true one. Reorder the first two branches and observe how a temperature of 35 now gets the wrong advice; order matters.
  3. Add a new elif for temperatures below -10 ("stay home"). Where must it go to ever be reached?
  4. Extend the final print with a compound condition: if it is between 18 and 24 inclusive (18 <= t <= 24), also print "perfect t-shirt weather".
  5. If your environment does not support input(), replace the input line with t = 21 and rerun with several hard-coded values โ€” the logic is the lesson, not the typing.
๐Ÿ python โ€” editable, runs in your browser
Ctrl/โŒ˜+Enter runs ยท Tab indents ยท numpy/pandas/sklearn auto-load on import (torch and network calls need a local run)
guided 3

Roundabouts โ€” while, for, break, continue

15 min
  1. Create loops.py with the starter code. Section 1 is a countdown: predict the last number printed before running (hint: the condition is checked BEFORE each pass).
  2. Section 2: for + range. Change it to range(1, 11) and then range(0, 30, 5) โ€” write down what the third argument does.
  3. Section 3: a search loop with break. Add a print AFTER the loop showing whether the target was found (set a variable found = True inside).
  4. Section 4: continue โ€” the loop skips multiples of 3. Flip it to instead skip everything EXCEPT multiples of 3.
  5. Deliberately create an infinite loop (remove the n = n - 1 line in section 1), run it, and stop it with Ctrl+C. Every programmer does this weekly; now it holds no fear.
๐Ÿ python โ€” editable, runs in your browser
Ctrl/โŒ˜+Enter runs ยท Tab indents ยท numpy/pandas/sklearn auto-load on import (torch and network calls need a local run)

On your own

The guessing game

20 min

Build guess.py: the classic number-guessing game, from a blank file.

Goal: the program holds a secret number (hard-code secret = 37 or use import random and random.randint(1, 100) โ€” a preview of Day 3). It repeatedly asks the player for a guess, replies "higher" or "lower", and congratulates them when correct โ€” including how many guesses it took.

Constraints: use a while loop; count attempts in a variable; use f-strings for every message; the reply for a correct guess must break out of the loop. Bonus: after 7 wrong guesses, print a mercy hint (is the secret even or odd? secret % 2).

Hints (only if stuck): the loop condition can be while True: with a break on success. Remember what type input() returns before you compare it to a number.

Ship before you stop

Choose-your-path quiz

Build pathquiz.py: a five-question command-line quiz about anything you like (Day 1โ€“2 material is a good default). Each question prints, reads an answer with input(), compares case-insensitively where sensible, and updates a running score. Wrong answers print the correct one โ€” the quiz should teach, like this program's own quizzes do. After question five, print a summary with an f-string ("You scored 4/5") and use if/elif/else to award a verdict tier (5 = "expedition ready", 3โ€“4 = "solid", under 3 = "revisit today's notes"). Use at least one loop (e.g. re-ask a question until the answer is non-empty). Save it in week-01; tomorrow you will refactor it with functions.

Rubric โ€” check what you completed (0/5)

Common mistakes & misconceptions

  • Forgetting that input() returns a str: comparing input("guess: ") == 37 is always False because "37" is not 37. Convert with int() first.
  • Using = when you mean ==. Single = binds a name; double == compares. Inside an if, = is a SyntaxError โ€” Python catches this one for you.
  • Believing reassignment changes the old value. x = 99 rebinds the label x; anything else pointing at the old value still sees it. (This distinction gets sharper with lists on Day 4.)
  • Off-by-one with range: range(5) is 0..4, and range(1, 5) is 1..4 โ€” the stop value is never included. Say the sequence out loud before trusting a loop.
  • Writing a while loop that never changes its condition variable โ€” an infinite loop. Ask of every while: what line in the body moves it toward False?
  • Testing if x == True instead of if x, or if name != "" instead of if name. Truthiness exists so conditions read cleanly; use it.
Knowledge check

Q1. What does this print? x = "5" y = x + x print(y)

Q2. How many times does this loop print? for i in range(2, 8):

Q3. In an if/elif/elif/else chain where several conditions are true, what runs?

Go deeper โ€” curated resources

docsPython Tutorial โ€” An Informal Introduction (numbers, strings, first steps) โ†—25 mindocsPython Tutorial โ€” More Control Flow Tools (if, for, range) โ†—20 minbookAutomate the Boring Stuff โ€” Ch. 1: Python Basics โ†—30 minbookAutomate the Boring Stuff โ€” Ch. 2: Flow Control โ†—30 min
If you have a third hour
  • match/case โ€” structural pattern matching โ€” Python 3.10+ has a match statement, a supercharged if/elif for matching shapes of data. Skim the tutorial section on it once if/elif feels comfortable; you will see it in modern codebases.
Done means
  • All three guided scripts run; the input() trap fix explained in a comment
  • Guessing game works including the attempt counter
  • Path quiz passes all five rubric checks
  • Quiz โ‰ฅ 2/3 (revisit the tech section on types or loops if lower)
How this connects

โ† Back: On Day 1 you learned to run code and read errors; today the code started making decisions. The TypeError you caused deliberately yesterday is the same one the input() trap produces.

Forward โ†’: Day 3 wraps today's logic into reusable functions โ€” your path quiz gets refactored. The while-plus-if shape you wrote in the guessing game is, at full scale, the agent loop of Day 120, and truthiness returns everywhere from Day 4 collections to Day 110 validation code.

Unlocks: D3 Functions, Scope & Modules ยท D4 Collections ยท D5 Strings, Files & Errors ยท D7 Week 1 Checkpoint: CLI Task Tracker