Index
[] accessing adding attributes
Index
[] BaseHTTPServer module , beginnings of strings built-in types ,
Index
[] CGI scripts child nodes classes , closing code commands (SQL) communication (Internet) dataemailstreaming data connections
Index
[]dictionarieslistsvalues databases entriesMySQLobjects defining deleting dictionaries valuesHTMLXML DOM trees, child nodes
Index
[] email endings of strings endswith() method entries (databases) execute() method executing extracting
Index
[] files modesreadingshelveTARZIP functions [See methods.]
Index
[] GET requests
Index
[] handling HTML documents HTMLParser module (HTML documents) HTTP servers httplib module
Index
[] Internet communication dataemailstreaming data
Index
[]
Index
[]
Index
[] list() method lists items
Index
[]endswith()list()open()parse()quit()read()readlines()split()walk() modules ,HTMLParser (HTML documents)httplibsocketSocketServerxml.sax MySQL databases entries
Index
[] namespaces , negative indices (strings) nodes child
Index
[] objects ,shelve files open() method opening
Index
[] parameters parse() method parsing POP3 servers POST requests
Index
[] quit() method
Index
[] read() method reading files readlines() method , requests GETPOST retrieving
Index
[] scripts (CGI) searching [See finding.] sending servers HTTP shelve files objects slicing socket module sockets SocketServer module (streaming data) split() method SQL commands starting statements streaming data strings endingstext substrings syntax statements system tools (modules) ,
Index
[]
Index
[] tags TAR files text strings threads tools (system modules) , trees (DOM), child nodes tuples , types data [See .]
Index
[]
Index
[] values dictionaries
Index
[] walk() method web services CGI scriptsHTTP serversXML-RPC websites
Index
[] XML documents child nodes XML-RPC xml.sax module
Index
[] ZIP files
Introduction
I was excited when my editor asked me to write a phrasebook on the Python language. The phrasebook is one of the smallest books I have ever written; however, it was one of the hardest.
The idea of a conventional phrasebook is to provide readers with quick phrases that actually mean something in the language. The Python phrasebook is designed to provide you with meaningful Python phrases that you can actually understand and use to quickly begin programming Python applications.
The content of this book are based on Python 2.4. You should keep in mind that the Python language is constantly being added to. I would recommend visiting the Python website at http://www.python.org to familiarize yourself with accessing the online documentation, available extensions, and any changes that are occurring.
This book is not a reference manual or language guide that encompasses the entire languagethat's not the purpose. The purpose is to provide you with a small, simple-to-use phrasebook that will get you going and provide a quick, easy reference later as you delve into new areas of the language.
When designing the content for this book, I tried to come up with the most relevant and interesting phrases that will actually help programs accomplish tasks that are pertinent to real-world needs. I welcome your comments and any phrases that you feel really need to be added to this book.
Note
Almost all the sample code used in this book is taken from actual working files. For your convenience, the Python scripts, CGI scripts, and HTML and XML documents that are shown as examples in the phrases of this book are available for download from the publisher's website. Register your book at www.samspublishing.com/register and download the code examples from this book. Feel free to modify them for your own needs.
I hope that you enjoy the phrases in this book and that they will be useful to you.
Chapter 1. Understanding Python
Python is an extremely powerful and dynamic object-oriented programming language. It has similarities to scripting languages such as Perl, Scheme, and TCL, as well as other languages such as Java and C.
This chapter is designed to give you a quick glimpse into the Python language to help you understand the phrases in the subsequent chapters. It is not meant to be comprehensive; however, it should give you a feel for the language and help you understand the basics so that you can refer to the Python documentation for more information.
Why Use Python?
There are several reasons to use Python. It is one of the easier languages to pick up and start using, and yet it can be extremely powerful for larger applications. The following are just some of the good points of Python:
Portability Python runs on almost every operating system, including Linux/Unix, Windows, Mac, OS 2, and others.
Integration Python can integrate with COM, .NET, and CORBA objects. There is a Jython implementation to allow the use of Python on any Java platform. IronPython is an implementation that gives Python programmers access to the .NET libraries. Python can also contain wrapped C or C++ code.
Easy It is very easy to get up to speed and begin writing Python programs. The clear, readable syntax makes applications simple to create and debug.
Power There are new extensions being written to Python all the time for things such as database access, audio/video editing, GUI, web development, and so on.
Dynamic Python is one of the most flexible languages. It's easy to get creative with code to solve design and development issues.
Open Source Python is an open source language, which means it can be freely used and distributed.
Invoking the Interpreter
Python scripts are executed by a Python interpreter. On most systems, you can start the Python interpreter by executing the python command at a console prompt. However, this can vary based on the system and development environment you have set up. This section discusses the standard methods to invoke the interpreter to execute Python statements and script files.
Invoking the interpreter without passing a script file as a parameter brings up the following prompt:
bwd-linux:/book # pythonPython 2.4.2 (#1, Apr 9 2006, 19:25:19)[GCC 4.1.0 (SUSE Linux)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>>
The Python prompt is indicated by >>> . If you execute a command that requires more input, a ... prompt will be displayed. From the interpreter prompt, you can execute individual Python statements, as follows:
>>> print "Printing a String"Printing a String
Invoking the interpreter with a script parameter, as shown next, begins execution of the script and continues until the script is finished. When the script is finished, the interpreter is no longer active.
bwd-linux:/book # python script.pyExecuting a Scriptbwd-linux:/book #
Scripts can also be executed from within the interpreter using the execfile(script) function built in to Python. The following example shows a script being executed using the execfile() function:
>>> execfile("script.py")Executing a Script>>>
Built-In Types
The built-in types that you will most frequently use in Python can be grouped into the categories listed in that is associated with each built-in object type and can be used to determine whether an object is of a specific type using the isinstance( object, typename ) function, as follows: