unrust

Python skill practice

How do you approach this?

Python Code Tracing Practice for Developers

Published · Updated

Published by Unrust. Scoring and editorial method

Quick answer

Trace Python by choosing a concrete input and recording the variables, object identities, branch decisions, and mutations that affect the result. Pay particular attention to aliases of lists and dictionaries, in-place methods that return None, and function calls that change an object a caller still references. The trace is a hypothesis you can check with a debugger or a focused assertion, not a substitute for executing uncertain behavior.

Original example

Trace a list alias before sorting it in place

scores = [8, 3, 5]
ranked = scores
ranked.sort()
first = scores[0]

ranked and scores refer to the same list, and sort mutates it while returning None. Does the caller expect scores to keep its original order? If it does, use a copied or non-mutating approach. With [8, 3, 5], the in-place version changes scores to [3, 5, 8]; sorted(scores) creates a distinct list and preserves scores. Choose the version that follows the ownership contract.

Follow aliases through mutable collections

Python assignment binds another name to the same object until you deliberately create a copy. Mark aliases in a state table and note whether append, sort, update, or a helper call changes the shared value in place. This prevents a local-looking edit from becoming an unexpected change in a caller or sibling branch.

Write loop invariants in plain language

For a loop, state what the accumulator, index, and collection represent before and after each iteration. This clarifies whether a branch handles the first item, an empty iterable, or the final update correctly, and it gives you a specific assertion to run when the actual result surprises you.

Check a concrete outcome

Use the original example with this input. These are local teaching fixtures, checked against the displayed snippet; they do not call external services.

Sorting ranked changes scores because the two names share one list. The first result is three and the caller's original ordering is lost.

Checked observation
Expression after the exampleObserved result
{"first": first, "scores": scores, "shared": ranked is scores}{"first": 3, "scores": [3, 5, 8], "shared": True}

Preserve the original order when the caller owns it

A shallow sorted copy is sufficient here because the elements are numbers. Copying a nested container would not independently copy its nested objects.

A separate ranked list
scores = [8, 3, 5]
ranked = sorted(scores)
first = ranked[0]
Identity-aware sorting trace
StepscoresrankedSame object?
Assign ranked = scores[8, 3, 5][8, 3, 5]Yes
ranked.sort()[3, 5, 8][3, 5, 8]Yes
Fresh input: ranked = sorted(scores)[8, 3, 5][3, 5, 8]No

Checklist

  • Which Python names refer to the same mutable object?
  • Does this method mutate in place or return a new value?
  • What invariant should hold after each loop iteration or helper call?

Practice loop

  1. 01Draw object-reference arrows for each Python list or dictionary passed into a small function.
  2. 02Record the accumulator and collection state after each loop iteration that can affect the result.
  3. 03Check the trace with a focused assertion or debugger at the first unexpected mutation.

Limits of this page

Manual tracing is less complete when generators, threads, async tasks, C extensions, or external I/O shape the behavior. A short trace is only a constrained snapshot and cannot determine whether unfamiliarity, time pressure, or a bad day caused a mistaken prediction.

Source context

Published by Unrust as practical teaching material using public language documentation and original examples. See the scoring and editorial method for scope, checking and limitations.

Check your own starting point

One diagnostic session is a snapshot, not causal proof of why a result occurred. It can help you choose a focused next practice step without making a broader claim about your ability.

Take the free diagnostic

Take the read in Python →Same six skills, side by side on your dashboard.