Skip to main content

Posts

Welcome!

This blog accompanies A Student’s Guide to Python for Physical Modeling by Jesse M. Kinder and Philip Nelson.  The book is now in its second edition ! A Student’s Guide provides an introduction to the Python computer language and a few libraries (NumPy, SciPy, and PyPlot) that will enable students to get started in physical modeling. Some of the topics covered include the following: basic Python programming importing and exporting data numerical arrays 2D and 3D plotting Monte Carlo simulations numerical integration solving ordinary differential equations symbolic mathematics animation image processing Python classes version control with Git You can have a look at the Table of Contents . On this Web site, you will find data sets, code samples, errata, additional resources, and extended discussions of the topics introduced in the book. Enjoy! Plots created with NumPy and PyPlot.
Recent posts

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

The Updated Edition Is Here!

Happy New Year! We are pleased to start off 2018 by announcing that the updated edition of A Student's Guide to Python for Physical Modeling is now available! The hard copy edition is shipping and is available from Princeton University Press . You can expedite your order by calling the distributor directly at (800) 343-4499. (The ISBN is 9780691180571.) The Kindle edition is currently available for pre-order on Amazon . The book will be available from other distributors, along with e-book editions, in the near future.

A Few Updates

We have been busy working on A Student’s Guide to Python for Physical Modeling this summer. We are preparing an updated edition for Winter of 2018. We wanted to take this opportunity to alert readers to a few items that have changed since the publication of the first edition, back in 2015. Python 3.6 The “latest version” of any software package is a moving target, and putting a version number in print ensures that it will be outdated at some point in the future. In the first edition, we used Python 3.4. The latest stable release of Python is version 3.6, and Python 3.7 is under development. Happily, all of the code samples in the book run with Python 3.6. One change that came with Python 3.5 may be of interest to scientific programmers. Python introduced a new operator for matrix multiplication: the “ @ ” symbol. NumPy recognizes this operator, and it can be used as shorthand for the np.dot() function. This may allow you to write simpler, more intuitive code, which usually lead

Make Your Own GUI with Python

Suppose you have written a Python script that carries out a simulation based on a physical model and creates a nice plot of the results. Now you wish to explore the model and run the calculation on many different sets of input parameters. What is the best way to proceed? You could write a script that runs the same calculation on a list of inputs that you select ahead of time: inputs = [(x0, y0, z0), (x1, y1, z1), (x2, y2, z2)] for I in inputs: # Insert simulation code here. You could place your script inside a function and call the function repeatedly from the IPython command line with different inputs: def run_simulation(x, y, z): # Insert simulation code here. Both methods work, but neither is ideal. If you use a script with an input list, you have to select the inputs ahead of time. If you find an interesting region of parameter space, you have to modify the script and run it again. If you embed your simulation within a function, you have to do a lot of typing at

Jupyter Notebooks

Happy New Year! Let’s start off 2016 by learning something new: Jupyter Notebooks , formerly known as IPython Notebooks. A Jupyter notebook is an interactive document that incorporates text, math, graphics, and code. It can be viewed in a Web browser. Unlike most documents, however, you can modify and execute the code inside the document. In this sense, a Jupyter notebook is similar to a session in Mathematica or Maple. The difference is that the interpreter running behind the scenes is not Mathematica or Maple. Jupyter notebooks were designed to run Ju lia, Pyt hon, and R , but they support over 40 languages at present. The best way to learn about Jupyter notebooks is to take a look at one. This notebook is a simple example from a greenhorn, but you can find many more on the Web. Introduction.ipynb Click on the link to view the notebook. You can download the notebook using the Save icon at the upper right corner of the notebook Web page. You can then open th

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

Speeding Up Python — Part 2: Optimization

The goal of this post and its predecessor is to provide some tools and tips for improving the performance of Python programs. In the previous post , we examined profiling tools — sophisticated stopwatches for timing programs as they execute. In this post, we will use these tools to demonstrate some general principles that make Python programs run faster. Remember: If your program already runs fast enough, you do not need to worry about profiling and optimization. Faster is not always better, especially if you end up with code that is difficult to read, modify, or maintain. Overview We can summarize our principles for optimizing performance as follows: Debug first. Never attempt to optimize a program that does not work. Focus on bottlenecks. Find out what takes the most time, and work on that. Look for algorithmic improvements. A different theoretical approach might speed up your program by orders of magnitude. Use library functions. The routines in NumPy, SciPy,

Speeding Up Python — Part 1: Profiling

When people argue about programming languages, a common critique of Python is, “It’s slow.” This is occasionally followed by, “A program written in C will run a thousand times faster.” Such generalization carry little weight. Python is often fast enough, and a well-written Python program can run significantly faster than a poorly-written C program. Plus, Moore’s Law implies that computers today are over a thousand times faster than those of 15 years ago: You can do with Python today what was only possible with a highly optimized, compiled program in 2000. It is also important to consider development time. Suppose a C program takes a week to write and debug and 1 minute to run, and an equivalent Python program takes a day to write and debug and 1000 minutes (about a day) to run. The “slow” Python program will finish running five days earlier than the “fast” C program! If you already know Python and don’t know any C, then the time difference will be even greater. In short, you nee

Lists, Comprehensions, and Generators

In A Student’s Guide to Python for Physical Modeling , we emphasized NumPy arrays and paid less attention to Python lists. The reason is simple: In most scientific computing applications, NumPy arrays store data more efficiently and speed up mathematical calculations, sometimes a thousandfold. However, there are some applications where a Python list is the better choice. There are also times when the choice between a list and an array has little or no effect on performance. In such cases a list can make your code easier to read and understand, and that is always a good thing. In this post, I will describe Python lists and explain a special Python construct for creating lists called a list comprehension . I will also describe a similar construct called a generator expression . Lists A list is an ordered collection of items. You may have made a “To Do” list this morning or a grocery list for a recent trip to the store. In computer science, a list is a data structure that sup