Core Python Programming , Second Edition
Wesley J. Chun
Upper Saddle River, NJ Boston Indianapolis San Francisco New York
Toronto Montreal London Munich Paris Madrid Capetown
Sydney Tokyo Singapore Mexico City
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419
For sales outside the United States, please contact:
International Sales
Visit us on the Web: informit.com/ph
Library of Congress Cataloging-in-Publication Data
Chun, Wesley.
Core Python programming / Wesley J. Chun. 2nd ed.
p. cm.
Includes bibliographical references and index.
ISBN 0-13-226993-7 (pbk. : alk. paper)
1. Python (Computer program language) I. Title.
QA76.73.P98C48 2006
005.13'3dc22
2006019559
Copyright 2007 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, write to:
Pearson Education, Inc.
Rights and Contracts Department
501 Boylston Street, Suite 900
Boston, MA 02116
Fax: (617) 671-3447
ISBN 0-13-226993-7
Reprinted with corrections, April 2009
Text printed in the United States on recycled paper at Courier in Stoughton, Massachusetts.
Fifth printing, April 2009
To my parents,
who taught me that everybody is different.
And to my wife,
who lives with someone who is different.
Praise for Core Python Programming
The long-awaited second edition of Wesley Chuns Core Python Programming proves to be well worth the waitits deep and broad coverage and useful exercises will help readers learn and practice good Python.
Alex Martelli, author of Python in a Nutshell and editor of Python Cookbook
There has been lot of good buzz around Wesley Chuns Core Python Programming . It turns out that all the buzz is well earned. I think this is the best book currently available for learning Python. I would recommend Chuns book over Learning Python (OReilly), Programming Python (OReilly), or The Quick Python Book (Manning).
David Mertz, Ph.D., IBM DeveloperWorks
I have been doing a lot of research [on] Python for the past year and have seen a number of positive reviews of your book. The sentiment expressed confirms the opinion that Core Python Programming is now considered the standard introductory text.
Richard Ozaki, Lockheed Martin
Finally, a book good enough to be both a textbook and a reference on the Python language now exists.
Michael Baxter, Linux Journal
Very well written. It is the clearest, friendliest book I have come across yet for explaining Python, and putting it in a wider context. It does not presume a large amount of other experience. It does go into some important Python topics carefully and in depth. Unlike too many beginner books, it never condescends or tortures the reader with childish hide-and-seek prose games. [It] sticks to gaining a solid grasp of Python syntax and structure.
http://python.org bookstore Web site
[If] I could only own one Python book, it would be Core Python Programming by Wesley Chun. This book manages to cover more topics in more depth than Learning Python but includes it all in one book that also more than adequately covers the core language. [If] you are in the market for just one book about Python, I recommend this book. You will enjoy reading it, including its wry programmers wit. More importantly, you will learn Python. Even more importantly, you will find it invaluable in helping you in your day-to-day Python programming life. Well done, Mr. Chun!
Ron Stephens, Python Learning Foundation
I think the best language for beginners is Python, without a doubt. My favorite book is Core Python Programming.
s003apr, MP3Car.com Forums
Personally, I really like Python. Its simple to learn, completely intuitive, amazingly flexible, and pretty darned fast. Python has only just started to claim mindshare in the Windows world, but look for it to start gaining lots of support as people discover it. To learn Python, Id start with Core Python Programming by Wesley Chun.
Bill Boswell, MCSE, Microsoft Certified Professional Magazine Online
If you learn well from books, I suggest Core Python Programming. It is by far the best Ive found. Im a Python newbie as well and in three months time Ive been able to implement Python in projects at work (automating MSOffice, SQL DB stuff, etc.).
ptonman, Dev Shed Forums
Python is simply a beautiful language. Its easy to learn, its cross-platform, and it works. It has achieved many of the technical goals that Java strives for. A one-sentence description of Python would be: All other languages appear to have evolved over timebut Python was designed. And it was designed well. Unfortunately, there arent a large number of books for Python. The best one Ive run across so far is Core Python Programming .
Chris Timmons, C. R. Timmons Consulting
If you like the Prentice Hall Core series, another good full-blown treatment to consider would be Core Python Programming . It addresses in elaborate concrete detail many practical topics that get little, if any, coverage in other books.
Mitchell L Model, MLM Consulting
Prentice Hall Core Series
Core J2EE Patterns, Second Edition, Alur/Malks/Crupi
Core PHP Programming, Third Edition, Atkinson/Suraski
Core Lego Mindstorms, Bagnall
Core Python Programming, Second Edition, Chun
Core Internet Application Development with ASP.NET 2.0, Connolly
Core JSTL, Geary
Core JavaServer Faces, Second Edition, Geary/Horstmann
Core Web Programming, Second Edition, Hall/Brown
Core Servlets and JavaServer Pages,Volume 1: Core Technologies, Second Edition, Hall/Brown
Core Servlets and JavaServer Pages,Volume 2:Advanced Technologies, Second Edition, Hall/Brown
Core Java 2, Volume IFundamentals, Eighth Edition, Horstmann/Cornell
Core Java 2, Volume IIAdvanced Features, Eighth Edition, Horstmann/Cornell