Learning objectives
By the end you can:
- Choose the right built-in container (
list,tuple,dict, orset) for a given job by asking three questions: does order matter, do duplicates matter, and how will this be looked up (by position, by key, or by membership)?, and defend that choice in plain language (CS1.04 will attach exact Big-O numbers to the same intuition). - Write idiomatic list, dict, and set comprehensions (including a filtering
ifclause and, where it stays readable, one level of nesting) and generator expressions, in place of manual accumulator loops; and recognize the point at which a comprehension has stopped being idiomatic and started being clever. - Use slicing (
start:stop:step, negative indices, slice assignment) fluently on lists, tuples, and strings, and state precisely when slicing produces an independent copy versus when plain indexing produces a shared reference (an alias). - Distinguish mutable from immutable built-in types, explain aliasing (
a = bshares one object) versus copying (copy.copy(),copy.deepcopy(), or a slice make a new one), and both predict and repair the classic mutable-default-argument bug before it ships. - Default to writing pure functions (no mutation of arguments, no hidden reads or writes of outside state, no printing) and explain concretely why purity makes code easier to test, reorder, and trust; and recognize when an impure function (I/O, logging) is legitimate, and how to keep it at the edges of a program rather than scattered through the middle.
- Organize code into modules and packages: write and import from a module, structure a small package with
__init__.py, use theif __name__ == "__main__":guard correctly, and avoid the commonest import mistakes (name-shadowing a stdlib module, a circular import, a wildcard import that hides where a name came from). - Design a clean class: constructors (
__init__), Python's encapsulation conventions (_protected, name-mangled__attr), and the core dunder methods (__repr__,__str__,__eq__,__hash__,__lt__(ordering),__len__,__iter__,__contains__) that make a custom object behave like a native part of the language instead of an opaque blob. - (Productivity objective: R10 duality / the Copilot Discipline.) Use an AI coding copilot to accelerate writing container-choice, comprehension, and dunder code for tasks like this module's, without skipping the by-hand mastery gate, and review every AI-suggested line for a hallucinated API, a subtly wrong edge case, or a silently broken invariant before it ships.
- Apply objectives 1–7 together to a small, testable, finance-flavored data model (
Position,Trade,Portfolioclasses over a joint India (₹, NSE) and US ($, Nasdaq/NYSE) basket) graded automatically by the CS1.02 code pack, hidden tests included.
The duality, stated once (R10). Like every AI-touched module in this curriculum, this node carries two objectives at once. The understanding objective (items 1–7 and 9) is what the mastery gate rewards: you earn it by writing the by-hand container choice, the pure function, the dunder pair, yourself, and by being able to explain why each one is right. The productivity objective (item 8) is the payoff you keep afterward: knowing how to point a coding copilot at exactly this kind of task once you already understand it, and knowing how to review what it hands back instead of trusting it on sight. A copilot can accelerate the rest of your career with this material. It cannot buy you the first pass through it.
Prerequisites & connections
Builds on. CS1.01 (Programming from Zero. Python I). This module assumes fluency with variable assignment; the built-in scalar types (int, float, str, bool); arithmetic and string operators; if/elif/else; for/while loops; writing and calling functions with positional and keyword arguments; and basic console I/O (print, input). If any of that feels shaky, go back to CS1.01 first, this module builds directly on top of it and will not re-teach it (R1: reference, don't duplicate).