Fortran vs. Python
Posted: July 9th, 2009 | Author: ryan | Filed under: Fortran, Python | Tags: Fortran, Python, xkcd | View CommentsI want to write more. I’d really like to write for Python Magazine, but I don’t have any ideas for a topic yet. Matt opened a can of worms by asking me why he should bother to learn Python as a Fortran alternative.
Both Matt and myself come from a Fortran background. At ISU, all physics majors were required to take a formal class in Fortran programming. Note only that, the vast majority of physics courses (all sophomore level courses and higher, except education courses) assigned computational homework sets or projects. It wasn’t always required to write your code in Fortran but pretty much everyone did. I always preferred coding in Fortran over C/C++ and I never really got into other languages as deeply as I did with those. I’ve even read many pages of the Fortran specification. Yeah. I used to really like Fortran, and defended it fiercely. I still do when I encounter someone who claims it is dead or useless. Bah! The truth is quite the contrary.
That said, I don’t write anything in Fortran. Not since Python. Now, getting back to Matt’s question, I don’t have a itemized list of reasons. Put simply, Python is simple. Consider these short examples.
Python
1 | print "Hello from Python" |
Fortran
1 2 3 | program hello print *, "Hello from Fortran" end program hello |
To say Hello from Fortran takes three lines, opposed to just one in Python. More importantly, the Python code just makes sense. It’s almost so simple that one could guess the syntax. This is true for a lot of things in Python.
And what the heck is going on with the asterisk? It looks like the print statement is to print two things: something that goes by the name ‘*’ and a string literal. Could it mean print everything, and the string? No. The print statement takes as its first argument the output device, in this case the default stdout. The integer 6 would work as well. To print to stderr or read from stdin you would use 0 and 5 respectively. The POSIX file descriptors for stdin, stdout and stderr are 0, 1 and 2 respectively.
To drive this home and to poke fun at myself I’ll admit that couldn’t remember how to use the fortran print statement. There were several unsuccessful attempts at compiling my three-liner before I turned to Google and was reminded to include the output device.
I need to sit down and think about a more detailed answer to Matt’s question. But until then check out this XKCD comic titled Python.




