by Milos Pelikan, Senior Analyst and Scott Manley, Senior Analyst/Programmer
Python is a scripting language available free for download for a range of platforms including Windows, Unix, Linux and the Mac. Python has gained considerable popularity due to its simple syntax but powerful library and command set.
The first version of Python was developed in 1990 in Amsterdam, however, the language is now owned and developed by the Python Software Foundation. The Python name comes not from the reptile but from the English comedy team ‘Monty Python’. Python enjoys a growing popularity for development of scripts as an alternative to languages such as Perl and TCL.
Python & ArcGIS
With the release of ArcGIS 9, ESRI has added support for the Python language. Python scripts can be written and added as Tools to ArcEditor or ArcView to perform the type of processing tasks previously written in AML or Avenue. Tools can be configured to easily pass one or more arguments to the Python script.
Editing and Running Python code
You have a number of options for authoring and editing your Python scripts:
- The first and simplest is to use your favorite text editor, there are Python ‘plug-ins’ for many of the most popular editors such as Textedit and UltraEdit.
- Secondly, the version of Python installed with ArcGIS comes with a simple IDE called WinPython. This will provide simple text highlighting and popup syntax to assist you when authoring scripts.
- The final option is to obtain one of the dedicated IDEs now being developed for Python. Many of these are commercial products ranging in price from $100 to $500, most of these will have 30-60 day evaluation version available for download.
Running your code
As Python is an ‘interpreted’ language rather than ‘compiled’, a single Python script can be run on any platform (with some minor exceptions).
To run a Python script we use the Python ‘interpreter’ or ‘run-time engine’, interpreters are available for most platforms. To run your script, simply type the following at the command prompt:
(The python program will need to be in your path or in your current working directory).
Some basic Python syntax
As with all languages, the best way to learn it is to roll up your sleeves and give it a go. Here are some of the basic code elements to get started.
Python provides if, elif and else commands for conditional execution of script elements.
|i = 10|
if i == 1:
elif i == 2:
elif i == 3:
elif i == 4:
elif i == 5:
print "Greater than Five"
Greater than Five
- Python uses ‘==’ rather than ‘=’ for equality checking
- If statements end in ‘:’
- Execution blocks must be indented otherwise an error will result
- The ‘elif’ statement is used instead of ‘case’ or ‘switch’ statements
Looping statementsFor looping, Python provides the familiar for construct. for will loop through a list or array of items placing each value it finds into a holding variable. This variable can then be used to output or interact with each value in turn.
To loop through a list of items use the following syntax:
|a = ['Spatial', 'Vision', 'solutions']|
for x in a:
- The for statement will automatically extract each item from the array and place it in the variable provided
To repeat a section of code a number of times use the range() function:
|a = range(3)|
for x in a:
- range() can also be used with two arguments range(i, j) with i as the start and j as the end value.
Books and Web Sites
This is the home of the python language, from here you can download python and python documentation. They also have tutorials and how-to guides.
An excellent free python distribution for Windows (comes with the PythonWin IDE.)
Dive into Python:http://www.diveintopython.org/
A free but very good introduction for Python (also available at bookshops)
Thinking in Python:http://www.mindview.net/Books/TIPython
Another free and excellent Python book (again not for beginners)