Beginning Python®: Using Python 2.6 and Python 3.1

James Payne

Wrox Programmer to Programmer™

at a Glace

Part I: Dipping Your Toe into Python
Programming Basics and Strings
Numbers and Operators
Variables — Names for Values
Part II: Python Language and the Standard Library
Making Decisions
Classes and Objects
Organizing Programs
Files and Directories
Other Features of the Language
Building a Module
Text Processing 
Part III: Putting Python to Work
Writing a GUI with Python
Accessing Databases
Using Python for XML
Network Programming
Extension Programming with C
Numerical Programming
An Introduction to Django 
Web Applications and Web Services 
Integrating Java with Python
Part IV: Appendices
Appendix A: Answers to the Exercises
Appendix B: Online Resources
Appendix C: What’s New in Python 3.1
Appendix D: Glossary

e-books shop
e-books shop
Purchase Now !
Just with Paypal

Book Details
 2.00 USD
 628 p
 File Size
 4,519 KB
 File Type
 PDF format
 2010 by Wiley Publishing, Inc  

About the Author
James Payne (Margate, FL) is Editor-in-Chief of Developer Shed, Inc. and has been writing and
programming since the age of seven years old. Proficient in many languages, he has written over
400 articles covering practically every major programming language. As a contractor, he develops
proprietary software for the financial industry using Python and likes to dabble in Django in his
spare time.

Welcome to Python 3.1!
I’ve been working with Python for about ten years now, and every new version has caused me to fall in
love with the language all over again. Version 3.1 is no different. If you are new to Python, rest easy —
I’ll guide you every step of the way. If, on the other hand, you are an old Python hand exploring the new version, the book is structured so that you can learn the new information you need, without wasting
time on already-known information.
I wanted to write this book because I love Python. I love it! And I want to share my love with you. And,
maybe you’ll grow to love it as I do.

Who This Book Is For
If you’re computer-literate, and want to learn a fun programming language to better control your
computer, this book is for you.
If you are a system administrator who wants to learn a great language to help you better manage and
configure systems and networks, this book is for you.
If you already know Python, but are wondering what cool new features are available in version 3.1, this
book is for you. In summary, this book is for anyone interested in exploring Python programming with the newest and most full-featured, easy-to-use version, 3.1.

What This Book Covers 
This book is designed to cover Python 3.1. Python 3.1, released in 2009, is the latest major revision of the Python programming language. Since Python is a cross-platform language, the content and examples in the book are applicable in any platform (unless specified otherwise). When there is a choice to be made as to platform independence, the examples will be as cross-platform as possible.
In addition, since Python 3.1 is relatively new, not all supporting libraries have been updated to work in
Python 3.x. In those instances where this is the case and it is felt that the theory still needs to be
expounded upon, Python 2.6 will be used in lieu of version 3.1.
What You Need to Use This Book
There are some minimal requirements to use the material in this book. The following are
recommendations, as Python itself runs on many different platforms. However, the first chapters assume that you have access to a GUI such as is available in Windows, Mac OS X, or the X Window system on UNIX and Linux. Naturally, some chapters, such as the GUI chapter, require the GUI as well, and chapters involving networking will make much more sense if a network connection is in place.
Following are the suggested minimum requirements:
❑ A PC running Linux, a BSD UNIX, or Windows running at 500MHz or faster, or a G3 or later
Macintosh running Mac OS X version 10.2 or later
❑ 256MB of memory (at a minimum)
❑ A graphical user interface native to the platform you are on
❑ Necessary access to the computer you are on so that you may install required software
❑ Network access to a TCP/IP network such as the Internet or a campus network
❑ Internet access to download required software

