Python® For Dummies®
Published by
Wiley Publishing, Inc.
111 River St.
Hoboken, NJ 07030-5774
www.wiley.com
Copyright © 2006 by Wiley Publishing, Inc., Indianapolis, Indiana
Published by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at http://www.wiley.com/go/permissions.
Trademarks: Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Website is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Website may provide or recommendations it may make. Further, readers should be aware that Internet Websites listed in this work may have changed or disappeared between when this work was written and when it is read. FULFILLMENT OF EACH COUPON OFFER IS THE SOLE RESPONSIBILITY OF THE OFFEROR.
For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.
For technical support, please visit www.wiley.com/techsupport.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.
Library of Congress Control Number: 2006924031
ISBN-13: 978-0-471-77864-6
ISBN-10: 0-471-77864-8
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
1O/ST/QY/QW/IN
Stef Maruch got her hands on an original 128K Mac in 1984 and has been writing about computers ever since. She has over fifteen years’ experience in instructional design, writing, and editing end-user computer manuals, including tutorials and user’s guides for Apple Newton, HyperCard and HyperTalk, and DVD Studio Pro.
Aahz Maruch is a writer, trainer, and consultant who has been using Python for more than seven years. He has been using computers professionally for 20 years, and his background includes stints of high-end tech support, systems administration, and programming. Aahz is currently working as a programmer for a company with a Web-based application.
The authors can be reached at authors@pythonfood.com.
Stef: I dedicate this book to my parents, Don and Betty Jones. You have always believed in me, even at times when I was quite improbable.
Aahz: I dedicate this book to the Python community. I hated programming until I learned Python (yes, for more than 20 years). I hope this book brings the joy of Python to many people.
This book is also dedicated to the Flying Spaghetti Monster.
Many people have helped us and supported us in writing this book. There are too many to mention all of them by name, so we want to start by thanking all the people we don’t name here — all the family and friends and community who have sustained us.
Paula Anderson, Naomi Tilsen, Joyce Wermont, and Maggie Young provided much appreciated emotional support.
Thanks to our editors at Wiley:
Acquisition editors Terri Varveris, Tiffany Franklin, and Kyle Looper, who shepherded these first-time For Dummies authors with just the right balance of patience and whip-cracking.
Project editor Pat O’Brien, who provided invaluable assistance in e-mails that were often time-stamped with hours well past the time any less- dedicated person would have been in bed.
Copy editor Andy Hollandbeck, who improved the book with his keen grasp of the beginner’s mind, light-hearted prose, and Monty Python quotations.
High praise also to the production staff at Wiley, who are doing such great work with an extraordinarily complex and flexible book design.
Our technical editor, David Goodger, vastly improved our book with his edits and suggestions.
We feel fortunate to have our agents, David Fugate, who supported us expertly and patiently through the lengthy acquisition process, and Carole McClendon, who provided support at a critical juncture. Thanks also to the efficient staff at Waterside Productions.
Many people gave us advice and help while we were writing:
Don and Betty Jones provided invaluable advice from the point of view of programming beginners
Aahz’s coworkers at Printra (http://printra.net/), especially Tony Lownds
The community of Python programmers on comp.lang.python and tutor@python.org not only helped Stef learn Python but also tirelessly work every day to promote Python and help make it accessible. This book wouldn’t be possible without them.
The folks who maintain www.python.org and run the Python Software Foundation provide a critical service without which Python would be poorer.
Millions of people volunteer their time and efforts to make the Open Source movement a powerful force for good in the computer industry.
We also want to thank each other. Living together and writing a book is stressful, but we’re glad we did this.
And, of course, none of it would be possible without Guido.
We’re proud of this book; please send us your comments through our online registration form located at www.dummies.com/register/.
Some of the people who helped bring this book to market include the following:
Acquisitions, Editorial, and Media Development
Project Editor: Pat O’Brien
Acquisitions Editor: Kyle Looper
Copy Editor: Andy Hollandbeck
Technical Editor: David Goodger
Editorial Manager: Kevin Kirschner
Media Development Specialists: Angela Denny, Kate Jenkins, Steven Kudirka, Kit Malone
Media Development Coordinator: Laura Atkinson
Media Project Supervisor: Laura Moss
Media Development Manager: Laura VanWinkle
Editorial Assistant: Amanda Foxworth
Sr. Editorial Assistant: Cherie Case
Cartoons: Rich Tennant (www.the5thwave.com)
Composition Services
Project Coordinator: Tera Knapp
Layout and Graphics: Claudia Bell, Denny Hager, Jake Mansfield, Barbara Moore, Barry Offringa, Heather Ryan
Proofreaders: Laura Albert, Susan Moritz, Techbooks
Indexer: Techbooks
Publishing and Editorial for Technology Dummies
Richard Swadley, Vice President and Executive Group Publisher
Andy Cummings, Vice President and Publisher
Mary Bednarek, Executive Acquisitions Director
Mary C. Corder, Editorial Director
Publishing for Consumer Dummies
Diane Graves Steele, Vice President and Publisher
Joyce Pepple, Acquisitions Director
Composition Services
Gerry Fahey, Vice President of Production Services
Debbie Stailey, Director of Composition Services
Title
Introduction
About This Book
Conventions Used in This Book
Foolish Assumptions
How This Book Is Organized
Icons Used in This Book
Where to Go from Here
Part I : Getting Started
Chapter 1: Introducing Python
The Right Tool for the Job
Cooking Up Programs
Chapter 2: Getting Your Hands on the Keyboard: Using Help, Interactive Mode, and IDLE
Two Ways to Interact with Python
Going One-on-One in Interactive Mode
Getting Help
Using Scripts and Modules
IDLE Musings
Chapter 3: Basic Elements and Syntax
Making Names and Storing Values
Data Type Does Matter
Operators Are Standing By
If We May Comment . . .
Oopsies! Understanding Error Messages
Deciphering Code Blocks
Chapter 4: Grand Tour of the Python Language
The spider.py Program
Examining a Python Program
Using Building Blocks
Chapter 5: Working Like a Programmer
The Three Ds
Maintaining Your Programs
Good Program Design Practices
Debugging Strategies
Part II : Building Blocks
Chapter 6: So This String Walks into a Bar
Stringing Them Along
Cat’s Cradle: Indexing and Slicing
Interpolating Between the Lines
Unraveling Unicode
Chapter 7: Counting Your Way to Fun and Profit
Integrating Integers
Floating Along
Imagining Complex Numbers
Using Math Modules
Turning Python into a Calculator with decimal
Chapter 8: Processing Lists and Tuples
Introducing Lists and Tuples
Manipulating Sequence Objects
Listcraft: Methods, Indexes, and Slices
Steering Clear of List Gotcha’s
Building Lists, Stacks, and Queues
Taking Tuples in Hand
Chapter 9: Diving into Dictionaries
Defining the Dictionary
Doodling Around with Dicts
Building Dictionaries
When Only a Dict Will Do
Setting Them Up
Part III : Structures
Chapter 10: Staying in Control
Things to Know about Control Structures
All about Conditions and Comparisons
Feeling Iffy
Staying in the Loop
Choosing Your Loop
Loopy Statements and Functions
Chapter 11: Fun with Functions
I Love Chunky Code
Argument Clinic: Passing Data
What’s in a Namespace
Chapter 12: Building Applications with Modules and Packages
Modular Living: Storing Your Code in Files
Wrapping It Up in a Package
Chapter 13: Getting Classy
Alley-OOP! Some Object-Oriented Programming Concepts
Now Class, for Instance . . .
Making and Calling Classes
Getting Inside the Factory: How Class and Instance Namespaces Interact
Class and Instance Conventions
Inheriting the Farm: Overriding and Extending Classes
When to Go to Class
Chapter 14: Introducing New-Style Classes
An Object’s Object: Intro to New-Style Classes
New Improved Class Features
Island of Dr. MRO
Exploding Your Head with Metaclasses
Chapter 15: Feeling Exceptional
All about Special Handling
Trying Things Out
Raising Your Code to New Levels
Making Your Program Exceptional
Chapter 16: Tackling Some Advanced Features
What’s That Idiom?
What to Do Next: Iterators and Generators
Expression and Comprehension: Listcomps and Genexps
With What, Your Bare Hands? (The Power of ‘with’ Statements)
Making Exceptions for Yourself
Under One Condition
Decorating Your Code
Focusing on Functions
Part IV : Libraries
Chapter 17: Using Python’s Primary Services
Python: Batteries Included
You Get All This! — The __builtin__ Module
But Wait, There’s More — The sys Module
Solving OS Incompatibility — The os and subprocess Modules
Staying on Time with the datetime and time Modules
Checking with the doctest Module
Keeping Track with the logging Module
Chapter 18: Processing Text
A Million Ways to re, You Know That There Are
Strings Disguised as Files
Paragraph Dumplings: Filling and Wrapping Text
Chapter 19: Digging into Disk Data
Shell Game: Copying and Moving Files
Zipping and Unzipping
Sussing Out SQL Databases
Pickling Your Data (And Relishing the Outcome)
Using shelve with DBM-style databases
Chapter 20: Accessing the Internet
Downloading Web Data
Taming the Wild URL
Getting Hip with Hypertext
The Great XML
MIME-ing Success: Managing E-Mail Messages
Simply SMTP
CGI: Gateway to the Web
Part V : The Part of Tens
Chapter 21: Ten Critical Python Idioms
Collecting Globs and Globs of Files
Rolling Dice and Shuffling Cards
Uniquely Ordered Lists
Reversing Your Way to Success
Exceptional Type-Testing
Classes Just for Data
Getting Close Enough with difflib
DSU! DSU! Rah rah DSU!
Simplifying Choices Using Dicts
Singles Going Steady
Chapter 22: Ten Great Resources
The Mothership: www.python.org
The comp.lang.python Newsgroup
Cheese Shop: Online Collection of Python Modules
Random Access Reference at wiki.python.org
The Python Cookbook Web Site
The Latest News
Being a PUG-nosed PIGgie: Local User Groups
Part VI : Appendixes
Appendix A: Getting and Installing Python
Operating Systems
Using Embedded Python
Appendix B: Python Version Differences
Python 2.5
Python 2.4
Python 2.3
Python 2.2
Python 2.1
Python 2.0
Congratulations! You’re ready to discover the easiest-to-read powerful programming language — or maybe the most powerful, easy-to-read programming language. That’s Python, of course.
With Python For Dummies , you can ferret out just a little or a lot. And with Python, you can write a little program that picks a random quote from a file, or you can write a set of programs that runs a complex business.
This book is for you whether you’re a student, you’re a hobbyist, you need to understand more about what your programmer co-workers are talking about, or you’re taking the first steps on a new career path.
Python For Dummies gives you everything you need to get to an advanced-beginner level of Python programming. And it points you to other resources so you can take your Python programming skills even further.
Python For Dummies is a reference book, which means you can read it in any order, and you don’t have to read every chapter or section. However, to some extent, later chapters about more complex Python features rely on information introduced in earlier chapters. So if you don’t understand something you see in a later chapter, go to Chapter 3, or go to the chapter on that feature to find out more. You can also look in the index to find a term or feature you want to know more about.
This book contains Python code examples. All code examples are in monospaced font so they are easy to recognize. Anything that you need to type is also indicated in monospaced font so you know exactly which commas should be typed and which commas are part of the surrounding sentence.
Python interactive mode examples include this prompt: >>> . If you don’t see the prompt, you can assume the code was written in a text editor.
We make the following assumptions about readers of this book:
You know how to use your computer and its operating system.
It’s helpful but not necessary to know how to set environment variables on your computer. It’s also helpful to have a Web browser with access to the Internet.
You have and know how to use a text editor that can produce plain ASCII text or files that end with the .txt extension.
If you don’t have a text editor that can do this, we include instructions for setting up Python’s IDLE programming environment to work with the examples in this book.
You have had a minimal amount of exposure to programming.
We really do mean minimal. If you had a programming class in high school, or wrote a few BASIC programs at one time, or even if you have used HTML tags, that counts.
If you have absolutely no experience with programming, you can still find out plenty from this book, but we recommend that you also look at a book or Web tutorial designed to introduce programming to beginners. You’ll benefit from the extended explanations of some concepts that we don’t have the space to discuss in detail here.
You might have done some programming in another language.
Programming knowledge is not required for this book, but people who have programmed in other languages have their own sets of issues when transitioning to Python, and we provide some material for such people.
You know little to nothing about Python.
If you know Python, this book will still be helpful as a reference or a source of tips and tricks you may not be aware of.
This book gives you an overview of Python; the lowdown about all of its major parts, structures, and libraries; and a glimpse into some more advanced features. You also find out where to go to discover more.
In this part, we introduce Python and situate it among the myriad other programming languages available. Python is good for some things and not for others; you find out which is which. We provide a hands-on introduction to some of Python’s abilities, using its helpful interactive mode and its IDLE programming environment. We briefly describe each of Python’s basic building blocks and show how all these blocks come together by dissecting a working program. We sketch an overview of how professional programmers design programs and debug code and show you how to put these practices to work to make your own programming life easier.
Python has six basic data types and many ways to work with each of them. In this part, we describe how to work with strings (chunks of text), numbers, lists and tuples (both of which store multiple data elements), dictionaries (which associate one element with another), and sets (which always contain unique elements, never duplicates).
Python code usually comes in chunks, both small and big, and each chunk does a particular thing. This part also includes a brief introduction to some advanced features and the new features of Python 2.5.
Python comes with everything you need to write a very powerful program, and other people have already solved lots of programming conundrums for you. Its libraries include primary services such as communication with the operating system, text processing tools, various ways of reading and writing information to disk, and Internet access methods.
All For Dummies books include The Part of Tens. In this part, we give you ten useful but not-so-obvious programming idioms and ten resources where you can find out more about Python.
Here you find instructions on how to install Python and its documentation, as well as a list of new features introduced with each new version of Python since 2.0.
Icons appear throughout the book to indicate special material. Here’s what they mean:
If you want an overview of Python’s history and what it can do, go to Chap- ter 1. If you’re new to Python and want to start working with it right away, go to Chapter 2. If you want a brief overview of all of Python’s building blocks, go to Chapter 3. If you know some Python and you want a refresher or additional info on some of its tools, go to the specific chapters you’re interested in.
In this part . . .
You get an overview of the Python programming language, an introduction to its interactive and developer environment, and a walkthrough of the building blocks that make up Python programs.
Chapter 1 describes the history of Python and all the exciting things it’s being used for today. You find out why computers are both the fastest and dumbest things around. Best of all, you discover why it’s called Python anyway.
Chapter 2 lets you talk to Python via its interactive mode and IDLE environment. You write a few basic programs and find out how to get Python to carry out commands for you, how to get Python to tell you things, and how to import tools that let you do even more.
Chapter 3 introduces you to Python’s data types and code blocks, the chunks you use to build programs.
Chapter 4 shows you a working program. You see how all the chunks of a Python program talk to each other, and you find out something about the design philosophies behind Python programs.
Chapter 5 lets you try on a programmer’s hat to understand how programmers work and why they make the design decisions they do. (Unfortunately, it doesn’t explain the relevance of caffeinated sodas to this process — you’ll have to figure that out for yourself.) There’s also a very useful section on strategies for debugging programs, which is a huge part of every programmer’s job.