The tour
Short pages, one Python idea each, from values and types to async code and interview patterns. Run and change the code on every page.
Welcome
Basics
- Values and types
- Numbers
- Strings
- Slicing strings
- String methods
- Exercise: Palindromes
- f-strings
- Booleans and truthiness
- None, is and ==
- Names and assignment
- Comparisons
- Converting types
- Exercise: Reverse digits
- Exercise: Clean names
Flow control
- If, elif and else
- Conditional expressions
- For loops and range
- While loops
- break, continue and loop else
- Exercise: Loops and functions
- enumerate
- zip
- match statements
- Exercise: Runs
Functions
- Functions
- Return values
- Default arguments
- The mutable default trap
- Keyword arguments
- *args and **kwargs
- Exercise: Scoreboard
- Scope
- Functions are values
- Lambda
- Closures
- The late-binding loop surprise
- Exercise: Fibonacci closure
- Decorators
- Decorators with arguments
- functools: cache and partial
- Type hints
- Docstrings and doctests
- Exercise: Call counter
- Recursion
- Exercise: Flatten
Collections
- Lists
- Slicing lists
- List methods and their cost
- Sorting
- Tuples and unpacking
- Dicts
- Dict methods
- Counting and grouping
- Exercise: Word count
- Sets, and why speed depends on the container
- Set operations
- Comprehensions
- Nested data
- Mutability and aliasing
- Copying
- Hashability
- Exercise: Group anagrams
- Exercise: Two lists in O(n)
Iteration
- Iterables and iterators
- Generators and yield
- Generator expressions
- Infinite generators
- Exercise: Sliding windows
- Built-ins that take iterables
- itertools: chain, accumulate, pairwise, groupby
- itertools: combinations, permutations, product
- yield from, and send
- Exercise: Merge sorted streams
Classes
- Classes and __init__
- Methods and self
- __repr__ and __str__
- Exercise: Stringers
- Equality and hashing
- Ordering
- Dataclasses
- Class and instance attributes
- Properties
- Inheritance and super()
- Composition over inheritance
- Duck typing and protocols
- Abstract base classes
- Making a container
- Operator overloading
- classmethod and staticmethod
- __slots__
- Exercise: Vectors
- Exercise: LRU cache
Errors
- Reading a traceback
- try and except
- else and finally
- Raising exceptions
- Custom exceptions
- EAFP vs LBYL
- Context managers and with
- Writing a context manager
- Common exceptions
- Exercise: Errors
- Exercise: Suppress and log
Batteries included
- collections.deque
- heapq
- bisect
- namedtuple
- enum
- math and statistics
- datetime
- json
- re basics
- Groups and sub
- Text tools
- random with a seed
- Exercise: Top k with a heap
- Exercise: Parse the logs
Interview patterns
- Big-O in practice
- The cost of Python operations
- How to approach an interview problem
- Hashing for lookups: Two Sum
- Two pointers on sorted data
- Sliding window
- Prefix sums
- Binary search
- Stacks: Valid Brackets
- Monotonic stack: Daily Temperatures
- Queues and BFS: Shortest Path in a Grid
- Graphs and DFS: Number of Islands
- Topological sort: Course Order
- Intervals: Meeting Rooms
- Heaps: K Closest Points
- Recursion and backtracking: Subsets
- Dynamic programming: Coin Change
- Dynamic programming on two strings: LCS
- Strings: Anagrams
- Strings: Longest Palindromic Substring
- Where next
Testing and debugging
- Say your test cases out loud
- assert and plain test functions
- pytest in the real world
- Doctest traps
- Exercise: Lost Chunk
- Testing with fakes
- unittest.mock
- A method for debugging
- logging instead of print
- Exercise: Usernames
Concurrency
- Concurrency and parallelism
- Threads and concurrent.futures
- Processes for CPU-bound work
- async and await
- Running tasks together with gather
- Exercise: Fetch All
- Timeouts and cancellation
- Queues: producers and consumers
- Races and locks
- Exercise: Web Crawler