>
>
>
>
CHAPTER 26
Working with Python
As PHP has come to dominate the world of web scripting, Python is increasingly dominating the domain of command-line scripting. Python's precise and clean syntax makes it one of the easiest languages to learn, and it enables programmers to code more quickly and spend less time maintaining their code. Although PHP is fundamentally similar to Java and Perl, Python is closer to C and Modula-3, and so it might look unfamiliar at first.
Most other languages have a group of developers at their cores, but Python has Guido van Rossum — creator, father, and Benevolent Dictator For Life (BDFL). Although Guido spends less time working on Python now, he still essentially has the right to veto changes to the language, which has enabled it to remain consistent over the many years of its development. The end result is that, in Guido's own words, 'Even if you are in fact clueless about language design, you can tell that Python is a very simple language.'
The following pages constitute a 'quick start' tutorial to Python, designed to give you all the information you need to put together basic scripts and to point you toward resources that can take you further.
Python on Linux
Fedora comes with Python installed by default, as do many other versions of Linux and Unix — even Mac OS X comes with Python preinstalled. This is partly for the sake of convenience: Because Python is such a popular scripting language, preinstalling it saves having to install it later if the user wants to run a script. However, in Fedora's case, part of the reason for preinstallation is that several of the core system programs are written in Python, including yum
itself.
The Python binary is installed into /usr/bin/python
; if you run that, you enter the Python interactive interpreter, where you can type commands and have them executed immediately. Although PHP also has an interactive mode (use php -a
to activate it), it is neither as powerful nor as flexible as Python's.
As with Perl, PHP, and other scripting languages, you can also execute Python scripts by adding a shebang line to the start of your scripts that points to /usr/bin/python
and then setting the file to be executable. If you haven't seen one of these before, they look something like this: #!/usr/bin/python
.
The third and final way to run Python scripts is through mod_python
, which is installed by default when you select the Web Server application group from the Add/Remove Packages dialog.
For the purposes of this introduction, we will be using the interactive Python interpreter because it provides immediate feedback on commands as you type them.
Getting Interactive
We will be using the interactive interpreter for this chapter, so it is essential that you are comfortable using it. To get started, open a terminal and run the command python
. You should see this:
[paul@caitlin ~]$ python
Python 2.3.4 (#1, Oct 26 2004, 16:42:40)
[GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
Type 'help', 'copyright', 'credits' or 'license' for more information.
>>>
The >>>
is where you type your input, and you can set and get a variable like this:
>>> python = 'great' >>> python
'great'
>>>
On line 1, the variable python
is set to the text great
, and on line 2 that value is read back from the variable when you type the name of the variable you want to read. Line 3 shows Python printing the variable; on line 4, you are back at the prompt to type more commands. Python remembers all the variables you use while in the interactive interpreter, which means you can set a variable to be the value of another variable.
When you are finished, press Ctrl+D to exit. At this point, all your variables and commands are forgotten by the interpreter, which is why complex Python programs are always saved in scripts!
The Basics of Python
Python is a language wholly unlike most others, and yet it is so logical that most people can pick it up very quickly. You have already seen how easily you can assign strings, but in Python nearly everything is that easy—as long as you remember the syntax!
Numbers
The way Python handles numbers is more precise than some other languages. It has all the normal operators — such as +
for addition, -
for subtraction, /
for division, and *
for multiplication — but it adds %
for modulus (division remainder), **
for raise to the power, and //
for floor division. It is also very specific about which type of number is being used, as this example shows:
>>> a = 5
>>> b = 10