Introduction to Programming Languages


If you're going to be a physicist or an astronomer, you're going to have learn how to program. Even if experiment or observation is your thing, you'll still need to know at least one language. How are you going to manipulate those mounds of data files quickly and efficiently? In excel?

In theoretical physics, people who perform purely analytical work are scarce now a days since what can be done has usually already been done. There are those that do it, but even the most accomplished individuals find it hard to get funding. Thus, the need for computation, and consequently, programming knowledge is growing rapidly. So, what language should I learn? Before we get into that it's probably better to classify these languages. Programming languages can essentially be broken down into lower level, higher level and fancy packages (there's probably a more technical name than fancy, but I'm sticking with it for now).

I. Lower level languages (Fortran, C, etc.)

Lower level languages such as Fortran and C have been around for a long time and usually get a bad reputation. Newer is better, right? Not always! There are some decisive advantages to lower level languages. In particular, they're blazingly fast for numerical work. If your code needs to crunch some numbers, then look no further. The majority of large scale simulation codes have the bulk of their computational work done in one of these two languages. These languages are also very easy to learn. The disadvantage here is that codes can get really, really, long. If you decide upon Fortran, do yourself a favor and learn Fortran 95. It's faster, and contains useful features like namelists and modules. Certain things should be left in the 70' disco...and Fortran 77.

II. Higher level languages (Python, Perl, Ruby, C#, etc.)

There are too many higher level languages to list here and some are certainly more popular than others. A great benefit is that it takes considerably less lines of code to accomplish a task compared to a lower level language. What takes C about two full sheets of printed paper can probably be done in a few lines of Perl. Second, there are many built in functions and modules which you would have to design or steal for use in a lower level language (but they might not be optimized for speed). Third, most people use higher level languages as opposed to lower level languages so the community, and thus support, is much easier to find. If you need to read in and output data (I/O) from a lot files then it's going to be much easier to use one of these languages than doing it all in Fortran or C. The disadvantage here is that some of the intrinsic functions and syntax can be tedious and difficult to initially pick up.

III. Fancy packages (Matlab, Mathematica, Maple, IDL, IRAF, etc.)

Fancy packages are usually programs with their own languages. First and foremost, the support and libraries are massive. For example, IDL and IRAF are extremely common astronomy tools and as such have several built in routines specifically tuned for astronomical applications. There are also certain tasks that other programs just can't do, like analytically solve integrals like mathematica does. The downside here is that you generally have to pay for these programs. Although open source alternatives do exist, like Matlab's rival, Octave, and professors often have multiple licenses. Also, they're slower than molasses in a North Dakota winter.

Ok, so since all that's over with, which language should you learn? Well, that depends. If you're in experiment then you can probably get by with only learning one language. Most likely you won't be doing anything computationally expensive so lower level languages aren't necessary. From the perspective of your author, Python might be good place to start. It's becoming increasingly popular in the scientific community, it's fairly intuitive, it's flexible (things just work) and has a number of useful built in modules. If you're in theory, there's a good chance you'll need more than one language. Initially, a higher level language will do until your codes get more complex. Then it will be necessary to learn a lower level language. Of course you'll have to convert all your codes so there's something to be said for using both simultaneously to begin with. People will be haters on languages, but in the end it comes down to what works for you.

Fortran 95: