06 / 90

Day 06: Truthiness and branching with text/data

Build and test branches that handle normal, empty, and misleading text/data inputs, while using safe Git diffs and commits to preserve evidence.

Self-Directed Learning & Python 480 active minutes (8 hours focused study)
01 120 min

Truthiness table for real inputs

Independent Mastery Check (no AI assistance).

Prerequisites

Day 05 comparison check completed; you can run a Python file with python3 and read its output.

Learning Objective

Predict and verify bool() for empty, zero, and whitespace inputs, and explain why an `if value:` branch can mislabel real data.

Instructions

Required work: Before running anything, write down your prediction of bool(value) and of the branch presence(value) ("present" when the value is truthy, otherwise "missing") for each of [], [0], "", " ", 0, and "0". Then run the program, copy the exact output, and mark every row where your prediction differed.

Review: For each mismatch, write one sentence that states the rule: empty containers and numeric zero are falsy; every non-empty string, including " " and "0", is truthy.

Optional work: Only after the required evidence is complete, add None and [[]] to the list, predict them first, then run.

Practice & Prediction Log

Create truthiness.py that defines presence(value) (return "present" when value is truthy, otherwise "missing") and prints repr(value), bool(value), and presence(value) on one line for each of [], [0], "", " ", 0, "0", in that order.

Objective Knowledge Check & Diagnostic Trace

Answer the following questions to verify your understanding. Select your declared assistance condition before submitting.

Declared Assistance Condition

Note: AI-free work is an honest declared condition, not claimed surveillance.

1.

In the truthiness table, which input is truthy, so presence(value) reports it as "present", even though it holds no visible data?

