1.4.12 · D1Python & Scientific Computing

Foundations — Reading documentation and debugging

1,865 words8 min readBack to topic

Before you can read a traceback or a numpy signature, you must be able to read the vocabulary the parent note quietly assumes. This page builds every one of those pieces from nothing, in an order where each rung of the ladder rests on the one below it.


0. The very bottom rung: a "value"

You already met these in 1.4.1-Python-basics-syntax-and-variables. We name a box with =:

score = 85

Read this right-to-left: "take the value 85, put it in a box, hang the label score on it." The = is not the maths equals — it is an arrow pointing left, an instruction to store.

Figure — Reading documentation and debugging

1. Type — "what kind of thing is in the box?"

Why does the topic need this? Because the parent's debugging story ends with

print(student, type(student['score']))  # ... <class 'int'>

That type(...) call is the single most useful debugging question: "Is the box holding the kind of thing I think it is?" A bug is very often a str "85" sitting where an int 85 was expected — they look identical when printed, but you cannot do maths on text.

Figure — Reading documentation and debugging

2. Dict — a box with named compartments

You open a compartment with square brackets:

student['score']   # -> 85   ("look up the 'score' locker")

The parent note leans on this constantly: student['score'], record['price']. The ['...'] is the lookup operation — "fetch the value filed under this key."


3. List and the loop — doing a thing to many things

To visit every carriage we use a for loop:

for student in students:      # "for each carriage, call it 'student'"
    total += student['score'] # do something with it

total += x is shorthand for total = total + x — "add x into the running total." The += is an accumulator: it grows a value across the loop.


4. len( ), round( ), and the idea of a "function call"

  • len(students)how many items → 2
  • round(average, 2) → round average to 2 decimal places

The word argument just means "the thing you put in the parentheses." The word return means "the thing that drops out." The parent note's whole documentation section is really about reading the label on the vending machine: what buttons exist, what coins they need, what snack comes out.

Figure — Reading documentation and debugging

5. Signature, parameters, defaults — reading the machine's label

Reading it left to right:

  • Required parameter (start, stop): no =, so you must supply it.
  • Default parameter (num=50): has =value, so if you say nothing, it uses 50.

6. The traceback — a story read bottom-to-top

The parent note says "read bottom-to-top." Here's why the picture makes that obvious:

  • Top line = where your program started (<module>, line 45).
  • Each lower line = one function that called the next.
  • Bottom line = the exact statement that exploded, plus the exception type (KeyError, TypeError).
Figure — Reading documentation and debugging

7. Big-O notation — measuring "how much work"

The parent claims binary-search debugging turns work into . Let's earn those symbols.

Why and not something else? Because halving is repeated division by 2, and "how many times can I halve before I hit 1?" is exactly what answers. Every print that splits the suspect region in two is one halving.

Read: "the cost of searching lines = one check now, plus the cost of searching the surviving half." Unrolling that recursion is why the answer is a logarithm.


Prerequisite map

value and the = arrow

type: what kind of value

dict: named lockers

list plus for-loop plus +=

function call: inputs to output

signature: params and defaults

traceback bottom-to-top

Big-O: how much search work

Reading documentation

Systematic debugging

1.4.12 Reading docs and debugging

Both roads — the topic's "read docs" road and its "debug" road — start at the same humble box holding one value.


Equipment checklist

Cover the answer, say it out loud, then reveal.

What does = actually do, and how do you read it?
It stores: read right-to-left, "put this value in a box and hang this name on it." It is not maths-equals.
How do = and == differ?
= stores a value; == asks a True/False question comparing two values.
What does type(x) tell you and why care while debugging?
The kind of value in the box (int/float/str/None). Bugs often come from text sitting where a number was expected — they print the same but behave differently.
How do you fetch the value filed under 'score' in a dict?
student['score'] — square brackets do a key lookup.
What common error does a missing/misspelled dict key raise?
A KeyError.
In total += student['score'], what is += doing?
Accumulating — it means total = total + student['score'], growing a running total across the loop.
What is the difference between a parameter and an argument?
Parameter = the named slot in the signature; argument = the actual value you pass when calling.
How do you spot a default in a signature like num=50?
It has =value; if you say nothing, that value is used automatically.
Which line of a traceback names the real culprit?
The bottom line (most recent call last) plus its exception type; top lines only show how you arrived.
Why is binary-search debugging not ?
Each test halves the remaining suspect region, and counts how many halvings reach a single line.