Transcription of PYTHON - Programmer Books
1 PYTHON PROJECTSINTRODUCTION ..xxvCHAPTER 1 Reviewing Core PYTHON .. 1 CHAPTER 2 Scripting with PYTHON .. 47 CHAPTER 3 Managing Data .. 103 CHAPTER 4 Building Desktop Applications .. 161 CHAPTER 5 PYTHON on the Web .. 223 CHAPTER 6 PYTHON in Bigger Projects .. 257 CHAPTER 7 Exploring PYTHON s Frontiers .. 285 APPENDIX AAnswers to Exercises .. 303 APPENDIX BPython Standard Modules .. 319 APPENDIX C Useful PYTHON Resources .. 327 REFERENCES .. 331 INDEX ..333 PYTHON ProjectsPython ProjectsLaura Cassell AlanGauldPython ProjectsPublished byJohn Wiley & Sons, Crosspoint BoulevardIndianapolis, IN 2015 by John Wiley & Sons, Inc., Indianapolis, IndianaPublished simultaneously in CanadaISBN: 978-1-118-90866-2 ISBN: 978-1-118-90889-1 (ebk)ISBN: 978-1-118-90919-5 (ebk)Manufactured in the United States of America10 9 8 7 6 5 4 3 2 1No 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 108of 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.
2 Requests to the Publisher for permission should be addressed to thePermissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online of Liability/Disclaimer of Warranty:The publisher and the author make no representations or warranties withrespect to the accuracy or completeness of the contents of this work and specifi cally disclaim all warranties, including without limitation warranties of fi tness 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 workis sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professionalservices.
3 If professional assistance is required, the services of a competent professional person should be sought. Neitherthe publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site isreferred 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 thiswork was written and when it is general information on our other products and services please contact our Customer Care Department within theUnited States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) publishes in a variety of print and electronic formats and by print-on-demand.
4 Some material included with standard print versions of this book may not be included in e- Books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at For more information about Wiley products, of Congress Control Number:2014946683 Trademarks: Wiley, Wrox, the Wrox logo, Programmer to Programmer , and related trade dress are trademarks orregistered trademarks of John Wiley & Sons, Inc. and/or its affi liates, in the United States and other countries, and may not be used without written permission. PYTHON is a registered trademark of PYTHON Software Foundation Corporation. All other trademarks are the property of their respective owners.
5 John Wiley & Sons, Inc., is not associated with anyproduct or vendor mentioned in this my truly great boys Nathan, Ben, and Matt: We will do so many things now that I m not writing. Thank you for all the quiet time while I was writing;you can come out of your rooms now. Laura CassellTo my wife, Heather, for her continued support and patience with my eccentric working hours. Alan GauldABOUT THE AUTHORSLAURA CASSELLhas been poking at code on the web since 1997. She taught herself Perl in the early2000s where she discovered that programming materials were in dire need of an overhaul and thebarrier to entry to teach programming was incredibly high. Thus, her journey to learn programming so she can bring it to other people from Atlanta, GA, Laura founded PyLadies Atlanta, and got her start teaching Pythonand JavaScript for Big Nerd Ranch.
6 She has since moved on to engineering management andcurrently resides in Portland, OR where she manages a team of Pythonistas doing software analyticsfor New Relic, Inc. She still volunteers for teaching and speaking gigs when time GAULDis an enterprise architect with more than 40 years of experience in IT, working mainly in the telecommunications and customer service areas. He has programmed in more than20 languages, producing everything from mainframe billing systems through embedded micro-controllers. For the last 15 years, his preference has been for PYTHON . He is the author of a beginner s book on PYTHON and is co-moderator of the PYTHON -tutor mailing not programming, he likes climbing, backpacking, and skiing. He is also a keenphotographer, artist, and acoustic music enthusiast.
7 He lives in Scotland with his wife, THE TECHNICAL EDITORSALEX BRADBURYis a compiler hacker, Linux geek, and Free Software enthusiast. He has beena long time contributor to the Raspberry Pi project and also co-authored Learning PYTHON withRaspberry Pi. He is currently a researcher at the University of Cambridge Computer Laboratoryand is a co-founder of lowRISC, a non-profi t project to produce a complete open-source System-on-Chip (SoC).TODD SHANDELMAN fondly remembers coding assembly language programs on punch cardsfor IBM System/370 mainframes. After occupying various other ecological niches in software technology over the years (C, C++, and Perl, to name but a few), Todd s best days are now spentcoding pure PYTHON and from the Linux command line.
8 In his spare time he is a professional translator of Russian and Hebrew, specializes in foreign- language typography, and can fi nd his way in the dark around Unicode and UTF-8. Todd earned a Bachelor of Science degree in Business Administration from the University of the State of New York. He lives in Houston, Texas, with his wife and EditorJim MinatelProject EditorJennifer LynnTechnical EditorsAlex BradburyTodd ShandelmanProduction EditorChristine MugnoloCopy EditorKimberly A. CoferProduction ManagerKathleen WisorManager of Content Development and AssemblyMary Beth Wakefi eld Marketing DirectorDavid MayhewMarketing ManagerCarrie SherrillfProfessional Technology & StrategyDirectorBarry PruettBusiness ManagerAmy KniesAssociate PublisherJim MinatelProject Coordinator, CoverPatrick RedmondProofreaderSarah Kaikini, Word One New YorkIndexerJohnna DinseCover DesignerWileyCover Image D LIKE TO GIVE a big thank you to Alan Gauld, who helped put so much of this book together,and to Mary James and Jennifer Lynn for making sure this idea came to fruition.
9 It s a better book because of all of you also to the PYTHON community, you are all the most welcoming community that I ve ever found. You have created a place where people feel welcome and able to approach everyone, experts to novices alike. Keep up the good work and thank you for allowing me to be a part of thiswonderful community. Laura CassellI D LIKE TO GIVE a shout out to Laura Cassell for kick-starting this project, Jennifer Lynn forsteering us through it, and the PYTHON community for their support over the last 15 years. Alan GauldCONTENTSINTRODUCTION xxvCHAPTER 1: REVIEWING CORE PYTHON 1 Exploring the PYTHON language and the Interpreter 2 Reviewing the PYTHON Data Types 3 Numeric Types.
10 Integer and Float 4 The Boolean Type 5 The None Type 6 Collection Types 6 Strings 7 Bytes and ByteArrays 8 Tuples 10 Lists 10 Dictionaries 12 Sets 13 Using PYTHON Control Structures 15 Structuring Your Program 15 Using Sequences, Blocks and Comments 16 Selecting an Execution Path 17 Iteration 18 Handling Exceptions 20 Managing Context 21 Getting Data In and Out of PYTHON 21 Interacting with Users 21 Using Text Files 23 Extending PYTHON 24 Defi ning and Using Functions 24 Generator Functions 26 Lambda Functions 27 Defi ning and Using Classes and Objects 28 Creating and Using Modules and Packages 33 Using and Creating Modules 33 Using and Creating Packages 34 Creating an