A Meteorologist’s Road to Python (Part 2)

Editor’s note: In this post, Associate Professor of Meteorology Alex DeCaria of Millersville University continues sharing about his experience in learning Python (see Part 1 for the back story), focusing on specific features of Python that he loves, as well as one caution about using Python 3 vs. Python 2.7. If you have comments, please feel free to email him at [email protected]

As I said in Part 1, Python is modern and easy to learn. The syntax is clean, and makes sense! Here are some favorite things that I love about Python:

1) No begin/end statements! Blocks of code are delimited purely by indentation. This is pure genius in my book!

2) The new string formatting paradigm (used in later versions of Python). This is another very cool and easy to use feature which I make use of frequently.

3) The matplotlib plotting library. The plots and graphs are much nicer than anything I’ve ever seen in IDL. They are easy to create and very flexible. I have just scratched the surface of what matplotlib is capable, and am impressed with what I see.

4) The Tkinter GUI library. It works out of the box! No extra installation of Tcl/Tk required (as was needed with Ruby Tk). And, the GUI’s have a very native feel. (I have written two GUI programs so far: an air parcel thermodynamic calculator and a geostrophic wind calculator. If anyone would like to try them out I would be happy to send them to you.)

5) The Python Image Library (PIL). This is another great library that comes as part of the Enthought distribution, and works out of the box. In Ruby the similar library is the RMagick library which requires the separate installation of the ImageMagick software and the RMagick library, along with the headache of sorting out all the dependencies. I always dreaded setting this up on a new system. But with Python and PIL, it is load and go!

6) The extremely simple for-loop syntax.

7) The Enthought Python distribution. Everything works! Installation is painless!

I am a willing and happy Python convert. I plan on writing all future code in Python, except for heavy-duty number crunching in numerical models (thought I do want to write a shallow-water model in Python, just because …). My Ruby books will likely be donated to my local library in the near future, and I won’t shed a tear.

My one nagging concern about switching to Python is the whole Python 2 vs. Python 3 conundrum. I have been using Python 2.7, which is kind of a ‘bridge’ version between the older Python 2 and the new Python 3 (which is not backwards compatible with Python 2). There is the fear that at some point the momentum will switch to Python 3 and my code will no longer work. However, I didn’t want to jump right in to Python 3, since many of its libraries are not mature. Python 2.7 does incorporate many of the newer features from Python 3 and I’ve been taking pains to write my code using the newer syntax whenever possible. Two examples are:

1) Using the print function rather than the print statement: print(stuff) rather than print stuff.

2) Using the newer string formatting syntax:

x = 'World'
hello_string = 'Hello {0:s}'.format(x)

rather than

hello_string = 'Hello %s' %x

Being a newcomer to Python I can easily train myself to use the newer Python 3 style syntax. But, I can certainly feel for the veteran Python programmer who has thousands of line of legacy code that may not work with Python 3.

This entry was posted in Beginner, Featured Tips, Stories, Why Python?. Bookmark the permalink.
  • Gordon McQuarrie

    I am a consultant aiming to help the U.K. Met Office in their use of Python.
    Your viewpoints will help me greatly.
    Thanks

  • Jos de Kloe

    As for your python 2 vs 3 worries: I have found that if you develop in python 2 and run the 2to3 conversion tool on a regular basis the migration is quite easy. Ofcourse it also depends on the availability of libraries you need. At least numpy, matplotlib, scipy are already available in python3. I am not sure about the enthought package though.