The Python Programming Language

NOTE: This is the old 2009–2011 tutorial for reference only

Introduction

Python is a dynamic programming language that offers a readable syntax, integration with other languages and tools, and is relatively easy to learn. We will use Python throughout this course. I use Python for running numerical models, generating dynamic Web pages, manipulating data and files, and for many other research applications. See Python Advocacy and references therein for an explanation of how great Python really is.

There are many tutorials where you can learn the basics of programming with Python. A good place to start is the official site Python, which offers, among other things, The Python Tutorial. A very important reference manual details The Python Standard Library, which describes built-in data types, functions, and exceptions, along with specific modules that you must import at the top of your program. These powerful modules dramatically enhance Python's capabilities. Lastly, A Byte of Python provides a great introduction to Python for newbies. Note that Blizzard has Python version 2.4.3 installed. Here is a huge list of Python Tutorials for lots of applications. Start with the list under Beginners.

While reading tutorials may give a nice overview, my experience is that learning a programming language is best accomplished by jumping right in and modifying an existing program. With a little practice, you'll be writing your own programs from scratch. The following steps will get you started with using Python on Blizzard.

Reading, Analyzing, and Writing Files

Many tasks in programming often involve reading and writing files. Here, you will retrieve, read, and analyze the most recent observations from the Oklahoma Mesonet. You may want to read the parameter description for these files. Grab MDFpickle.py and summary.py and save them in an appropriate directory on Blizzard using wget.

Open these files with gvim or another suitable text editor and examine the code. (To colorize your code in gvim, and thus make it much easier to read, visit the Customizing GVIM tutorial.) Notice that these Python programs use tabs for indentation, unlike other programming languages with which you may have some familiarity. This makes it rather easy to read your programs, especially if you have several nested blocks, and actually forces good programming practice. For some interesting opinions, search Google for discussions of Python's indentation rules.

You can execute your Python programs on Blizzard after changing the file permissions to make them executable: chmod u+x *.py Execute these programs with ./MDFpickle.py
./summary.py latest.mdf.pickle
Have a look at an explanation of Python's pickle module to see what MDFpickle.py is doing.

snow If your Python programs execute without a problem, then make some modifications to summary.py. Before doing so, you may want to make a copy of your file: cp summary.py mycode.py Read through the comments within mycode.py and follow the directions to add your code for calculating the average wind direction and correlations between variables. If you're not sure what a particular statement does, Google it. If that doesn't work out for you, please ask for help. Your completed program should produce a summary report for a set of Oklahoma Mesonet observations that, if you added the optional HTML tags, looks much like this one. Please post your latest.mdf, mycode.py, and mdfsummary.txt (or .html if you added HTML tags) files to your password-protected Web page and send me a link to the page when you are done.

North Carolina's Public Liberal Arts University