Attempt History & Evidence

    Activity Mastery Criterion

    Without AI assistance, your prediction for all six inputs is written before execution, your captured output matches the reference exactly, and you correctly explain both misleading rows (`" "` reported present, `0` reported missing). Required and optional work are kept separate, and your evidence states the independent assistance condition.

    Authoritative Source Resources & Provenance

    linking PSF License Version 2

    Python Standard Library: Built-in Types

    Author: Python Software Foundation | Original Source Link ↗

    Relevant Section / Range: Sections Truth Value Testing, Comparisons, and Mutable Sequence Types

    Verified Version & Date: Python 3.12 documentation (verified 2026-10-30)

    Selection Rationale:

    Normative reference for truthiness, comparison, binding, and mutation behavior.

    Source Prerequisites:

    Day 03 recovery drill

    Safe Fallback (Offline Resilience):

    Run the included Python examples and consult the installed interpreter help.

    Compared Candidate Sources & Selection Analysis (2)
    Python Data Model reference ↗ compared_alternative [PSF License Version 2]

    Deeper object-semantics reference than the expression-level drills need; stdtypes keeps the operator-level material in scope.

    The Python Tutorial: An Informal Introduction ↗ compared_alternative [PSF License Version 2]

    Narrative introduction to the same operators; the library reference was selected as the normative signature source.

    linking CC BY-NC-SA 3.0 (book) noncommercial share-alike

    Git User Manual: Recording Changes

    Author: Git project | Original Source Link ↗

    Relevant Section / Range: Recording Changes to the Repository

    Verified Version & Date: Verified 2026-10-30

    Selection Rationale:

    Primary reference for inspecting a small diff before a safe commit.

    Source Prerequisites:

    Basic Git awareness

    Safe Fallback (Offline Resilience):

    Use git diff and git status locally; do not commit until the diff is understood.

    Compared Candidate Sources & Selection Analysis (2)
    git-diff command reference ↗ compared_alternative [Git documentation (GPL-2.0)]

    Command-level flag reference; the Pro Git chapter was selected for its guided walkthrough of inspecting changes before committing.

    Atlassian Git tutorials: git diff ↗ compared_alternative [All Rights Reserved]

    Friendly commercial walkthrough; the book chapter provides deeper staging/index semantics for the same inspection habit.

    02 120 min

    Explicit conditions and input normalization

    Independent Mastery Check (no AI assistance).

    Prerequisites

    Activity 01 truthiness table completed; you know what `str.strip()` and `int()` do.

    Learning Objective

    Implement classify(raw) against an exact contract that normalizes text once and converts only validated digit text, so missing, zero, positive, and invalid inputs are never confused.

    Instructions

    Required work: Read the contract in the exercise. Before running any code, write the return value you expect for each of the 16 inputs in the reference table (at minimum " 7 ", "", "0", and "unknown"). Implement classify(raw), run it on all inputs, capture the exact output, and compare it line by line with your predictions.

    Review: For every mismatch, name the contract rule that was broken, fix the code, and rerun until the output matches exactly.

    Optional work: Only after the required evidence is complete, add an input of your own (for example "٣", the Arabic-Indic digit three), predict its result from the contract, then run it.

    Practice & Prediction Log

    Write classify(raw) that follows this contract exactly:
    - Input: raw is always a str. Other types are outside the contract (for example, classify(None) raises AttributeError).
    - Normalize once: text = raw.strip(). This removes leading and trailing whitespace of every kind (spaces, tabs, newlines, the full-width space U+3000). Inner spaces are kept.
    - If text == "", return "missing".
    - If text consists only of the ASCII digits 0-9 (text.isascii() and text.isdigit()), convert it with int(text): return "zero" when the number is 0 (so "00" is zero) and "positive" otherwise (so "007" is positive).
    - Everything else returns "invalid": letters in any case ("unknown", "Zero"), signs ("-3", "+7"), decimal points ("3.5"), underscores ("1_000"), inner spaces ("1 2"), and non-ASCII digits ("7"). Invalid text is never treated as zero.
    - The return value is exactly one of the four lowercase strings "missing", "zero", "positive", "invalid".
    Print one line per input in the form f"{raw!r:12} -> {classify(raw)}", so the output is stable and whitespace stays visible.
    The 16 inputs to predict and run, in this order ("\t\n" is a tab plus a newline; "\u30007" is the full-width space U+3000 followed by 7): " 7 ", "", "0", "unknown", " ", "\t\n", "\u30007", "007", "00", "Zero", "-3", "+7", "3.5", "1_000", "1 2", "7".

    Objective Knowledge Check & Diagnostic Trace

    Answer the following questions to verify your understanding. Select your declared assistance condition before submitting.

    Declared Assistance Condition

    Note: AI-free work is an honest declared condition, not claimed surveillance.

    1.

    Under the Day 06 contract, what does classify(" 0\t") return?

    Attempt History & Evidence

      Activity Mastery Criterion

      Without AI assistance, your predictions for the 16 inputs are written before execution, your classify output matches the reference table exactly, and you can state the conversion rule in one sentence. Required and optional work are kept separate, and your evidence states the independent assistance condition.

      Authoritative Source Resources & Provenance

      linking PSF License Version 2

      Python Standard Library: Built-in Types

      Author: Python Software Foundation | Original Source Link ↗

      Relevant Section / Range: Sections Truth Value Testing, Comparisons, and Mutable Sequence Types

      Verified Version & Date: Python 3.12 documentation (verified 2026-10-30)

      Selection Rationale:

      Normative reference for truthiness, comparison, binding, and mutation behavior.

      Source Prerequisites:

      Day 03 recovery drill

      Safe Fallback (Offline Resilience):

      Run the included Python examples and consult the installed interpreter help.

      Compared Candidate Sources & Selection Analysis (2)
      Python Data Model reference ↗ compared_alternative [PSF License Version 2]

      Deeper object-semantics reference than the expression-level drills need; stdtypes keeps the operator-level material in scope.

      The Python Tutorial: An Informal Introduction ↗ compared_alternative [PSF License Version 2]

      Narrative introduction to the same operators; the library reference was selected as the normative signature source.

      linking CC BY-NC-SA 3.0 (book) noncommercial share-alike

      Git User Manual: Recording Changes

      Author: Git project | Original Source Link ↗

      Relevant Section / Range: Recording Changes to the Repository

      Verified Version & Date: Verified 2026-10-30

      Selection Rationale:

      Primary reference for inspecting a small diff before a safe commit.

      Source Prerequisites:

      Basic Git awareness

      Safe Fallback (Offline Resilience):

      Use git diff and git status locally; do not commit until the diff is understood.

      Compared Candidate Sources & Selection Analysis (2)
      git-diff command reference ↗ compared_alternative [Git documentation (GPL-2.0)]

      Command-level flag reference; the Pro Git chapter was selected for its guided walkthrough of inspecting changes before committing.

      Atlassian Git tutorials: git diff ↗ compared_alternative [All Rights Reserved]

      Friendly commercial walkthrough; the book chapter provides deeper staging/index semantics for the same inspection habit.

      03 120 min

      Safe diff and commit rehearsal

      Agent-Assisted Engineering Check: declare permissions and review boundary; agent output is not independent evidence.

      Prerequisites

      Activity 02 contract and reference classify completed; Git and python3 available in a terminal; an AI coding agent that you can give a read-only, bounded review request.

      Learning Objective

      Write expected outputs before asking an agent, then review a proposed fix in a disposable Git repository: test it against those expectations, check the diff with git diff --check, and commit only the reviewed file.

      Instructions

      Required work: Work only in a new disposable repository created with mktemp -d, never in a real project. (1) Write expected.txt with your own expected outputs for six inputs before you ask any agent. (2) Commit the buggy baseline classify.py, run it, and record the mismatch with diff. (3) Ask an agent for a bounded review with a request such as: "Review only classify.py against the contract below. Do not run commands, edit files, or commit. Reply with a proposed replacement and list which of the six inputs change result." Record the agent's name, the permissions you granted (read-only, one file), and its reply. (4) Apply the proposed replacement, inspect git diff, rerun the cases, run git diff --check, and commit only classify.py. The reference answer gives a complete reproducible sequence that uses the Activity 02 implementation as the proposal.

      Review: Write a prediction before each command, capture the exact output, and explain every difference between expected and observed output.

      Optional work: Only after the required evidence is complete, add a trailing space to one line of the committed classify.py, observe what git diff --check reports, then undo it with git restore classify.py.

      Practice & Prediction Log

      In a disposable repository, record your expected output for " 7 ", "", "0", "unknown", " ", and " 0 " under the Activity 02 contract (print format f"{raw!r} -> {classify(raw)}"). Commit a baseline, review an agent-proposed fix with git diff, verify it with diff expected.txt actual.txt and git diff --check, and commit only classify.py, leaving expected.txt and actual.txt untracked.

      Objective Knowledge Check & Diagnostic Trace

      Answer the following questions to verify your understanding. Select your declared assistance condition before submitting.

      Declared Assistance Condition

      Note: AI-free work is an honest declared condition, not claimed surveillance.

      1.

      Your outputs match expected.txt and git diff --check prints nothing. git status --short shows M classify.py, ?? actual.txt, and ?? expected.txt. Which command stages only the reviewed file?

      Attempt History & Evidence

        Activity Mastery Criterion

        Your expected.txt was written before the agent request; your evidence shows the baseline mismatch, the inspected diff, `diff exit: 0`, `check exit: 0`, and a commit that contains only classify.py while expected.txt and actual.txt stay untracked. The agent request, the permissions granted (read-only, one file), and the reply are recorded and labeled agent-assisted, not independent evidence. Required and optional work are kept separate.

        Authoritative Source Resources & Provenance

        linking PSF License Version 2

        Python Standard Library: Built-in Types

        Author: Python Software Foundation | Original Source Link ↗

        Relevant Section / Range: Sections Truth Value Testing, Comparisons, and Mutable Sequence Types

        Verified Version & Date: Python 3.12 documentation (verified 2026-10-30)

        Selection Rationale:

        Normative reference for truthiness, comparison, binding, and mutation behavior.

        Source Prerequisites:

        Day 03 recovery drill

        Safe Fallback (Offline Resilience):

        Run the included Python examples and consult the installed interpreter help.

        Compared Candidate Sources & Selection Analysis (2)
        Python Data Model reference ↗ compared_alternative [PSF License Version 2]

        Deeper object-semantics reference than the expression-level drills need; stdtypes keeps the operator-level material in scope.

        The Python Tutorial: An Informal Introduction ↗ compared_alternative [PSF License Version 2]

        Narrative introduction to the same operators; the library reference was selected as the normative signature source.

        linking CC BY-NC-SA 3.0 (book) noncommercial share-alike

        Git User Manual: Recording Changes

        Author: Git project | Original Source Link ↗

        Relevant Section / Range: Recording Changes to the Repository

        Verified Version & Date: Verified 2026-10-30

        Selection Rationale:

        Primary reference for inspecting a small diff before a safe commit.

        Source Prerequisites:

        Basic Git awareness

        Safe Fallback (Offline Resilience):

        Use git diff and git status locally; do not commit until the diff is understood.

        Compared Candidate Sources & Selection Analysis (2)
        git-diff command reference ↗ compared_alternative [Git documentation (GPL-2.0)]

        Command-level flag reference; the Pro Git chapter was selected for its guided walkthrough of inspecting changes before committing.

        Atlassian Git tutorials: git diff ↗ compared_alternative [All Rights Reserved]

        Friendly commercial walkthrough; the book chapter provides deeper staging/index semantics for the same inspection habit.

        04 120 min

        Three-day review: independent edge-case function

        Independent Mastery Check (no AI assistance).

        Prerequisites

        Activities 01-03 completed; your Day 04 and Day 05 notes available for the explanation.

        Learning Objective

        Without AI, implement and test a branch that handles an empty string, "0", and unknown text, explain one surprising Python behavior from Days 04-06, and record a one-file safe commit.

        Instructions

        Required work: Without AI, write the four acceptance cases ("", "0", "unknown", " 12 ") and their exact expected messages before coding. Implement quantity_message(raw) so that every case passes its assert, commit only quantity.py in a disposable repository, and write a short explanation of one surprising result from Days 04-06.

        Review: Write a prediction of the output before running, compare it literally with the expected messages, and explain any difference.

        Optional work: Only after the required evidence is complete, translate the four messages into another language while keeping the same rules and the same four cases.

        Practice & Prediction Log

        Implement quantity_message(raw) (where raw is a str) with these exact messages: empty or whitespace-only text → missing: enter a whole number; ASCII digit text equal to 0 → zero: nothing to order; other ASCII digit text → ok: <number>; anything else → invalid: <repr of the stripped text> is not a whole number. Test it with assert on "", "0", "unknown", and " 12 ", commit only quantity.py in a disposable repository, and explain one surprising result from Days 04-06 in two or three sentences.

        Objective Knowledge Check & Diagnostic Trace

        Answer the following questions to verify your understanding. Select your declared assistance condition before submitting.

        Declared Assistance Condition

        Note: AI-free work is an honest declared condition, not claimed surveillance.

        1.

        Why is the branch if raw: not enough to detect a missing quantity?

        Attempt History & Evidence

          Activity Mastery Criterion

          Without AI assistance, all four acceptance cases pass with the exact messages, your explanation correctly contrasts truthiness with explicit comparison, and the commit contains only quantity.py with a clean diff check and empty `git status --short` output afterwards. Required and optional work are kept separate, and your evidence states the independent assistance condition.

          Authoritative Source Resources & Provenance

          linking PSF License Version 2

          Python Standard Library: Built-in Types

          Author: Python Software Foundation | Original Source Link ↗

          Relevant Section / Range: Sections Truth Value Testing, Comparisons, and Mutable Sequence Types

          Verified Version & Date: Python 3.12 documentation (verified 2026-10-30)

          Selection Rationale:

          Normative reference for truthiness, comparison, binding, and mutation behavior.

          Source Prerequisites:

          Day 03 recovery drill

          Safe Fallback (Offline Resilience):

          Run the included Python examples and consult the installed interpreter help.

          Compared Candidate Sources & Selection Analysis (2)
          Python Data Model reference ↗ compared_alternative [PSF License Version 2]

          Deeper object-semantics reference than the expression-level drills need; stdtypes keeps the operator-level material in scope.

          The Python Tutorial: An Informal Introduction ↗ compared_alternative [PSF License Version 2]

          Narrative introduction to the same operators; the library reference was selected as the normative signature source.

          linking CC BY-NC-SA 3.0 (book) noncommercial share-alike

          Git User Manual: Recording Changes

          Author: Git project | Original Source Link ↗

          Relevant Section / Range: Recording Changes to the Repository

          Verified Version & Date: Verified 2026-10-30

          Selection Rationale:

          Primary reference for inspecting a small diff before a safe commit.

          Source Prerequisites:

          Basic Git awareness

          Safe Fallback (Offline Resilience):

          Use git diff and git status locally; do not commit until the diff is understood.

          Compared Candidate Sources & Selection Analysis (2)
          git-diff command reference ↗ compared_alternative [Git documentation (GPL-2.0)]

          Command-level flag reference; the Pro Git chapter was selected for its guided walkthrough of inspecting changes before committing.

          Atlassian Git tutorials: git diff ↗ compared_alternative [All Rights Reserved]

          Friendly commercial walkthrough; the book chapter provides deeper staging/index semantics for the same inspection habit.