Skip to main content

The Second Edition

We are pleased to announce the second edition of A Student’s Guide to Python for Physical Modeling!

The book will be available in early August, in time for fall classes.

We have updated the book to reflect changes in the language, and we have added new material on methods that have become more prominent in scientific programming in recent years — such as data science and version control. The second edition is about 70 pages longer than the updated edition. However, we maintained the concise, hands-on approach of earlier editions and worked diligently to make the text clear and easy to follow.

New Material

Our first task was to bring everything up to date so that readers can build a working Python environment from scratch. Everything from installation instructions to screenshots have been brought up to date.

We also developed and tested all code with the latest version of Python: Python 3.9. We made a deliberate effort to use simple constructs and standard libraries, so the code samples run with earlier versions of Python, too.

(The “latest version” of an open-source software package is a quickly moving target. With Python’s new release schedule, any version number in print is guaranteed to be out of date in less than a year! If you want to explore the latest changes to the Python language, you can read about what’s new..)

The bulk of the new material in the second edition comes in two new chapters.

Chapter 10 — Advanced Techniques introduces several Python programming tools that are advanced for someone new to computer programming, but are also indispensable to scientific computing:

  • Dictionaries and generators
  • Tools for data science and machine learning: pandas and scikit-learn (a very brief introduction)
  • Symbolic computing with SymPy (greatly expanded coverage from earlier editions, with a guided investigation of the first passage problem)
  • Python classes (a tutorial in writing classes to study random walks in 1, 2, or 3 dimensions and on a variety of lattices)

Appendix B — Command Line Tools introduces readers to working at the command line: a UNIX/Linux shell, a macOS terminal, or the Anaconda Prompt in Windows. It also introduces Git for version control. (You can now obtain the entire repository of code samples from GitHub!)

  • Introduction to navigating the file system from the command line
  • Creating, moving, and deleting files and directories
  • Using a text editor from the command line
  • Version control with Git

There are many other updates and additions throughout the rest of the text, too.

  • Raw strings (convenient for LaTeX)
  • Comparison of plt.subplot and plt.subplots
  • Heat maps with pcolormesh
  • Complex root finding with fsolve
  • Solving ODE’s with solve_ivp

We have also added several new code samples and “Your Turn” exercises — with solutions — so readers can try out the new techniques.

Thanks to the many readers who provided helpful comments and suggestions on earlier editions of the book. We look forward to your feedback on the second edition!

Comments

Popular posts from this blog

Paths in Python

How do you get your Python interpreter to find modules that are not located in your current working directory? The answer is … you tell it where to look. When you type something like from my_module import my_function Python searches a collection of directories (i.e., folders) on your computer. If the directory containing <my_module.py> is not in this collection, you will receive an ImportError . This can be frustrating if you know the file exists, and even more so if you know where it exists. In this post, we will take a brief look at how to add paths to the collection of directories searched by Python. Paths A path is essentially a set of directions to a file: /Users/username/modules/my_module.py [Mac OS X, Linux] C:\modules\my_module.py [Windows] It tells your operating system how to navigate from a fixed starting point — the “root directory” / in Unix-based systems, or C:\ in Windows — through a collection of folders to the de

Illuminating Surface Plots

Matplotlib provides functions for visualizing three-dimensional data sets. One useful tool is a surface plot. A surface plot is a two-dimensional projection of a three-dimensional object. Much like a sketch artist, Python uses techniques like perspective and shading to give the illusion of a three-dimensional object in space. In this post, I describe how you can control the lighting of a surface plot. Surface Plots First, let’s look at some of the options available with the default three-dimensional plotting tools. This script will create a surface plot of a Bessel function. Its ripples will emphasize the effects of lighting later. import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # Import 3D plotting tools. from scipy.special import jn # Import Bessel function. # Define grid of points. points = np.linspace(-10, 10, 51) X, Y = np.meshgrid(points, points) R = np.sqrt(X**2 + Y**2) Z = jn(0,R) # Create 3D surface plo

Raising a Figure Window to the Foreground

This post describes a utility function that will raise a plot window to the foreground of your screen. The function will only work with the Qt graphics backend, so I will start with a brief overview of graphics backends. If you just want to use the function as a black box, you can do the following: Set the Graphics Backend to “Qt” in the Spyder preferences menu. Copy this function into your working directory. Graphics Backends You may have have written a script to produce precisely the data you need, but a lot of processing is required to transform these numbers into a figure. You need to create a plot window, draw a figure inside of it, and manage all of the attributes that control a figure’s appearance: title, axis labels, line widths, colors, tick marks, etc. All of this happens in the background when you type a command like plt.plot(x,y) . A graphics backend is the software that Python uses to physically draw a figure on your computer screen. If you have already import