Scientific Python for Both Expert and Novice Programmers
by Alex Small
When learning a new language, I want a book that will walk through simplified versions of things that I'd actually do. I've written lots of Matlab code, but I wanted to learn Python to teach computational physics in an open source environment. While going through this book, my usual thought was, “Great, now how would I go further and ..." only to have my question answered in the next section. Along the way, I even managed to learn a few things about programming.
The book goes through Python in particular, and programming in general, via tasks that scientists will likely perform. It starts with the fundamentals, showing the reader how to evaluate and print the value of a simple 1D kinematic formula, even discussing the command line and editor for true novices. The rest of the chapter further develops formulas, simple math, (such as temperature conversions and trajectory calculations), and the output of strings and numbers. This chapter would be useful for a novice getting comfortable with scientific computation, yet it would not bore an experienced programmer who wants to skim for Python syntax. Each chapter includes numerous exercises (18 in chapter 1, but typically 30 or more per chapter) involving calculations, and the exercises and text often reference each other. This is a promising start!
Chapter 2 delves into the meat of scientific programming: loops and lists. Chapter 3 covers functions and logic. I appreciated the versatility of Python's syntax in this chapter and came away realizing how many different — and readable — ways there are to do the same thing. In chapter 4, the author focuses on input data, errors, and modules (function libraries, essentially). He introduces arrays in chapter 5, along with plotting. I appreciated his decision to stick close to Matlab conventions for plotting syntax, along with his emphasis on good habits, such as vectorizing code.
With just these chapters and a few elements from subsequent chapters, a beginner could complete programming projects in other courses. An experienced programmer would even be comfortable with Python for many core tasks of scientific computing. The remaining chapters, though, are well worth studying.
For instance, chapter 6's discussion of files, strings, and dictionaries shows more of Python's versatility. Chapter 7's introduction to classes opens with excellent motivation—how can you pass functions to programs for doing calculus if different functions have different numbers of parameters? It then develops this sophisticated topic by example.
Chapter 8, my favorite, focuses on random numbers and simple games. Students are invited to solve probability problems via Monte Carlo simulations rather than formulas with factorials. If we can condition students to try a Monte Carlo simulation to help with a hard question in probability, then we've imparted a valuable habit (even if we do annoy purists).
Chapter 9's focus on object-oriented programming introduces the concept of class hierarchy through three examples that science students should care about: writing a general program to compute numerical derivatives, writing a general program to do numerical integration, and making and plotting geometrical shapes.
Hans Petter Langtangen has written the book in a lucid, thoughtful manner, and most of the examples were things that I would want to do. The appendices complement the rich variety of examples in the chapters to make it a self-contained book for a first course on scientific computing. In addition to excellent homework exercises, each chapter ends with a challenging summarizing example.
I might not recommend it for students who have never programmed before. Heavy use of relevant examples is certainly important, but learning to program means learning to work in a structured, step-by-step manner with tools that obey strict rules. There aren't many diagrams, charts, or tables to illustrate concepts such as loops, lists, logic, and arrays. Even strong students might appreciate some tables or Venn diagrams to summarize, say, distinctions between lists, tuples, and arrays. More step-by-step lists or pseudocode prior to developing sample code would also help. I also wish that the index was a bit more extensive, perhaps with a separate index of all of the Python commands introduced in the book.
Still, this is an excellent book for the experienced programmer who wants to learn a new language and become a better programmer. It contains valuable information for students new to scientific computing and would be the perfect bridge between an introduction to programming and an advanced course on numerical methods or computational science.
All of the code in the book is available (along with the author's lecture slides) on the author's website (http://vefur. simula.no/intro-programming). The website also has additional instructions for downloading and installing Python and a suitable editor.
Alex Small is an assistant professor in the Department of Physics and Astronomy at California State Polytechnic University, Pomona. Contact him at firstname.lastname@example.org. This review is excerpted from the original review in Computational Science and Engineering (March/April 2012, pp. 6–7).