Python
Python (http://python.org) is a modern, high-level language suitable for a wide variety of programming tasks. It is often used as a scripting language, automating and simplifying tasks at the operating system level, but it is equally suitable for building large and complex programs. Python has been used to write web-based systems, desktop applications, games, scientific programs, and even utilities and other higher-level parts of various operating systems.
Python supports a wide range of programming idioms, from straightforward procedural programming to object-oriented programming and functional programming.
Python is sometimes criticized for being an interpreted language, and can be slow compared to compiled languages such as C. However, the use of bytecode compilation and the fact that much of the heavy lifting is done by library code means that Python's performance is often surprisingly good—and there are many things you can do to improve the performance of your programs if you need to.
Open source versions of the Python interpreter are freely available for all major operating systems. Python is eminently suitable for all sorts of programming, from quick one-off scripts to building huge and complex systems. It can even be run in interactive (command-line) mode, allowing you to type in one-off commands and short programs and immediately see the results. This is ideal for doing quick calculations or figuring out how a particular library works.
One of the first things a developer notices about Python compared with other languages such as Java or C++ is how expressive the language is: what may take 20 or 30 lines of code in Java can often be written in half a dozen lines of code in Python. For example, imagine that you wanted to print a sorted list of the words that occur in a given piece of text. In Python, this is easy:
words = set(text.split()) for word in sorted(words): print(word)
Implementing this kind of task in other languages is often surprisingly difficult.
While the Python language itself makes programming quick and easy, allowing you to focus on the task at hand, the Python Standard Library makes programming even more efficient. This library makes it easy to do things such as converting date and time values, manipulating strings, downloading data from web sites, performing complex maths, working with e-mail messages, encoding and decoding data, XML parsing, data encryption, file manipulation, compressing and decompressing files, working with databases—the list goes on. What you can do with the Python Standard Library is truly amazing.
As well as the built-in modules in the Python Standard Library, it is easy to download and install custom modules, which could be written either in Python or C. The Python Package Index (http://pypi.python.org) provides thousands of additional modules that you can download and install. And if this isn't enough, many other systems provide Python bindings to allow you to access them directly from within your programs. We will be making heavy use of Python bindings in this book.
Python is in many ways an ideal programming language. Once you are familiar with the language and have used it a few times, you'll find it incredibly easy to write programs to solve various tasks. Rather than getting buried in a morass of type definitions and low-level string manipulation, you can simply concentrate on what you want to achieve. You almost end up thinking directly in Python code. Programming in Python is straightforward, efficient, and, dare I say it, fun.
Python 3
There are two main flavors of Python in use today: the Python 2.x series has been around for many years and is still widely used today, while Python 3.x isn't backward compatible with Python 2 and is becoming more and more popular as it is seen as the main version of Python going forward.
One of the main things holding back the adoption of Python 3 is the lack of support for third-party libraries. This has been particularly acute for Python libraries used for geospatial development, which are often dependent on individual developers or have requirements that were not compatible with Python 3 for quite a long time. However, all the major libraries used in this book can now be run using Python 3, and so all the code examples in this book have been converted to use Python 3 syntax.
If your computer runs Linux or Mac OS X, then you can use Python 3 with all these libraries directly. If, however, your computer runs MS Windows, then Python 3 compatibility is more problematic. In this case, you have two options: you can attempt to compile the libraries yourself to work with Python 3 or you can revert to using Python 2 and make adjustments to the example code as required. Fortunately, the syntax differences between Python 2 and Python 3 are quite straightforward, so not many changes will be required if you do choose to use Python 2.x rather than Python 3.x.