05 / 90

Day 05: Mutation, identity, types, and comparisons

Explain mutation versus rebinding, use == and is deliberately, and diagnose misleading inputs and beginner failures with observable Python data.

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

Mutation versus rebinding trace

Independent Mastery Check — no AI assistance during the assessed work. If you receive any help, declare it honestly; assisted work counts only as remediation, and you must complete a fresh unassisted attempt before claiming mastery.

Prerequisites

Day 04 value-and-binding check completed; you can save a short Python file and run it from the terminal.

Learning Objective

Predict, run, and explain why append changes a list that two names share, while a = a + [2] rebinds only the name a.

Instructions

Required work:
1. Before running anything, write your prediction of both printed lines in the draft area.
2. Save the program from the exercise as trace.py and run python trace.py.
3. Copy the exact output next to your prediction and mark every mismatch.
4. For each case, explain in one or two sentences which list object a and b refer to after the third line of that case.

Optional work: once the required evidence is complete, add print(id(a), id(b)) after each case and confirm that the two ids match only in case 1.

Practice & Prediction Log

Predict the output, then run:

a = [1]
b = a
a.append(2)
print("case 1:", a, b, a is b)
a = [1]
b = a
a = a + [2]
print("case 2:", a, b, a is b)

Why does b change in case 1 but not in case 2?

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.

After running a = [1], then b = a, then a = a + [2], what does print(b) show?

Attempt History & Evidence

    Mastery Criterion

    Without AI assistance, you recorded a prediction before running, the exact two output lines, and a correct explanation that distinguishes mutating a shared object (case 1) from rebinding one name to a new object (case 2).

    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

    Equality, identity, and misleading inputs

    Independent Mastery Check — no AI assistance during the assessed work. If you receive any help, declare it honestly; assisted work counts only as remediation, and you must complete a fresh unassisted attempt before claiming mastery.

    Prerequisites

    Activity 1 (mutation versus rebinding) completed; Day 04 value-and-type check.

    Learning Objective

    Use == for value comparison and is for identity (especially is None), and expose misleading inputs such as the text "3" versus the integer 3 with printed evidence.

    Instructions

    Required work:
    1. Write a prediction for every value on the four printed lines.
    2. Save the program as compare.py, run it, and record the exact output beside your prediction.
    3. Explain why [1] == [1] is True but [1] is [1] is False.
    4. Explain why raw == 3 is False even though the text looks like 3, and what int(raw) changes.
    5. Explain why value is None is the recommended way to test for None.

    Optional work: once the required evidence is complete, set raw to " 3 " and then to "3.0", predict what int(raw) does each time, and run it.

    Practice & Prediction Log

    Predict every value, then run the program. raw stands for what input() returns when a user types 3: input() always returns a string.

    print(1 == True, 0 == False)
    print([1] == [1], [1] is [1])
    raw = "3"
    print(raw == 3, int(raw) == 3)
    value = None
    print(value is None)

    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 if value is None: preferred over if value == None:?

    Attempt History & Evidence

      Mastery Criterion

      Without AI assistance, your predictions and exact output cover all four lines, and your explanations correctly separate value equality (==) from object identity (is), name the str-versus-int mismatch, and justify is None.

      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

      Beginner failure clinic: SyntaxError, str versus int, and None

      Agent-Assisted Engineering Check — you may ask an authorized coding agent to help diagnose. Declare the tool, what it was allowed to read or change, and your review boundary. You must run every starter and fix yourself and review every diff; agent output is not independent evidence. If no authorized tool is available, do the clinic by hand and record the agent part as “blocked — not run”.

      Prerequisites

      Activities 1–2 completed; you can read the last line of a traceback and compare two files with `diff` or `git diff`.

      Learning Objective

      Reproduce three classic beginner failures from complete starter files, name each failure category before editing, apply a minimal fix, and confirm it with post-fix output and edge cases.

      Instructions

      Required work:
      1. Save the three starter files from the exercise exactly as shown.
      2. Before running each one, write a prediction: will it fail, and in which category (syntax error, type/value mismatch, or invalid operation on None)?
      3. Run each starter and copy the exact last line of the error, or the printed output.
      4. Fix each starter with the smallest change, save it as fixed_syntax.py, fixed_compare.py, or fixed_none.py, compare it with diff -u starter_x.py fixed_x.py (or git diff), and run the fixed file.
      5. For the comparison bug, run two edge cases after the fix — raw = "4" and raw = "three" — and state which inputs the program accepts.
      6. If you used an agent, record the tool, its permissions, what it suggested, and which suggestion you verified or rejected.

      Optional work: once the required evidence is complete, make the comparison program reject non-numeric text with a clear message instead of a traceback.

      Practice & Prediction Log

      Three complete starter programs, each with exactly one bug:

      starter_syntax.py

      score = 3
      if score = 3:
      print("passed")

      starter_compare.py (raw stands for what input() returns when a user types 3)

      raw = "3"
      if raw == 3:
      print("passed")
      else:
      print("not passed:", repr(raw), type(raw).__name__)

      starter_none.py

      items = None
      items.append(1)
      print(items)

      Run each one, name the failure category, fix it, and show the output after the fix.

      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.

      starter_compare.py runs without any error but prints not passed: '3' str. Which failure category is this?

      Attempt History & Evidence

        Mastery Criterion

        For all three starters you recorded a prediction, the exact error line or output, the failure category, a minimal diff, and the post-fix output; the comparison fix is checked with raw = "4" and raw = "three"; and any agent use is declared with tool, permissions, and review boundary.

        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

        Review: trace table, four comparison predictions, and prevention rules

        Independent Mastery Check — no AI assistance during the assessed work. If you receive any help, declare it honestly; assisted work counts only as remediation, and you must complete a fresh unassisted attempt before claiming mastery.

        Prerequisites

        Activities 1–3 completed, including your recorded failure-clinic evidence.

        Learning Objective

        Independently reproduce the day's evidence: a two-column mutation/rebinding trace, four predicted-then-verified comparisons, and one prevention rule for each failure from the clinic.

        Instructions

        Required work:
        1. Without AI assistance and before running any code, fill in a two-column trace (column 1: mutation with append; column 2: rebinding with a = a + [2]) showing a, b, and a is b after each line.
        2. Write your prediction (True or False) for the four comparisons 1 == True, [1] == [1], [1] is [1], and "3" == 3.
        3. Run the program in the exercise and mark each prediction correct or incorrect.
        4. Write one prevention rule for each of the three clinic failures (syntax error, str versus int, None) and cite the observed output line that supports it.

        Optional work: once the required evidence is complete, invent one new comparison that mixes equality and identity, predict it, and verify it.

        Practice & Prediction Log

        Fill in the trace and the four predictions first, then run:

        a = [1]
        b = a
        a.append(2)
        print("mutation:", a, b, a is b)
        a = [1]
        b = a
        a = a + [2]
        print("rebinding:", a, b, a is b)
        print(1 == True)
        print([1] == [1])
        print([1] is [1])
        print("3" == 3)

        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.

        Which question is about identity rather than equality?

        Attempt History & Evidence

          Mastery Criterion

          Without AI assistance: the trace is correct before running, all four predictions are recorded and checked against the exact output, and each of the three prevention rules cites an observed output line.

          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.