Back to all DSA books

Data structures
and algorithms
in Python.

Python for DSA, complete edition
Python for DSA book cover

Learn the algorithms in the language that reads like pseudocode, and see what that pseudocode costs.

Python makes an algorithm look short, which can hide what it costs. This book teaches the classic data structures and algorithms with plain Python 3.12 or newer and only the standard library: no NumPy, no notebooks, no packages. Every chapter has a complete program you can run with its exact output, the cost in time and memory, and exercises with solutions, plus the Python details that change an answer: list versus deque, dict ordering, shared references, the recursion limit and float sums.

PDF, 62 pages, 1.1 MB. Free with Google sign-in.

Written for Python, not translated into it.

What’s inside.

Each chapter has a complete program to run, the time and memory cost, and exercises.

  1. Install Python and prepare your workspace

    Install Python 3.12 or newer, open a terminal and an editor, and run your first program.

  2. Python foundations for DSA

    The reliable core: names, numbers, functions, loops, lists, ranges and comparisons.

  3. Lists, dicts and sets

    The everyday containers, what each operation costs, and how to choose among them.

  4. Strings and Unicode

    Strings are immutable sequences of code points; count, compare and build text correctly.

  5. Classes, references and copies

    Build nodes from classes, and reason about names that share one mutable object.

  6. Recursion

    Base cases, smaller problems, and the recursion limit every Python call stack has.

  7. Stacks and queues

    A list for newest-first, collections.deque for oldest-first, and why list.pop(0) is slow.

    See stacks and queues visualized
  8. Linked lists

    Nodes joined by references, generators to walk them, and when a list is worth it.

    See linked lists visualized
  9. Hash tables and Python dicts

    How a key finds its slot, the hash and equality contract, and Counter and defaultdict.

    See hash tables visualized
  10. Trees and binary search trees

    Traverse a tree, insert and search a BST without recursion, and see why sorted input is slow.

    See trees visualized
  11. Heaps and priority queues

    Write a min-heap, then use heapq with tie-breakers so equal priorities never crash.

    See heaps visualized
  12. Searching and sorting

    Binary search with bisect, a stable merge sort, and sorted() with key functions.

  13. Two pointers, sliding windows and prefix sums

    Stop rescanning the same elements, and keep float totals from drifting.

  14. Divide and conquer and backtracking

    Combine answers to halves, or explore choices and undo each one cleanly.

  15. Graph traversal

    Represent a graph with a dict of lists, run BFS with deque and DFS with a stack, and recover a path.

    See graph traversal visualized
  16. Greedy algorithms

    When the locally best choice is globally best, and how to test a greedy rule.

  17. Dynamic programming

    Define a state and a recurrence, then compare a table with functools.cache.

  18. Mixed practice and projects

    Choose a technique without a chapter title to help, and build a dependency planner.

Prefer another language? Go for DSA, JavaScript for DSA, Java for DSA or C++ for DSA.