19 lessons3 h

Python Programming

Python is the language security people reach for when they need something done quickly. Exploit scripts, CTF solvers, file parsers, and one-off tools to decode a blob of Base64 are almost always Python, because it takes a few lines to do what C takes a page for. Every exploit in Binary Exploitation is a Python script, and the Pwntools Guide assumes you can already write one.

This course teaches Python as a working tool. It starts with the syntax and built-in data structures, moves through functions, classes and files, and finishes with the standard-library modules and packages that turn programming challenges from an afternoon into a few minutes. If you already know another language, most of the early lessons will go quickly; the value is in learning what Python gives you for free.

Course Layout

  1. Getting Started. How Python runs code, the indentation rules, variables and types, and control flow.
  2. Data Structures. The built-in types you will use in every script: strings, lists, tuples and sets, dictionaries, and the bytes that security work lives in.
  3. Functions & Modules. Writing reusable functions and splitting code into modules.
  4. Objects & Errors. Defining your own types with classes, and handling what goes wrong with exceptions.
  5. Files & Text. Reading and writing files, and pulling structure out of text with regular expressions.
  6. Tooling. Installing third-party code into virtual environments, and checking your own with pytest.
  7. Problem-Solving Toolkit. The libraries that make challenges short: functools and itertools, specialized containers, NumPy for grids and matrices, and NetworkX for graph problems.

Work through the first five sections in order. Tooling and the Problem-Solving Toolkit stand on their own, so take them as you need them.

Keep a REPL open
Run python3 in a terminal next to the lesson and try each example as you read it. Python's interactive prompt is the fastest way to check what a method returns or what an error looks like.

Toolchain

All you need is Python 3.10 or newer, which most Linux distributions ship as python3. Check yours:

python3 --versionPython 3.12.7

The later lessons use third-party packages such as NumPy and NetworkX. Packages & Environments shows how to install them with uv into a project's own environment, instead of into the system Python.

Python 2 is dead.
Older tutorials and exploit scripts are often written for Python 2, where print is a statement and strings are bytes. Everything here is Python 3. If a snippet from the internet fails with a SyntaxError on print, that is why.