Hello! Python
Anthony Briggs
Copyright
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact:
Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email:
orders@manning.com2012 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
User Friendly artwork, characters, and strips used by permission from UserFriendly.Org. All Rights Reserved.
Recognizing the importance of preserving what has been written, it is Mannings policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without elemental chlorine.
| Manning Publications Co.20 Baldwin RoadPO Box 261Shelter Island, NY 11964 | Development editor: Sebastian StirlingTechnical proofreader: Marion NewlevantCopyeditors: June Eding, Tiffany TaylorTypesetter: Marija TudorCover designer: Leslie Haimes |
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 MAL 17 16 15 14 13 12
Brief Table of Contents
Table of Contents
Foreword
When Anthony asked me if I would write a foreword to this book, I thought, Oh, no! Another job! Ill just refuse. But something urged me to at least take a look at the text, which I soon saw was sprinkled with frames from the User Friendly cartoon series (a firm favorite, I am sure, with those few surviving individuals who like me have been working with computers since the days of punched cards and tape). So I thought I would take a look at the manuscript, and found that in 12 short chapters you can learn enough about Python and some of its most popular applications to either get started programming or decide that the programmers life is not for you.
Even the latter conclusion would make the money invested in buying Hello! Python worthwhileif you dont enjoy programming in Python, you are unlikely to enjoy programming at all, in which case you might save yourself the trouble of years spent in a mismatched career.
The book is full of sound practical advice, and nowhere does it try to make pretentious and unbelievable claims. It is a solid work that will, I am sure, introduce many more people who might not currently think of themselves as programmers to the Python language.
I hope that Hello! Python will give a broad audience new insights into programming and the fascinating world of information technology. In the absence of sensible computer science education in secondary schooling (which many U.S. states appear unable to afford at present), this book is appealing enough to draw students to the subject. By the time they discern the educational purpose they will be so engaged with the text that they will digest the whole volume.
STEVE HOLDEN
PRESIDENT, THE OPEN BASTION
Preface
When I was first asked to write Hello! Python, I didnt want to write just another introductory bookI wanted to write something different. The programming books that Ive read in the past have often been just a laundry list of features: a list can have things in it, and you can call len(mylist) to find out exactly how many things, .pop() to chop an element from the end, .append() to add ... There you go, thats all you need to know about lists, now on to the next feature. If youre shown a program, its usually either a trivial few lines or a couple of chapters tacked on to the end of the book as an afterthought.
Then I thought back to how I first learned to program. I didnt read an entire programming book from cover to cover and then write a program after I knew everything there was to know. Instead I started with a goal, something that I wanted to do, and worked toward it, figuring things out as I went. I read programming books from time to time, but really only to figure out the bits I was stuck on. When I was done, my programs werent particularly elegant or fast, but they were mineI knew how they worked, and they solved a real problem that I was having.
Fast-forward to today, and my programs are elegant and fast, for the most part. And most of the really good programmers I know have learned to program the same way. In Hello! Python, Ive tried to re-create that process, but speeded up, with all the things Ive learned about programming and the pitfalls Ive encountered. Every chapter (except the first and last) includes a practical program at its core to illustrate either a particular Python feature or a libraryoften several. Some of them are fun, some of them are useful, but there are no boring beginning chapters where you learn, in excruciating detail, every feature of a list or dictionaryor, worse, learn how Python adds numbers together.
Instead, youll watch a program being written and learn about Python features as you need them, not before. Several of the chapters build on previous ones, so youll learn how to extend existing programs to add new features and keep their design under controlessential if youre going to be writing programs of any scope. The book also explores several different styles of program, from simple scripts, to object-oriented programs, to event-based games.
The idea is to provide a book thats differentthat lets you begin writing programs from the first chapter and learn how to use Pythons features by seeing them used in action. I hope this is the sort of book that will help people really understand how to use Python.
Acknowledgments
First Id like to thank Lyndall, my beautiful wife, for being supportive and giving me the time I needed to write this book. It took much longer than we originally thought, but her enthusiasm was unwavering, despite the many weekends that I spent cloistered in the study.
Second, Id like to thank the team at Manning: my editor, Sebastian Stirling, for his suggestions and experience; June Eding and Tiffany Taylor for the final editing, proofreading, and push across the line; Karen Tegt-meyer for organizing the whole thing; and Michael Stephens for helping me develop the initial concept of the book.
Third, Id like to acknowledge J.D. Illiad Fraser of User Friendly for letting Manning use the User Friendly cartoons in the Hello! Series and allowing me to put my own words in the characters mouths in this book.
Next, Id like to thank all of my beta testers who helped find errors Daniel Hadson, Eldar Marcussen, William Taylor, David Hepworth, and Tony Haigas well as everyone in the MEAP program who offered advice and criticism or discovered errors.
Finally, I would like to thank the following peer reviewers who provided invaluable feedback on the manuscript at various stages of its development: Tray Skates, Curtis Miller, Joe Hoover, Michael R. Bain, Francesco Goggi, Mike Stok, Michael R. Head, Cheryl M. Davis, Daniel Bretoi, Amos Bannister, Rob Allen, Dr. John Grayson, William Z. Taylor, Munch Paulson, David Hepworth, Eldar Marcussen, Daniel Hadson, Tony Niemann, Paolo Corti, Edmon Begoli, Lester Lobo, Robby OConnor, and Sopan Shewale. And special thanks to Marion Newlevant for her careful technical review of the final manuscript during production and to Steve Holden for agreeing to write the foreword to my book.