Cheatsheets

Python cheatsheet

venv, modules, virtualenv, and common standard-library calls.

Environment

Command / topicWhat it does
python3 -m venv .venvCreate a virtual environment
source .venv/bin/activateActivate on Linux/macOS
pip install -r requirements.txtInstall dependencies
python -m pytestRun tests as a module

Language essentials

Command / topicWhat it does
from pathlib import PathPreferred path handling
with open(path) as f:Safe file open/close
list(dict.fromkeys(items))Deduplicate while preserving order
{k: v for k, v in d.items() if v}Dict comprehension filter

Debugging

Command / topicWhat it does
python -m pdb script.pyStart the debugger
raise SystemExit("msg")Exit with a clear error
import traceback; traceback.print_exc()Print a caught stack

← All cheatsheets · Courses