Table of Contents
Introduction xxvii
Part I: Dipping Your Toe into Python 1
Chapter 1: Programming Basics and Strings 3
How Programming is Different from Using a Computer 3
Programming is Consistency 4
Programming is Control 4
Programming Copes with Change 5
What All That Means Together 5
The First Steps 5
Installing Python 3.1 on Non-Windows Systems 6
Using the Python Shell 6
Beginning to Use Python — Strings 7
What is a String? 7
Why the Quotes? 7
Why Three Types of Quotes? 8
Using the print() Function 8
Understanding Different Quotes 9
Putting Two Strings Together 11
Joining Strings with the Print() Function 12
Putting Strings Together in Different Ways 12
Summary 13
Exercises 14
Chapter 2: Numbers and Operators 15
Different Kinds of Numbers 15
Numbers in Python 16
Program Files 18
Using the Different Types 19
Basic Math 21
Some Surprises 23
Using Numbers 24
Order of Evaluation 24
Number Formats 25
Mistakes Will Happen 26
Some Unusual Cases 27
Summary 28
Exercises 29
Chapter 3: Variables — Names for Values 31
Referring to Data — Using Names for Data 31
Changing Data Through Names 33
Copying Data 33
Names You Can’t Use and Some Rules 34
Using More Built-in Types 34
Tuples — Unchanging Sequences of Data 34
Lists — Changeable Sequences of Data 37
Dictionaries — Groupings of Data Indexed by Name 39
Treating a String Like a List 41
Special Types 42
Other Common Sequence Properties 43
Referencing the Last Elements 43
Ranges of Sequences 44
Growing Lists by Appending Sequences 45
Using Lists to Temporarily Store Data 45
Working with Sets 46
Summary 47
Exercises 48
Part II: Python Language and the Standard Library 49
Chapter 4: Making Decisions 51
Comparing Values — Are They the Same? 51
Doing the Opposite — Not Equal 53
Comparing Values — Which One Is More? 54
More Than or Equal, Less Than or Equal 55
Reversing True and False 56
Looking for the Results of More Than One Comparison 56
How to Get Decisions Made 57
Repetition 60
How to Do Something — Again and Again 60
Stopping the Repetition 62
Handling Errors 65
Trying Things Out 65
Summary 67
Exercises 69
Chapter 5: Functions 71
Putting Your Program into Its Own File 71
Functions: Grouping Code under a Name 73
Choosing a Name 75
Describing a Function in the Function 75
The Same Name in Two Different Places 76
Making Notes to Yourself 78
Asking a Function to Use a Value You Provide 79
Checking Your Parameters 81
Setting a Default Value for a Parameter—Just in Case 83
Calling Functions from within Other Functions 84
Functions Inside of Functions 86
Flagging an Error on Your Own Terms 87
Layers of Functions 88
How to Read Deeper Errors 88
Summary 89
Exercises 90
Chapter 6: Classes and Objects 93
Thinking About Programming 93
What is an Object? 93
Objects You Already Know 94
Looking Ahead: How You Want to Use Objects 95
Defining a Class 96
How Code Can Be Made into an Object 96
Objects and Their Scope 104
Summary 107
Exercises 108
Chapter 7: Organizing Programs 111
Modules 112
Importing a Module So That You Can Use It 112
Making a Module from Pre-existing Code 113
Using Modules — Starting with the Command Line 115
Changing How Import Works — Bringing in More 118
Packages 118
Modules and Packages 120
Bringing Everything into the Current Scope 120
Re-importing Modules and Packages 121
Basics of Testing Your Modules and Packages 124
Summary 124
Exercises 125
Chapter 8: Files and Directories 127
File Objects 127
Writing Text Files 128
Appending Text to a File 129
Reading Text Files 130
File Exceptions 131
Paths and Directories 131
Exceptions in os 132
Paths 132
Directory Contents 135
Obtaining Information about Files 136
Renaming, Moving, Copying, and Removing Files 137
Example: Rotating Files 138
Creating and Removing Directories 140
Globbing 140
Summary 142
Exercises 142
Chapter 9: Other Features of the Language 143
Lambda and Filter: Short Anonymous Functions 143
Map: Short-Circuiting Loops 144
Decisions within Lists — List Comprehension 145
Generating Iterators for Loops 146
Special String Substitution Using Dictionaries 148
Featured Modules 149
Getopt — Getting Options from the Command Line 149
Using More Than One Process 152
Threads — Doing Many Things in the Same Process 154
Summary 156
Exercises 156
Chapter 10: Building a Module 157
Exploring Modules 157
Importing Modules 159
Finding Modules 159
Digging through Modules 160
Creating Modules and Packages 162
Working with Classes 163
Defining Object-Oriented Programming 163
Creating Classes 163
Extending Existing Classes 165
Finishing Your Modules 166
Defining Module-Specific Errors 166
Choosing What to Export 167
Documenting Your Modules 168
Testing Your Module 176
Running a Module as a Program 178
Creating a Whole Module 179
Installing Your Modules 183
Summary 187
Exercises 188
Chapter 11: Text Processing 189
Why Text Processing Is So Useful 189
Searching for Files 190
Clipping Logs 191
Sifting through Mail 192
Navigating the File System with the os Module 192
Working with Regular Expressions and the re Module 199
Summary 203
Exercises 204
Part III: Putting Python to Work 205
Chapter 12: Testing 207
Assertions 208
Test Cases and Test Suites 209
Test Fixtures 213
Putting It All Together with Extreme Programming 216
Implementing a Search Utility in Python 216
A More Powerful Python Search 222
Formal Testing in the Software Life Cycle 224
Summary 225
Chapter 13: Writing a GUI with Python 227
GUI Programming Toolkits for Python 228
Tkinter Introduction 229
Creating GUI Widgets with Tkinter 229
Resizing the Widget 230
Configuring Widget Options 231
Putting the Widgets to Work 231
Creating Layouts 232
Packing Order 233
Controlling Widget Appearances 233
Radio Buttons and Checkboxes 235
Dialog Boxes 236
Other Widget Types 237
Summary 238
Exercises 238
Chapter 14: Accessing Databases 239
Working with DBM Persistent Dictionaries 240
Choosing a DBM Module 240
Creating Persistent Dictionaries 241
Accessing Persistent Dictionaries 243
Deciding When to Use DBM and When to Use a Relational Database 245
Working with Relational Databases 245
Writing SQL Statements 247
Defining Tables 249
Setting Up a Database 250
Using the Python Database APIs 252
Downloading Modules 252
Creating Connections 253
Working with Cursors 253
Working with Transactions and Committing the Results 260
Examining Module Capabilities and Metadata 261
Handling Errors 261
Summary 262
Exercises 263
Chapter 15: Using Python for XML 265
What Is XML? 265
A Hierarchical Markup Language 265
A Family of Standards 267
What Is a Schema/DTD? 268
What Are Document Models For? 268
Do You Need One? 268
Document Type Definitions 268
An Example DTD 268
DTDs Aren’t Exactly XML 270
Limitations of DTDs 270
Schemas 270
An Example Schema 270
Schemas Are Pure XML 271
Schemas Are Hierarchical 271
Other Advantages of Schemas 271
XPath 272
HTML as a Subset of XML 272
The HTML DTDs 273
HTMLParser 273
XML Libraries Available for Python 274
What Is SAX? 274
Stream-based 275
Event-driven 275
What Is DOM? 275
In-memory Access 275
Why Use SAX or DOM 275
Capability Trade-Offs 276
Memory Considerations 276
Speed Considerations 276
SAX and DOM Parsers Available for Python 276
xml.sax 276
xml.dom.minidom 277
Intro to XSLT 280
Transformation and Formatting Language 280
Functional, Template-Driven 280
What Is lxml? 280
Element Classes 281
Adding Text to Elements 282
Parsing with lxml 283
Parsing Files 284
Summary 285
Exercises 285
Chapter 16: Network Programming 287
Understanding Protocols 289
Comparing Protocols and Programming Languages 289
The Internet Protocol Stack 290
A Little Bit About the Internet Protocol 292
Sending Internet E-mail 293
The E-mail File Format 294
MIME Messages 295
Sending Mail with SMTP and smtplib 303
Retrieving Internet E-mail 305
Parsing a Local Mail Spool with mailbox 305
Fetching Mail from a POP3 Server with poplib 307
Fetching Mail from an IMAP Server with imaplib 309
Secure POP3 and IMAP 313
Webmail Applications Are Not E-mail Applications 313
Socket Programming 314
Introduction to Sockets 314
Binding to an External Hostname 316
The Mirror Server 317
The Mirror Client 318
SocketServer 320
Multithreaded Servers 321
The Python Chat Server 322
Design of the Python Chat Server 323
The Python Chat Server Protocol 323
The Python Chat Client 329
Single-Threaded Multitasking with select 331
Other Topics 332
Miscellaneous Considerations for Protocol Design 333
The Peer-to-Peer Architecture 333
Summary 334
Exercises 335
Chapter 17: Extension Programming with C 337
Extension Module Outline 338
Building and Installing Extension Modules 340
Passing Parameters from Python to C 342
Returning Values from C to Python 345
The LAME Project 346
The LAME Extension Module 350
Using Python Objects from C Code 363
Summary 366
Exercises 366
Chapter 18: Numerical Programming 367
Numbers in Python 368
Integers 368
Long Integers 369
Floating-point Numbers 369
Formatting Numbers 370
Characters as Numbers 373
Mathematics 374
Arithmetic 374
Built-in Math Functions 375
Complex Numbers 378
Arrays 380
The array Module 382
Summary 384
Exercises 384
Chapter 19: An Introduction to Django 387
What Are Frameworks and Why Would I Use One? 388
Other Features of Web Frameworks 388
Django — How It All Began 389
Installing Django 389
Understanding Django’s Architecture 390
Initial Project Setup 391
Creating a View 394
Working with Templates 396
Using Templates and Views 398
Models 401
Creating a Model: First Steps — Configure the Database Settings 401
Creating a Model: Creating an Application 403
Working with Models: Installation 404
Summary 405
Exercises 406
Chapter 20: Web Applications and Web Services 407
REST: The Architecture of the Web 408
Characteristics of REST 409
REST Operations 410
HTTP: Real-World REST 411
The Visible Web Server 412
The HTTP Request 415
The HTTP Response 416
CGI: Turning Scripts into Web Applications 417
The Web Server Makes a Deal with the CGI Script 419
CGI’s Special Environment Variables 420
Accepting User Input through HTML Forms 422
HTML Forms’ Limited Vocabulary 422
The cgi Module: Parsing HTML Forms 423
Safety When Accessing Form Values 423
Building a Wiki 428
The BittyWiki Core Library 429
The BittyWiki Web Interface 432
Web Services 441
How Web Services Work 442
REST Web Services 442
REST Quick Start: Finding Bargains on 443
Introducing WishListBargainFinder 445
Giving BittyWiki a REST API 448
Wiki Search-and-Replace Using the REST Web Service 451
The XML-RPC Request 457
The XML-RPC Response 459
If Something Goes Wrong 459
Exposing the BittyWiki API through XML-RPC 460
Wiki Search-and-Replace Using the XML-RPC Web Service 463
SOAP 465
SOAP Quick Start 466
The SOAP Request 466
The SOAP Response 467
If Something Goes Wrong 468
Exposing a SOAP Interface to BittyWiki 468
Wiki Search-and-Replace Using the SOAP Web Service 470
Documenting Your Web Service API 472
Human-Readable API Documentation 473
The XML-RPC Introspection API 474
WSDL 475
Choosing a Web Service Standard 478
Web Service Etiquette 479
For Consumers of Web Services 479
For Producers of Web Services 479
Using Web Applications as Web Services 480
Summary 480
Exercises 480
Chapter 21: Integrating Java with Python 481
Scripting within Java Applications 482
Comparing Python Implementations 483
Installing Jython 483
Running Jython 484
Running Jython Interactively 484
Running Jython Scripts 485
Controlling the jython Script 486
Making Executable Commands 487
Running Jython on Your Own 488
Packaging Jython-Based Applications 488
Integrating Java and Jython 489
Using Java Classes in Jython 489
Accessing Databases from Jython 494
Writing Java EE Servlets in Jython 500
Choosing Tools for Jython 506
Testing from Jython 506
Embedding the Jython Interpreter 507
Calling Jython Scripts from Java 508
Handling Differences between C-Python and Jython 510
Summary 511
Exercises 512
Part IV: Appendices 513
Appendix A: Answers to the Exercises 515
Appendix B: Online Resources 549
Appendix C: What’s New in Python 3.1 553
Appendix D: Glossary 559
Index 569

