1.3.2Python Intermediate

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

2,048 words9 min readdifficulty · medium6 backlinks

The map of drawers (the 20% that gives 80%)

Module What it's FOR (one word) Killer tools
math Pure-number math sqrt, floor, ceil, gcd, factorial, pi, comb, log
random Pseudo-randomness random, randint, choice, shuffle, sample, seed
os Talk to the OS / files getcwd, listdir, path.join, environ, makedirs
sys Talk to the interpreter argv, exit, path, maxsize
datetime Calendar dates & times datetime.now, timedelta, strftime, strptime
time Clock / sleep / timing time(), sleep(), perf_counter()
collections Better containers Counter, defaultdict, deque, namedtuple
itertools Lazy looping algebra count, cycle, product, permutations, combinations, chain, groupby
functools Tools that act on functions reduce, lru_cache, partial, wraps

math — exact numeric helpers


random — controllable chance


os & sys — the environment vs the interpreter


datetime & time — two different clocks


collections — containers with superpowers


itertools — lazy looping algebra


functools — tools that operate ON functions

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

Common mistakes (Steel-man + fix)


Active-recall flashcards

Which module for Counter, defaultdict, deque?
collections
math.comb(n,k) returns what, and as what type?
(nk)=n!k!(nk)!\binom{n}{k}=\frac{n!}{k!(n-k)!}, as an exact integer
Is random.randint(1,6) inclusive of 6?
Yes — both endpoints are inclusive
Difference between strftime and strptime?
strf = format object→string; strp = parse string→object
Why use os.path.join instead of string +?
It uses the correct OS separator → portable code
Why time.perf_counter() over time.time() for timing?
Highest-resolution monotonic clock; never jumps backward
Size of permutations(n, r)?
n!/(nr)!n!/(n-r)!
Size of combinations(n, r)?
(nr)=n!/(r!(nr)!)\binom{n}{r}=n!/(r!(n-r)!)
What does functools.reduce(f,[a,b,c]) compute?
f(f(a,b),c)f(f(a,b),c) — left-fold accumulation
What does lru_cache do for recursive fib?
Memoizes results → turns O(2n)O(2^n) into O(n)O(n)
Why pass list not list() to defaultdict?
defaultdict calls the factory itself when a key is missing
What is sys.argv[0]?
The script's own name
Why is deque.popleft() better than list.pop(0)?
O(1)O(1) vs O(n)O(n) (no shifting)
Recall Feynman: explain to a 12-year-old

Imagine a huge toolbox that comes free with your bike. One drawer (math) has number gadgets, one (random) is a dice cup, one (os) talks to your computer about files, one (collections) has magic boxes that count things by themselves, and one (itertools) is a machine that makes every possible combination of your stickers without you doing it by hand. You don't need to make these tools — they're already in the box. You just need to know which drawer to open.

Connections

  • Python Iterators and Generators — why itertools is lazy
  • Big-O Notation — why deque/lru_cache change complexity
  • Recursion and Memoizationlru_cache deep dive
  • Combinatorics — math.comb, permutations, combinations formulas
  • File Handling in Python — pairs with os and os.path
  • Decoratorsfunctools.wraps, lru_cache, partial

Concept Map

ships with Python

module drawer

module drawer

module drawer

module drawer

module drawer

module drawer

module drawer

module drawer

comb equals

seed gives

vs

shares clock with

Standard Library batteries included

math single-number math

random pseudo-randomness

os operating system

sys interpreter

datetime calendar

time clock and sleep

collections containers

itertools lazy looping

functools function tools

binomial coefficient

reproducible sequence

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, Python ki sabse badi taakat hai uska standard library — yeh ek aisa toolbox hai jo Python install karte hi free mein milta hai, kuch alag se download nahi karna padta. Soch lo har module ek drawer hai jisme ready-made, tested aur C-fast functions bhare pade hain. Aapko har function ratne ki zaroorat nahi — bas yeh yaad rakho ki kaun sa kaam kaunse drawer mein hai. Yahi 80/20 rule hai: thoda map yaad karo, bahut saara kaam ho jayega.

Quick tour: math single numbers ke liye (sqrt, comb, gcd), random dice/chance ke liye (aur seed se same sequence dobara aata hai — testing mein bahut kaam ka), os files aur folders ke liye, sys Python interpreter se baat karne ke liye (jaise sys.argv command-line arguments). datetime calendar dates ke liye aur time timing/sleep ke liye — yeh do alag cheezein hain, confuse mat hona.

collections mein superpowers wale containers hain: Counter cheezein automatic gin leta hai, defaultdict missing key par error nahi deta, aur deque dono taraf se O(1)O(1) mein add/remove karta hai. itertools lazy hota hai — matlab on-demand items banata hai, isliye combinations aur permutations jaise combinatorics problems isse mast solve hote hain. Aur functools mein lru_cache hai jo recursive fibonacci ko O(2n)O(2^n) se O(n)O(n) bana deta hai — magic memoization!

Yeh isliye matter karta hai kyunki interviews aur real projects mein log apna code chhota aur reliable rakhte hain — wheel dobara nahi banate. Jab aap sahi drawer khol lete ho, aapka code chhota, fast aur bug-free ho jaata hai.

Go deeper — visual, from zero

Test yourself — Python Intermediate

Connections