C H A P T E R 1
■ ■ ■
3
Language and Syntax
Elegant is an adjective that is often used to describe the Python language. The word elegant is defined as
“pleasingly graceful and stylish in appearance or manner.”
Uncomplicated and
powerful could also be
great words to assist in the description of this language. It is a fact that Python is an elegant language
that lets one create powerful applications in an uncomplicated manner. The ability to make reading and
writing complex software easier is the objective of all programming languages, and Python does just
that.
While we’ve easily defined the goal of programming languages in a broad sense in paragraph one,
we have left out one main advantage of learning the Python programming language: Python has been
extended to run on the Java platform, and so it can run anywhere with a JVM. There are also C and .NET
versions of Python with multiplatform support. So, Python can run nearly everywhere.
In this book, we
focus on Jython, the language implementation that takes the elegance, power, and ease of Python and
runs it on the JVM.
The Java platform is an asset to the Jython language much like the C libraries are for Python. Jython
is able to run just about everywhere, which gives lots of flexibility when deciding how to implement an
application. Not only does the Java platform allow for flexibility with regards to application deployment,
but it also offers a vast library containing thousands of APIs that are available for use by Jython. Add in
the maturity of the Java platform and it becomes easy to see why Jython is such an attractive
programming language. The goal, if you will, of any programming language is
to grant its developers the
same experience that Jython does. Simply put, learning Jython will be an asset to any developer.
As I’ve mentioned, the Jython language implementation takes Python and runs it on the JVM, but it
does much more than that. Once you have experienced the power of programming on the Java platform,
it will be difficult to move away from it. Learning Jython not only allows you to run on the JVM, but it
also allows you to learn a new way to harness the power of the platform. The language increases
productivity as it has an easily understood syntax that reads almost as if it were pseudocode. It also adds
dynamic abilities that are not available in the Java language itself.
In this chapter you will learn how to install and configure your environment, and you will also get an
overview of those features that the Python language has to offer. This chapter
is not intended to delve so
deep into the concepts of syntax as to bore you, but rather to give you a quick and informative
introduction to the syntax so that you will know the basics and learn the language as you move on
through the book. It will also allow you the chance to compare some Java examples with those which are
written in Python so you can see some of the advantages this language has to offer.
By the time you have completed this chapter, you should know the basic structure and organization
that Python code should follow. You’ll know how to use basic language concepts such as defining
variables, using reserved words, and performing basic tasks. It will give you a taste of using statements
and expressions. As every great program contains comments, you’ll learn
how to document single lines
of code as well as entire code blocks. As you move through the book, you will use this chapter as a
reference to the basics. This chapter will not cover each feature in completion, but it will give you
enough basic knowledge to start using the Python language.
www.it-ebooks.info
CHAPTER 1
■
LANGUAGE AND SYNTAX
4
The Difference between Jython and Python
Jython is an implementation of the Python language for the Java platform. Throughout this book, you
will be learning how to use the Python language, and along the way we will show you where the Jython
implementation differs from CPython, which is the canonical implementation
of Python written in the C
language. It is important to note that the Python language syntax remains consistent throughout the
different implementations. At the time of this writing, there are three mainstream implementations of
Python. These implementations are: CPython, Jython for the Java platform, and IronPython for the .NET
platform. At the time of this writing, CPython is the most prevalent of the implementations. Therefore if
you see the word Python somewhere, it could well be referring to that implementation.
This book will reference the Python language in sections regarding the language syntax or
functionality that is inherent to the language itself. However, the book will reference the name Jython
when discussing functionality and techniques that are specific to the Java platform implementation. No
doubt about it, this book will go in-depth to cover the key features of Jython and you’ll learn concepts
that only adhere the Jython implementation. Along the way, you will learn
how to program in Python
and advanced techniques.
Developers from all languages and backgrounds will benefit from this book. Whether you are
interested in learning Python for the first time or discovering Jython techniques and advanced concepts,
this book is a good fit. Java developers and those who are new to the Python language will find specific
interest in reading through Part I of this book as it will teach the Python language from the basics to
more advanced concepts. Seasoned Python developers will probably find more interest in Part II and
Part III as they focus more on the Jython implementation specifics. Often in this reference, you will see
Java code compared with Python code.
Installing and Configuring Jython
Before we delve into the basics of the language, we’ll learn how to obtain Jython and configure it for your
environment.
To get started, you will need to obtain a copy of Jython from the official website
www.jython.org. Because this book focuses on release 2.5.x, it would be best to visit the site now and
download the most recent version of that release. You will see that there are previous releases that are
available to you, but they do not contain many of the features which have been included in the 2.5.x
series.
Jython implementation maintains consistent features which match those in the Python language for
each version. For example, if you download the Jython 2.2.1 release, it will include all of the features that
the Python 2.2 release contains. Similarly, when using the 2.5 release you will have access to the same
features which are included in Python 2.5. There are also some extra pieces included with the 2.5 release
which are specific to Jython. We’ll discuss more about these extra features throughout the book.
Please grab a copy of the most recent version of the Jython 2.5 release. You will see that the release is
packaged as a cross-platform executable JAR file. Right away, you can
see the obvious advantage of
running on the Java platform. . .one installer that works for various platforms. It doesn’t get much easier
than that! In order to install the Jython language, you will need to have Java 5 or greater installed on your
machine. If you do not have Java 5 or greater then you’d better go and grab that from www.java.com and
install it before trying to initiate the Jython installer.
You can initiate the Jython installer by simply double-clicking on the JAR file. It will run you through
a series of standard installation questions. At one point you will need to determine which features you’d
like to install. If you are interested in looking through the source code for Jython, or possibly developing
code for the project then you should choose the “All” option to install everything. . .including source.
However, for most Jython developers and especially for those who are just beginning to learn the
language, I would recommend choosing the “Standard” installation option. Once you’ve chosen your
options and supplied an installation path then you will be off to the races.
In order to run Jython, you will need to invoke the jython.bat executable file on Windows or the
jython.sh file on *NIX machines and Mac OS X. That being said, you’ll have to
traverse into the directory
www.it-ebooks.info