Learn Python with Talk Python's 270 hours of courses

Solving 10 different simulation problems with Python

Episode #423, published Mon, Jul 24, 2023, recorded Fri, Jun 2, 2023

Python is used for a wide variety of software projects. One area it's really gained a huge amount of momentum is in the computational space (including data science). On this episode we welcome back Allen Downey to dive into a particular slice of this space: simulation problems and Python in Physics and Engineering in general.

Errata: At 40:06 "basic contact rate" was meant to be "basic reproduction number", which is the technical term for what people just call R-zero.

Watch this episode on YouTube
Play on YouTube
Watch the live stream version

Episode Deep Dive

Guests Introduction and Background

Allen Downey is a longtime professor turned curriculum designer who spent nearly 20 years teaching at Olin College. He’s the author of multiple books on programming, data science, and simulation, including his new text on using Python for modeling physics and engineering problems. After Olin, Allen worked on data competitions at Driven Data and now develops high-level, interactive curriculum at Brilliant, focusing on animation and interactive simulations. He’s especially known for making complex topics—such as modeling and simulation—both accessible and practical for developers and students alike.

What to Know If You're New to Python

To really follow the modeling and simulation examples in this episode, it helps to have a basic grasp of:

  • Writing simple Python code in Jupyter notebooks or a text editor.
  • Core Python data structures (lists, dictionaries) and numerical operations.
  • Installing and importing libraries (like pandas and matplotlib) via pip or conda.

