A Simple introduction to Python
18 Jan 2005
To continue in our series on scripting languages and more specifically
scripting options for Linux, we will look at Python (http://www.python.org/) this
time around. To call Python a scripting language is a bit of an understatement
since Python can do so much more than just scripting. Let's look at what makes
Python such an attractive choice for a variety of development uses.
Reasons to consider Python
Python is free. Now that is not unique in today's world of high quality open
source solutions, but it is a factor that is in Python's favor. The complete
source code for Python is provided as part of its distribution, so you as a
developer have what you need to customize Python to better fit your needs or to
make changes that help Python function within your environment.
Since Python compiles to an intermediate or bytecode, programs written in
Python can be expected to run the same on any of the platforms supported by
Python. This illustrates the strength of Python as a cross platform development
solution. With versions available on platforms from Linux to Windows to
Macintosh, you can build Python programs and deploy them across a
multi-platformed enterprise with ease.
The above two strengths of Python illustrate why Python might be considered
from a strategic perspective, but as a software developer there are a couple of
much more powerful reasons to consider Python.
Community support for Python is very strong, with add-on tools and resources
available that will allow you to build even very powerful GUI based desktop
applications. Libraries and the ability to integrate Python and C/C++
application code allows use on any high-level C code extensions you might
already have and also allows you to use Python as addition to C/C++. Python code
can call code written in C/C++ and it can also be called by applications written
in C/C++.
Python is built on a class model foundation, making it an excellent
programming language to use as an introduction to object-oriented programming.
Python supports the many OOP methodologies such as inheritance and polymorphism,
and because of the basic simplicity of the language, it is an excellent choice
for learning OOP.
Python, the first steps
Getting started with Python is very easy. Go to the Python Web Site at http://www.python.org/ and
download the version of Python that runs on your platform. Now if you are using
SUSE LINUX, you don't even have to do that. SUSE LINUX ships with a version of
Python available for installation.
To get started with Python on SUSE LINUX, open a terminal session and type
python at the prompt. Here is a screenshot of what you should see.
Here is Python running on Windows. To start python on Windows open a command
window and type python at the prompt (you may need to change into the Python
folder depending upon your configuration). You'll see this:
As you can see the two are very similar.
This command line interface to Python is very useful in learning Python and
also helpful later in your development as a debugging tool. To use the
interactive command line Python interpreter just enter your Python commands at
the ">>>" prompt and hit enter. A simple example of the use of the
command line is below.
In this simple example the Python interpreter compiled the code from the
command line and immediately executed it. No time consuming steps to compile and
link in order to see the results of the command.
Python does allow much more complex use of the command line interface. Below
is an example of a command line session that initializes a variable and then
uses that variable in its processing.
This should give you some idea of what is needed to get started with Python.
Your best first steps are to go to the Python web site, get Python installed (if
necessary) and then either go through the Python tutorial (http://docs.python.org/tut/tut.html) for a quick tutorial on
getting up and using Python or the quick introduction at (http://www.python.org/doc/Intros.html). Or both! These
resources will provide you with what you need in order to begin using Python
effectively. If you are new to programming or want a quick refresher that will
help you understand Python development from the ground up, try the Beginner's
Guide at http://www.python.org/moin/BeginnersGuide.
Using one or more of these resources should get you started and allow you to
get a good feel for what Python can be used to do. You will also find a large
number of additional resources listed on the Python web site for add-ons and
tools.
Conclusion
Python is much more powerful than a basic scripting language and contains a
very useful and feature-rich environment right out of the box. Using the command
line interface you can learn to use Python and master the basics of the language
without a heavy investment. Python is a great introduction to Object Oriented
Programming, with flexible uses both on the desktop and as a system-level
scripting tool.
Next steps
Next time we'll go more deeply into some of the more powerful development
capabilities of Python, focusing on GUI development for building powerful
desktop application, leveraging Python and a number of useful add-ons for
desktop development. We'll also look as tools that will help you develop Python
applications more easily and with greater power.
|