1.3.2 · D3Python Intermediate

Worked examples — Standard library — math, random, os, sys, datetime, time, collections, itertools, functools

3,101 words14 min readBack to topic

Parent: Standard Library (parent topic)


The scenario matrix

Before any example, here is the complete list of case-classes these modules can throw at you. Every cell below is covered by a worked example (its number is in brackets).

# Case class What makes it tricky Covered by
A Normal combinatorial count plug into a formula Ex 1
B Degenerate / boundary input , , empty iterable Ex 2
C Zero / edge numeric input , hypot(0,0), factorial(0) Ex 3
D Inclusive-vs-exclusive ends (sign of the boundary) randint vs randrange vs range Ex 4
E Reproducibility (same seed → same stream) deterministic "randomness" Ex 5
F Counting occurrences + missing-key default Counter, defaultdict Ex 6
G Both-ends queue, big-O contrast deque vs list Ex 7
H Infinite / lazy iterator, must be truncated count, product, exhaustion Ex 8
I Fold to one value (empty-fold degenerate case too) reduce, initial value Ex 9
J Real-world word problem (dates) + exam twist timedelta, strftime, month rollover Ex 10

The matrix has three "danger axes" that appear again and again:

  1. Boundary — what happens at , at , at "nothing"?
  2. Inclusive vs exclusive — does the top end count or not?
  3. Lazy vs eager — is the result a finished list, or a one-shot stream?

Keep those three in your head as you read.


Ex 1 — Cell A: a plain combination count


Ex 2 — Cell B: degenerate / boundary inputs


Ex 3 — Cell C: zero and edge numeric inputs


Ex 4 — Cell D: inclusive vs exclusive endpoints (the sign of the boundary)

This is the classic mistake the parent note steel-manned. Look at the picture — three tools, three different treatments of the top end.

Figure — Standard library — math, random, os, sys, datetime, time, collections, itertools, functools

Ex 5 — Cell E: reproducibility (same seed → same stream)


Ex 6 — Cell F: counting occurrences + missing-key default


Ex 7 — Cell G: both-ends queue, big-O contrast

The parent note claimed deque.popleft() is while list.pop(0) is . Here is why, visually.

Figure — Standard library — math, random, os, sys, datetime, time, collections, itertools, functools

Ex 8 — Cell H: infinite / lazy iterator, must be truncated


Ex 9 — Cell I: fold to one value, plus the empty-fold degenerate case


Ex 10 — Cell J: real-world date word problem + exam twist


Recall

Recall Which of

range, randrange, randint can produce the top endpoint? Which one includes b in (1, b)? ::: Only random.randint(1, b) — the other two use a half-open interval and exclude b.

Recall What is

list(combinations([], 2)) versus list(combinations('AB', 0))? One is empty, one holds a single empty tuple — which is which? ::: combinations([],2)[] (can't choose 2 from nothing); combinations('AB',0)[()] (exactly one way to choose nothing).

Recall Why must

reduce(f, []) be given a third argument? What breaks on an empty fold? ::: With no items and no initial accumulator there is nothing to return, so it raises TypeError. Supply an initial value (the identity: 0 for sums, 1 for products).