Key Points and Takeaways

  1. Why Modeling and Simulation Matter for Python and STEM Modeling real-world physical or engineering problems in Python lets developers prototype quickly, leverage libraries like NumPy or pandas, and handle data at scale. Allen highlights how Python’s ecosystem lowers the barrier to building simulations compared to traditional tools like MATLAB.
  2. Working with Real Data in Python The conversation covered how to bring external data—like population tables from Wikipedia—into pandas with functions such as read_html. Python’s flexibility means you can handle surprising or messy data sources with minimal overhead.
    • Links / Tools:
      • pandas read_html docs (https://pandas.pydata.org/docs/reference/api/pandas.read_html.html)
  3. Discrete vs. Continuous Systems in Python Allen explained that many physical or social systems (population dynamics, epidemics) are fundamentally discrete but well-modeled by continuous math (differential equations). Python’s ability to do step-by-step simulation (Euler’s method) or employ SciPy’s ODE solvers is essential for bridging both approaches.
    • Links / Tools:
      • Euler’s Method explanation (https://en.wikipedia.org/wiki/Euler_method)
      • SciPy (https://scipy.org)
  4. Exploring Epidemiology with the SIR Model The show dove into the SIR model (Susceptible, Infected, Recovered) for campus “freshman plague” scenarios. It demonstrated how Python can simulate disease spread or vaccination strategies, teaching how modeling extends beyond mere formulas.
    • Links / Tools:
      • SIR Model Overview (https://en.wikipedia.org/wiki/Compartmental_models_in_epidemiology)
  5. Newton’s Law of Cooling: The Coffee Cup Example A classic demonstration of applying differential equations in Python is predicting how coffee cools over time. It shows how Python can handle both the math (using iterative or ODE methods) and real-world factors like ambient temperature.
  6. Symbolic vs. Numeric Approaches: SimPy and SciPy The episode highlighted how you can solve equations numerically via loops or advanced libraries, but you can also explore symbolic math with SimPy for an exact formula. Each approach has trade-offs in speed, precision, and complexity.
    • Links / Tools:
      • Sympy (https://www.sympy.org)
      • SciPy ODE Solvers (https://docs.scipy.org/doc/scipy/reference/integrate.html)
  7. The Role of Modeling Decisions (Frictionless Planes vs. Realistic Systems) From ignoring friction in first-year physics to deciding whether to add or remove certain factors in a simulation, modeling always involves compromise. Python excels at rapid iteration, helping refine these assumptions.
    • Links / Tools:
      • Allen Downey’s book on Modeling and Simulation in Python (https://github.com/AllenDowney/ModSimPy) (Community-maintained link—check Allen’s GitHub for updates.)
  8. Bungee Dunk: Understanding Complex Forces Allen and Michael discussed the bungee jump “cookie dunk” stunt. A seemingly simple free-fall is complicated by cords unspooling and forces outpacing gravity. This underscores how real-world phenomena benefit from iterative Python simulations over simplistic math.
    • Links / Tools:
      • Veritasium Bungee Video (https://www.youtube.com/user/1veritasium)
  9. Teaching with Python and Building Textbook Material Allen’s textbook manifesto focuses on concise, practical books that students can read before class. Modeling examples in Python illustrate real data, ensuring learners see immediate, meaningful results rather than just theory.
  10. Editor Choices & Code Environment The show briefly touched on how writing code for simulations is often done in Jupyter Notebooks for immediate visualization. VS Code is also a popular choice for more robust editing, especially with Python plugins.

Interesting Quotes and Stories

  • “You can do all the theory you want, but eventually you will collide with data.” – Allen Downey, on why empirical data can shape or undermine purely theoretical models.
  • Freshman Plague: A humorous yet insightful example illustrating how easily diseases can spread in a dorm setting and how modeling such spread can teach fundamental epidemiology.
  • Coffee Cup’s Lost Heat = Lift to Mount Everest?: Allen highlighted how the sheer amount of heat energy could, in principle, raise the cup to an immense height—though it’s still not harnessable for real work, reflecting the difference between thermal and mechanical energy.

Key Definitions and Terms

  • SIR Model: A compartmental model in epidemiology (Susceptible, Infected, Recovered), used to understand how infections spread and eventually die out or reach equilibrium.
  • Euler’s Method: A straightforward numerical procedure for solving ordinary differential equations by approximating solutions step by step in small increments.
  • ODE Solver: Functions or algorithms, often found in SciPy, that solve Ordinary Differential Equations like population growth or heat transfer over continuous time.
  • SimPy: A Python library for symbolic mathematics, allowing exact math solutions rather than purely numeric approximations.
  • Logistic Growth: A population growth model that starts rapidly (like exponential) but slows as resources become limiting, leveling off at a carrying capacity.

Learning Resources

If you want to go deeper into data science or visualization in Python, here are a few targeted courses:

Overall Takeaway

Modeling and simulation open the door to deeper insights into everything from everyday coffee-cooling physics to large-scale population and epidemic studies. Python’s rich ecosystem of libraries—NumPy, pandas, SciPy, SimPy—makes it both powerful and accessible. Allen’s mission is to give learners and engineers the tools to move beyond mere theoretical math, grounding exploration in real data and iterative, hands-on computations. This episode underscores how Python helps transform abstract science into tangible, testable, and shareable simulations.

Links from the show

Allen’s web page: allendowney.com
Allen’s blog (Probably Overthinking It): allendowney.com/blog
Allen on Twitter: @allendowney
Allen on Mastodon: @allendowney@fosstodon.org
Modeling and Simulation in Python book: allendowney.github.io
Programming as a Way of Thinking: blogs.scientificamerican.com
Think Python book: greenteapress.com
Think OS book: greenteapress.com
Pint package: pint.readthedocs.io
Free version of the book and Jupyter notebooks: allendowney.github.io
Published version: nostarch.com
Elm programming language: elm-lang.org
SymPy examples: docs.sympy.org
Guinness World Record won for bungee 'dunk' into cup of tea: youtube.com
Watch this episode on YouTube: youtube.com
Episode transcripts: talkpython.fm

--- Stay in touch with us ---
Subscribe to Talk Python on YouTube: youtube.com
Talk Python on Bluesky: @talkpython.fm at bsky.app
Talk Python on Mastodon: talkpython
Michael on Bluesky: @mkennedy.codes at bsky.app
Michael on Mastodon: mkennedy

Talk Python's Mastodon Michael Kennedy's Mastodon