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.
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!
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
- 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.