Why Every Physicist Should Know Python

Regardless of your specific discipline in physics, a computer is almost certainly an integral part of your research. If you deal with data sets that need to be processed, you almost certainly have (1) a series of programs or scripts that you use for processing or post-processing of data or (2) a detailed and tedious routine to push data around in columns in a spreadsheet software like QTI Plot, Excel, Origin or Gnumeric. Either way, Python can save you time and effort.

What follows is a list of reasons (and maybe even suggestions) for why you should know Python.

  • Python is executed, not compiled. This immediately allows for rapid development, removing overhead of doing certain tasks manually. Once you become familiar with Python, you will tend to write codes to perform tasks that you perhaps would have otherwise done by hand.

    Executed languages have a reputation for slow execution. Few would dare do large Fourier transforms or matrix inversions in an executed language, but Python remains viable. The overhead for memory operations is low and thanks to NumPy (more on Numpy and SciPy in a moment) array slicing is also very efficient. In addition, Python integrates very well with binary codes. So use a well proven binary code for the heavy lifting but use Python everywhere in between.

  • A screenshot of SciPy at work, finding the maxima of Bessel functions.

    Python is a very mature language and almost anything fundamental you might want to do is implemented in a library or module. For numerical work, check out NumPy and SciPy. NumPy introduces a powerful N-dimensional array object and tools for integrating C/C++ and Fortran code. It also has built-in linear algebra, Fourier transform, and random number capabilities. SciPy builds upon NumPy adding even more useful tools such as numerical integration of functions, discrete sets, ODEs and PDEs and optimization tools. From the SciPy website:

    The SciPy library depends on NumPy, which provides convenient and fast N-dimensional array manipulation. The SciPy library is built to work with NumPy arrays, and provides many user-friendly and efficient numerical routines such as routines for numerical integration and optimization. Together, they run on all popular operating systems, are quick to install, and are free of charge. NumPy and SciPy are easy to use, but powerful enough to be depended upon by some of the world’s leading scientists and engineers. If you need to manipulate numbers on a computer and display or publish the results, give SciPy a try!

  • Matplotlib for visualization, 3D, VTK, and integration with GNUPLOT. Visualisation is a key part of any scientific workflow. I personally enjoy GNUPLOT, so I tend to use gnuplot.py for a good part of my plotting needs. You should also investigate matplotlib, an incredibly powerful plotting library which is very user friendly and integrates seamlessly with NumPy and SciPy. For good integration with LaTeX, one can also look at PyX. For 3D visualization, don’t forget about VTK, the Visualization Tool Kit. VTK has Python bindings. If you’re particularly ambitious, Python even has OpenGL bindings. Whatever your visualization needs are, Python can handle it.
  • Once you’ve spent some time writing Python code, you’ll likely come to apprciate how clean and readable the code is. This can save you time and effort later when (A) you look back at your own code with nary a notion of how it worked in the first place and (B) when you want to share your code with others.
  • And you are likely to share code with others, since Python code is very portable. Python is cross platform, so you don’t need to worry about sending code between colleagues: Windows, Linux and BSD users alike. If you’re a Windows user, it means you can develop code on your Windows machine and simply upload the code to a cluster when you’re done and run it without revisions or recompiling.

I hope this has convinced you to try Python. The learning curve is rather gentle, and I’ll be writing tutorials in the coming weeks for performing standard tasks using Python. If I’ve convinced you to learn more, I’ll conclude with a few links to get you started.

Be Sociable, Share!


  1. Durand says:

    Interesting post, shame that my computing lecturer disagrees. He’d apparently have us learning fortran. And I thought c was bad…

    • Tim Teatro says:

      Ha ha, Thanks for the comment. I’ve used both C and Fortran for a lot of years, and still do. It’s not necessarily Python vs. the others. They all have their uses. If you program takes you longer to write than to execute (which is most cases) than Python is the way to go. But if you have a calculation that runs for days, weeks, etc, then there’s no question that you want C or Fortran, because that’s where you’ll save the time.

      One neat thing about Python, however, is that it can call C and Fortran binary codes. For example, if you use the integrate.quad() function from SciPy to compute a definite integral, then it actually calls the Fortran library QUADPACK. So, in my opinion, Python is useful for a very wide range of tasks.

  2. Martin Mason says:

    Kudos. Been teaching computational physics in python since 98. I know that it has made me far more productive and hopefully been good for students. There is also a huge effort to get computation in the freshman/sophomore physics sequence. Bruce Sherwood and Ruth Chabay have a freshman text “Matter and Interactions” that integrates significant computational problems. There is also a nice easy visualization library (Vpython) that wraps openGL developed for physics students. Even the engineers can benefit from learning matplotlib and numpy instead of matlab. Imagine a world where every college freshman had to take a computational physics course in python along with english and algebra.

  3. Pingback: Neden her Fizikçi Python Bilmeli

  4. Pingback: Changing My Priorities (Again :P ). | Shifting Paradigms

  5. Pingback: Quora

Leave a Reply