A py5 (Python) port of The Nature of Code by Daniel Shiffman.
This is a derivative work of The Nature of Code by Daniel Shiffman, published by No Starch Press® Inc. The original book uses p5.js (JavaScript), and this repository contains a port to py5 (Python/Processing).
- Title: The Nature of Code
- Author: Daniel Shiffman
- Publisher: No Starch Press® Inc.
- Website: https://natureofcode.com
- License: CC BY-NC-SA 4.0
This port converts all code examples from p5.js (JavaScript) to py5 (Python), adapting explanations where necessary to reflect Python-specific concepts and syntax. Where the original relies on a browser-specific library, an idiomatic Python equivalent is used: Matter.js / Toxiclibs.js → pymunk (Chapter 6) and ml5.js → scikit-learn / hand-rolled NumPy networks (Chapters 10–11).
All twelve chapters are available as Jupyter notebooks in notebooks/:
| Notebook | Chapter |
|---|---|
00_introduction.ipynb |
Front matter — Introduction, Dedication & Acknowledgments |
00_randomness.ipynb |
Chapter 0 — Randomness & Perlin Noise |
01_vectors.ipynb |
1. Vectors |
02_forces.ipynb |
2. Forces |
03_oscillation.ipynb |
3. Oscillation |
04_particles.ipynb |
4. Particle Systems |
05_steering.ipynb |
5. Autonomous Agents |
06_libraries.ipynb |
6. Physics Libraries (pymunk) |
07_ca.ipynb |
7. Cellular Automata |
08_fractals.ipynb |
8. Fractals |
09_ga.ipynb |
9. Genetic Algorithms |
10_nn.ipynb |
10. Neural Networks (scikit-learn) |
11_nn_ga.ipynb |
11. Neuroevolution |
Copyright © 2024 by Daniel Shiffman.
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) license.
Attribution: When using this work, you must credit the author as follows: "Daniel Shiffman, published by No Starch Press® Inc." and indicate that this is a py5 port of the original work.
See LICENSE for full license details, and CREDITS.md for the full copyright notice and per-chapter image credits.
See RESOURCES.md for the original book's list of code-example ports to other languages and its further-reading bibliography (books, papers, and articles).
- Python 3.11 or higher
- uv package manager
- Git
git clone git@github.com:hollygrimm/py5-noc-book-2.git
cd py5-noc-book-2uv venvsource .venv/bin/activateuv syncpy5 runs on Processing/Java and needs a Java 17 JDK. If you don't already have one, the bundled install-jdk package can fetch one into ~/.jdk/:
uv run python -c "import jdk; print(jdk.install('17'))"This prints the install path (something like ~/.jdk/jdk-17.0.17+10). Point JAVA_HOME at it:
export JAVA_HOME="$HOME/.jdk/jdk-17.0.17+10" # use the path printed aboveIf you already have a JDK 17, set JAVA_HOME to that instead — e.g. $(/usr/libexec/java_home -v 17) on macOS, or a /usr/lib/jvm/java-17-* path on Linux. py5 needs Java 17 specifically; newer JDKs (e.g. 21) may not work.
Note: Add the export line to your ~/.bashrc or ~/.zshrc to make it permanent, then source it.
jupyter labJupyterLab will open in your browser at http://localhost:8888
.
├── notebooks/ # Jupyter notebooks (chapters), solutions/, and images/
├── pyproject.toml # Project dependencies
├── CREDITS.md # Copyright, license, and image credits
├── RESOURCES.md # Ports and further reading
└── README.md # This file
All notebooks use the standard Python kernel from the virtual environment. Import py5 at the top of your notebook:
import py5Then use py5 functions with the py5. prefix (e.g., py5.size(), py5.background(), py5.run_sketch()).
Key packages included:
py5[jupyter]- Processing for Pythonjupyterlab- Interactive notebook environmentnumpy- Numerical computingmatplotlib- Plotting and visualizationpymunk- 2D physics engine (Chapter 6)scikit-learn- Machine learning (Chapter 10)opencv-python- Computer visionshapely- Geometric operationstrimesh- 3D geometry processing
See pyproject.toml for the complete list.
The project uses uv for dependency management. To add new dependencies:
uv add <package-name>Before publishing, and after any dependency bump, check the locked dependency tree for known CVEs:
uv run --with pip-audit pip-auditTo fix a flagged package without disturbing the pinned, run-reviewed versions (py5, numpy, matplotlib, pymunk, scikit-learn, pillow), upgrade just that package:
uv lock --upgrade-package <name>
uv syncIf you get Java-related errors, ensure JAVA_HOME is set:
echo $JAVA_HOME