Module 11: Advanced Python
Advanced 19 chapters
136. Iterators
Learn how Python's for loop really works. Understand iterables, iterators, the iter() and next() functions, and how to build your own iterator class.
137. Generators
Learn to write generator functions and generator expressions that produce values lazily, save memory and make pipelines of data processing.
138. yield Keyword
Learn how the yield keyword turns a normal function into a generator that produces values one at a time, remembers its place between calls, and saves memory.
139. Decorators
Learn what decorators are, how they wrap a function to add behaviour, how to write ones that take arguments, and how functools.wraps keeps function details intact.
140. Closures
Understand closures: inner functions that remember variables from the function that created them, how nonlocal lets them change those variables, and the late binding trap in loops.
141. Context Managers
Learn how context managers and the with statement set things up and clean them up safely, how to write your own with a class or with contextlib, and how they handle errors.
142. Regular Expressions
Learn to search, match, extract and replace text with patterns using Python's re module, from simple character classes to groups and substitutions.
143. Enumerate
Use enumerate() to loop over items together with their positions, choose your own starting number, and replace clumsy index counters in your loops.
144. Zip
Learn how zip() pairs up items from several iterables, how it stops at the shortest one, and how to use it for parallel loops, dictionaries and transposing data.
145. Unpacking
Learn sequence unpacking, starred names, swapping values, nested unpacking and the * and ** operators for function calls and for building lists and dictionaries.
146. Comprehensions Advanced
Go beyond simple list comprehensions: multiple for clauses, filters versus conditional expressions, nested comprehensions, dict and set comprehensions, generator expressions and the walrus operator.
147. Type Hints
Learn how to add type hints to functions and variables, describe collections, unions and optional values, use Callable and TypeVar, and understand that Python does not enforce hints at run time.
148. Dataclasses
Use the dataclasses module to build data-holding classes with little code: fields, defaults, default_factory, frozen and ordered options, __post_init__, asdict, replace and inheritance.
149. Collections Module
Learn the collections module: Counter, defaultdict, deque, namedtuple, OrderedDict and ChainMap. These containers solve common problems more cleanly than plain lists and dicts.
150. itertools
Use the itertools module to build fast, memory-friendly loops: count, cycle, chain, islice, groupby, product, permutations, combinations, accumulate and more.
151. functools
Learn functools: reduce, partial, lru_cache, cache, wraps, total_ordering, cmp_to_key and singledispatch. These tools help you build and reuse functions in a clean way.
152. copy and deepcopy
Understand assignment, shallow copy and deep copy. Learn when copy.copy is enough, when you need copy.deepcopy, and how to customise copying for your own classes.
153. Memory Management
Learn how Python stores objects in memory, how names point to objects, and how reference counting frees memory that is no longer needed.
154. Garbage Collection
Understand why reference counting is not enough, how Python's cyclic garbage collector finds unreachable objects, and how to control it with the gc module.