1.3.2 · D1Python Intermediate

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

1,849 words8 min readBack to topic

This is the parent topic's foundation layer. If a word in the parent felt like it was assumed, it is defined below in order — each idea uses only the ideas above it.


0. The absolute atom: a value and a name

Before any module, Python only knows two things: values (a piece of data like the number 5 or the text "hi") and names (labels you stick on values).

Picture a value as a labelled box, and the name as a sticky-note arrow pointing at that box.

Why the topic needs this: every line like now = datetime.now() sticks the name now onto whatever value the tool produced. If you don't see = as assignment, every example looks like a broken equation.


1. A function: a machine that takes inputs and returns an output

Picture a vending machine: you press buttons (arguments), it drops out a snack (return value).

Why the topic needs this: every line in the parent (sqrt(...), randint(1,6), Counter("banana")) is a call. The three parts — name, arguments, return — are the grammar of the whole page.


2. A module: the labelled drawer, and import

Picture a toolbox where each drawer has a label (math, random, os...). import math slides that drawer open; math.sqrt reaches in for one specific tool.

Why the topic needs this: the entire parent note is organised drawer-by-drawer. os.path.join, datetime.now, functools.reduce are all dot-paths into modules. Without the dot idea, os.path.join('a','b') is unreadable.


3. Collections: list, tuple, dict — where data is stored

Functions act on data, and that data usually lives in one of three shapes. The parent's whole collections section is about upgrading these three shapes.

Picture: a list is a numbered shelf, a tuple is that shelf welded shut, a dict is a labelled locker wall (you open a locker by its label, not its number).

Why the topic needs this: Counter("banana") returns a dict-like {'a':3,...}; deque([1,2,3]) upgrades a list; namedtuple upgrades a tuple. Every parent container builds on these three.


4. Iterator and lazy: producing items one at a time

The parent's itertools section leans hard on the word "lazy". Here is the picture behind it.

Picture a water tap (iterator) versus a full bucket (list): the tap gives water on demand and could run "forever" without a giant bucket.

Why the topic needs this: it explains why the parent writes list(permutations('ABC', 2)) and warns "they're exhausted after one pass." See Python Iterators and Generators for the deeper machinery.


5. Big-O: measuring how slow a tool gets as data grows

The parent says deque.popleft() is but list-front-removal is . Here is what means in plain words.

Why the topic needs this: it's the reason the parent prefers deque over list for queues, and why lru_cache turns naive fib from into . Deeper: Big-O Notation.


6. Math symbols the parent quietly uses

The parent's formula callouts use three notations. Each is just shorthand.

Why the topic needs this: math.comb(n,k) is ; permutations uses ; product(A,B) has size . These live in Combinatorics. The reduce/fib examples connect to Recursion and Memoization, and lru_cache is a special decorator. Reading/writing files with os builds toward File Handling in Python.


How the foundations feed the topic

value and name equals

function call name brackets args

module and dot import

collections list tuple dict

iterator and lazy

Big-O growth cost

math notation factorial choose

Standard Library topic

Read top-down: names and calls come first, drawers (modules) sit on calls, containers feed both laziness and cost-analysis, and math notation feeds the formula-heavy tools — all four streams pour into the Standard Library topic.


Equipment checklist

Cover the right side; can you answer each before revealing?

What does = mean in x = 5?
"Let the name x point at the value 5" — assignment, not equality.
What are the three parts of math.hypot(3, 4)?
The name hypot, the arguments 3, 4, and the returned value 5.0.
What does the dot mean in math.sqrt?
"The sqrt that lives inside the math module."
Difference between math.pi and math.sqrt(9)?
pi is a stored value (no brackets); sqrt(9) is a call that runs and returns a value.
List vs tuple vs dict in one word each?
List = changeable row; tuple = frozen row; dict = key→value lookups.
What index is the first item of a list?
Index 0.
What does "lazy iterator" mean?
It produces one item only when asked and doesn't store the rest.
Why can itertools.count(0) be infinite without crashing?
It generates each number on demand; nothing is stored, so no giant list is built.
What does vs mean?
= same effort regardless of size; = effort grows in step with input size .
What is in words?
The number of ways to choose items from when order doesn't matter.
What is ?
.
Recall Self-test: could you read a parent line cold?

Try reading list(combinations('ABC', 2)) aloud, naming every part. ::: combinations is a function inside itertools; 'ABC', 2 are its arguments; it returns a lazy iterator, and list(...) pours that iterator into a real list so you can see all items.