← Slides Course overview Original Markdown (.md) All materials (.zip)

02 — Python fundamentals

Time: 2–4 h, plus individual practice. Goal: write short functions without copying verbatim.

Start with editor + terminal, not a notebook

First complete the macOS workbench. Keep VS Code and Terminal.app open on the same folder. Type a function in hello.py, save with Command-S, run with python3 hello.py, then change/save/rerun using Control-P to recall the command. The tight edit–save–run–inspect loop matters more than typing speed.

Resource

Harvard's CS50P Week 0 (functions/variables), Week 1 (conditions) and Week 2 (loops). The Week 0 PDF slides are useful on a second screen; use the lecture pages for current materials.

Minimum syllabus

First micro-lab: observe an edit

Create hello.py in VS Code, type the following, save with Command-S, then run python3 hello.py in Terminal:

name = 'friend'
print(f'Hello, {name}!')

Change the name in the editor; save; recall the command with Control-P; intentionally introduce a typo and fix it. The console executes the saved file; a changed but unsaved editor buffer is not automatically the same thing.

Guided lab: predictable responses

Start with:

def respond(message: str) -> str:
    text = message.lower().strip()
    if "hello" in text:
        return "Hello!"
    if text.endswith("?"):
        return "I cannot answer that yet."
    return "Tell me more."

Add five rules. Separate input/output from the core function so you can test it.

Work from examples/rule_bot.py. Run:

python3 examples/rule_bot.py

Independent exercises

  1. Turn the rules into a dictionary and add a default response.
  2. Count turns in the conversation and stop on quit.
  3. Write a pure function that detects whether a number is even.
  4. Create two tests: greeting and unknown question.

Mastery checkpoint

Without looking up a complete solution, implement a function that takes a list of strings and returns only the ones containing "python" (case-insensitively). Talk through its return type and test one edge case.

Next: development workflow