e-books shop

How This Book Is Structured
As might be expected from a “Beginning” book, the book begins with an introduction to the language.
From there, you’ll move through the core of the language, then move on to more advanced and
specialized topics. The book is divided up into four parts.
Part I — Dipping Your Toe into Python
The first part will allow you to, as the title suggests, dip your toe in.
Programming Basics and Strings
First you’ll be introduced to Python. This chapter will explore what Python is, and why it is so useful
and powerful. Also explored will be Python’s history from its early development to the newest version,
which is the focus of this book. You’ll also learn about the scope of Python’s reach, and all the different
areas of application development in which Python plays a part. Finally, you’ll learn to work with your
first data type — strings.
Numbers and Operators
This chapter will guide you through the basics of working with numbers and operators. You will learn
the different types of numbers, how to perform simple — and complex — equations, and work with the
various operators. You will also learn about order of precedence and formatting numbers.
Variables — Names for Values
Ultimately, programming languages help you to manage different types of information — in other
words, data. An understanding of data types and how they are represented in Python is essential to
programming in Python. This chapter will help you to understand the best ways to represent different
data types in Python.
Part II — Python Language and the Standard Library
Of course, the core piece of knowledge you need to use a language is to know the language itself, and
familiarize yourself with its syntax and modules. This part will start small, with data types and
variables, and gradually introduce additional concepts until you have all the information you need to
develop fully functional Python programs.
You’ll want to read through these chapters sequentially –– each chapter builds on the information
presented in the previous chapter.
Making Decisions
Ultimately, there will come a point when your program must make a decision — do I take this path or
that path? And what happens when I take that path? In this chapter, you will learn how to compare data,
such as deciding if one value is greater than another, and use repetition to repeat repetitive tasks.  
This chapter will help you to expand on your Python knowledge by introducing you to functional
programming. Functions allow you to take advantage of powerful concepts like parameter passing and
code reuse. You’ll learn how to use functions to make your code more efficient and flexible.
Classes and Objects
Here you will be shown what objects are and learn to create classes. You will learn how to define them,
create objects in your classes, write methods, and discuss the scope of your objects.
Organizing Programs
When your programs get larger, you’ll want to divide them up into separate components. This chapter
will discuss Python modules. You’ll also explore packages, which are nothing but collections of modules.
Files and Directories
An important part of everyday programming is learning to work with files and directories. This chapter
focuses on creating, modifying, and working with files in general. In addition, you will learn how to
obtain data from files and how to interact with the various directories.
Other Features of the Language
Here you will learn about some of the other features the language has to offer, including how to make
decisions with lists, string substitutions with dictionaries, and some of the featured modules.
Building a Module
Modules help you save time by allowing you to reuse snippets of code. It also ensures fewer errors, as
the module you use will have been tested and used many times before. Here, we will learn to create our
own modules, as well as import and work with pre-existing modules –– something that makes Python
particularly powerful.
Text Processing
There are so many things you can do with text in programming and in essence, text is the key to
effectively communicating with your user. After all, without it, the only thing you are left with is images.
In this chapter you learn to process text in a variety of ways, including: working with regular
expressions, searching for files, and searching for files of a particular type.
Part III — Putting Python to Work
So, now that you know what Python is, and how to work with the language, what’s next, you ask? 
This final part explores many of the programming topics you’ll likely encounter or want to explore. 
These can be looked at sequentially, or in any order you like . . . these chapters are independent of each other.
There is only one way to ensure your program works before it is in the hands of the user, and that is by
testing your program. Here, you will learn not only the concepts behind properly testing your programs,
but the tools and frameworks available to you.
Writing a GUI with Python
Thus far in the book, all the programs work through the command line. In this chapter, you’ll be
introduced to the concept of GUI programming. You’ll also walk through creating a few GUI programs
with Tkinter, the most popular GUI toolkit used by Python programmers.
Accessing Databases
Databases store information that your program can use for an infinite amount of reasons. It also acts as a place for you to store information, and later retrieve that information for a given task. In this chapter you learn about the different types of databases and how to work with them.
Using Python for XML
XML is a powerful tool for processing data on the Internet. Here, you will learn the basics of XML
including the difference between schema and DTD, basic syntax, how to create and validate your own
XML, and more advanced topics such as using lxml.
Network Programming
Now that the Internet has wormed its way into our everyday lives, and has become more of a necessity
than a privilege, learning to programmatically send e-mails and allow users to communicate across the
web is essential. In this chapter, you will learn how to do just that.
Extension Programming with C
This chapter delves into programming with the C language, including working with C frameworks
and modules, the basics of C, and passing parameters from Python to C, and then returning value back
to Python.
Numerical Programming
Numbers were touched on briefly in the beginning of this book; now it is time to delve more deeply
below the surface. Here you will learn all there is to know about integers and floating point numbers, as
well as complex numbers, arrays, and working with built-in math functions and modules.
An Introduction to Django
Django is a web application framework written in Python, which utilizes the model-view-architecture
pattern. Originally created for managing news websites, Django has become popular for its ease of use,
allowing programmers to create complex websites in a simple fashion, including database-focused sites.
Here we will learn the basics of Django.
Web Applications and Web Services
Here you will learn the foundations of working with web applications and web services. You will learn
about the REST architecture, as well as how to work with HTTP Requests and Responses
Integrating Java with Python
In this chapter you learn the basics of Java, building a strong foundation before you delve blending the
two languages together. You will learn the various modules that allow you to work with Java in Python
and how to create simple, yet effective applications.
Part IV: Appendices
In the back of the book, there are some useful appendices to further your knowledge and fun with
❑ Answers to the Exercises
❑ Online Resources –– where do you go from here?
❑ What’s New in Python 3.1
❑ Glossary of terms
Loading... Protection Status