Join the p2p.wrox.com. Wrox Programmer to Programmer. Beginning. Python. Using Python 2.6 and Python 3.1. James Payne

Similar documents
Jeff Duntemann. Assembly Language. Programming with Linux. Step by Step THIRD EDITION

TELLING STORIES A SHORT PATH TO WRITING BETTER SOFTWARE REQUIREMENTS

New. On Smith s A Tree Grows in Brooklyn. Over 300 Titles Available and 100 Million Sold. 24-hour-a-day downloads at cliffsnotes.

Excel Formulas and Functions FOR. DUMmIES. by Ken Bluttman and Peter G. Aitken

Visual Basic 2008 FOR. DUMmIES. by Bill Sempf

ebay For Seniors Learn to: Marsha Collier Making Everything Easier! Get your own ebay account for buying and selling

Tumblr FOR PORTABLE EDITION

Do s and Don ts to Protect Your Financial Life BEN STEIN

Visual Guide to Elliott Wave Trading

HYPNOTIC WRITING How to Seduce and Persuade Customers with Only Your Words JOE VITALE John Wiley & Sons, Inc.

Digital Filmmaking For Kids

my lie MEREDITH MARAN A True Story of False Memory Best-selling author of What It s Like to Live Now and Class Dismissed

ANN HANDLEY AND C.C. CHAPMAN

Declutter Your Life How Outer Order Leads to Inner Calm

SURVEYING (VOLUME II)

3DTV CONTENT CAPTURE, ENCODING AND TRANSMISSION

Math For Real Life FOR

SharePoint 2010 BOOKS. Microsoft. Emer McKenna Kevin Laahs Veli-Matti Vanamo. Making Everything Easier!

Jeremy Baras

Learn to: Paulina Christensen, PhD Anne Fox Wendy Foster. 2nd Edition

Arabic. DUMmIES. by Amine Bouchentouf FOR

Home Staging. DUMmIES

11 BOOKS. Starting an Online Business. Making Everything Easier! 2nd Edition. Inside your Google AdWords gift card worth $25

JOSHI s Process Equipment Design

Environmental Impact of Fertilizer on Soil and Water

SAMPLE LESSON BUY EBOOK NOW. Learn to Read Music! Buy Ebook NOW 1

Teaching and the Internet: The Application of Web Apps, Networking, and Online Tech for Chemistry Education

Being Agile. Your Roadmap to Successful Adoption of Agile. Mario E. Moreira

What s New in Visual FoxPro 7.0

Radiology for Undergraduate Finals and Foundation Years

3DTV CONTENT CAPTURE, ENCODING AND TRANSMISSION

SAP Edge Services, cloud edition Edge Services Overview Guide Version 1802

Enjoy Writing. your Science Thesis or Dissertation!

ISBN-13. Everything you need to know about the ISBN-13 transition. The Book Industry Study Group, Inc. Brought to you by.

_FM 7/22/09 10:10 AM Page 1 COLLABORATING. with SharePoint. Carey Cole

User s manual. Digitales TV module for NAVIGON English (United Kingdom)

Beet-Sugar Handbook ffirs.indd i ffirs.indd i 10/12/2006 3:01:08 PM 10/12/2006 3:01:08 PM

Introduction to Primo

Early Power and Transport

Move & Store Cart CONTENTS. Assembly Instructions

KIDS INVENTING! A Handbook for Young Inventors SUSAN CASEY

Kamai Hybrid TV Media Player

MOB501. SAP Omnichannel Banking 8.3 SP01 PL03 Development COURSE OUTLINE. Course Version: 03 Course Duration: 4 Day(s)

MetaLib and Your Resources

Publishing with University of Manitoba Press

WORKSHOP GUIDELINES Casting Director Workshops USE OF THESE GUIDELINES IS FOR GUIDANCE ONLY. IT DOES NOT CONSTITUTE CGA ENDORSEMENT OF ANY WORKSHOP

THE ORGANIZATIONAL ALIGNMENT HANDBOOK. A Catalyst for Performance Acceleration

See for options on how to legitimately share published articles.

Journal of Material Science and Mechanical Engineering (JMSME)

Little Book Big Profits Series

New ILS Data Delivery Guidelines

Negotiation Exercises for Journal Article Publishing Contracts and Scholarly Monograph Publishing Contracts

The. Craft of. Editing

Reference Release Definition for ConnMO

This document is a preview generated by EVS

Instruction Manual. AVT-5841 Component Video And Stereo Audio Switcher

Amber, Resinite, and Fossil Resins

Epoxy Resin Chemistry II

2018 Student Film Festival Submission Rules and Guidelines

Device Management Requirements

Non-Fiction Self-Help Book Template

CHILDREN S ESL CURRICULUM: LEARNING ENGLISH WITH LAUGHTER

Human Rights Violation in Turkey

Letters.org. SORRY LETTER TO AUNT. Included: Sorry Letter to Aunt

ALEPH Z39.50 Client Conformance to U.S. National Z39.50 Profile (ANSI/NISO Z ) Version and Later

Well, my name is Iggy, said the second toe. And my name is Ziggy, said the fourth toe.

Polymer Technology Dictionary

Efficient Organic Light-Emitting Diodes (OLEDs)

DM DiagMon Architecture

Immanuel Kant s Critique of Pure Reason

ELECTRIC POWER SYSTEMS

User Manual. Model 1351 DVI Repeater with HDCP

Luminescence Applications

November Ex Libris Certified Third-Party Software and Security Patch Release Notes

Paul M. Gauthier. Lectures on Several Complex

Clean Code.

Intellectuals and Politics in Post-War France

LITERARY CRITICISM from Plato to the Present

Practical Recording Techniques. Fifth Edition

Welcome to Verde. Copyright Statement

See Education Licence B: Statutory Text and Artistic Licence for further information

The digital revolution and the future of scientific publishing or Why ERSA's journal REGION is open access

Flue Gas Desulfurization

Music is Fun! Grade 5. Written by Linda Ann Hopley Illustrated by S&S Learning Materials

Dns For Dummies Download or Read Online ebook dns for dummies in PDF Format From The Best User Guide Database

Performance Anxiety in Media Culture

Xpedition Layout for Package Design. Student Workbook

Table of Contents. Acknowledgements About the Authors Chapter 1 Introduction... 6

PUBLISHING 101: NAVIGATING THE ACADEMIC PUBLISHING PROCESS SURVIVAL SKILLS FOR GRADUATE STUDENTS MISSISSIPPI STATE UNIVERSITY LIBRARIES

SAP Edge Services Edge Services Overview Guide Version 1711

Publish or Perish: Scheduling Challenges in the Publishing Industry. Matthew J. Drake with Beate Klingenberg and David Gavin, Marist College

Buy The Complete Version of This Book at Booklocker.com:

Seven Languages in Seven Weeks

Instruction Manual AP-536 HDMI Audio Extractor

CHARLOTTE MECKLENBURG PUBLIC ACCESS CORPORATION

Guide to InTouch HMI Documentation Invensys Systems, Inc.

PART 7 Other Forms of Communication

Firmware Update Management Object Architecture

Author submission guidelines

The Reflected. S.D. Warren Yearbook Advertising

Transcription:

Join the discussion @ p2p.wrox.com Wrox Programmer to Programmer Beginning Python Using Python 2.6 and Python 3.1 James Payne

Programmer to Programmer Get more out of wrox.com Interact Take an active role online by participating in our P2P forums @ p2p.wrox.com Wrox Online Library Hundreds of our books are available online through Books24x7.com Wrox Blox Download short informational pieces and code to keep you up to date and out of trouble! Join the Community Sign up for our free monthly newsletter at newsletter.wrox.com Browse Ready for more Wrox? We have books and e-books available on.net, SQL Server, Java, XML, Visual Basic, C#/ C++, and much more! Contact Us. We always like to get feedback from our readers. Have a book idea? Need community support? Let us know by e-mailing wrox-partnerwithus@wrox.com

Beginning Python : Using Python 2.6 and Python 3.1 Introduction...............................................xxvii Part I: Dipping Your Toe into Python Chapter 1: Programming Basics and Strings.......................... 3 Chapter 2: Numbers and Operators............................... 15 Chapter 3: Variables Names for Values........................... 31 Part II: Python Language and the Standard Library Chapter 4: Making Decisions... 51 Chapter 5: Functions... 71 Chapter 6: Classes and Objects... 93 Chapter 7: Organizing Programs... 111 Chapter 8: Files and Directories... 127 Chapter 9: Other Features of the Language... 143 Chapter 10: Building a Module... 157 Chapter 11: Text Processing... 189 Part III: Putting Python to Work Chapter 12: Testing... 207 Chapter 13: Writing a GUI with Python... 227 Chapter 14: Accessing Databases... 239 Chapter 15: Using Python for XML... 265 Chapter 16: Network Programming... 287 Continues

Chapter 17: Extension Programming with C... 337 Chapter 18: Numerical Programming... 367 Chapter 19: An Introduction to Django... 387 Chapter 20: Web Applications and Web Services... 407 Chapter 21: Integrating Java with Python... 481 Part IV: Appendices 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

Beginning Python

Beginning Python Using Python 2.6 and Python 3.1 James Payne Wiley Publishing, Inc.

Beginning Python : Using Python 2.6 and Python 3.1 Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright 2010 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-0-470-41463-7 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 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 Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions. 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 here from. The fact that an organization or Web site 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 Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read. For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. 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: 2009936814 Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, 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. Python is a registered trademark of Python Software Foundation. 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.

To my patient and loving wife, Whitney, who believed in me before I did. And to my parents, Ronnie and Sharon Payne, who raised me to believe I could do anything I put my mind to, even when I told them I wanted to be Santa Claus. For my brother, Ron, who read my work even when it was bad, Dorjan, Eric, Clem, and Nick because they know things about me and will tell them if I don t include them.

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.

Executive Editor Carol Long Project Editor Ed Connor Technical Editor Chris McAvoy Production Editors Amy Weintraub and Tim Tate Copy Editor Kim Cofer Editorial Director Robyn B. Siesky Editorial Manager Mary Beth Wakefield Marketing Manager David Mayhew Credits Production Manager Tim Tate Vice President and Executive Group Publisher Richard Swadley Vice President and Executive Publisher Barry Pruett Associate Publisher Jim Minatel Project Coordinator, Cover Lynsey Stanford Proofreaders Scott Klemp and Kristy Eldredge, Word One Indexer Ron Strauss Cover Image istockphoto.com/frank_u

Acknowledgments I would like to acknowledge the gang at Developer Shed: Jack and Jay Kim, whose constant hatred is an inspiration, Charles Fagundes, who made me an editor and not just a writer, Keith Lee, who provided coding support, and a special thanks to Jenny Ruggieri, who got me the job that got me this book. Lastly, I d like to thank all of the people that worked on the previous editions of this book for laying the groundwork, and the Editors Carol Long, Jenny Watson, Ed Connor, and Chris McAvoy who helped me meet deadlines no matter how much I didn t want to. I would also like to acknowledge Guido Von Rossum, without whom there would be no language to write about.