ebook img

Programming in Python 3: A Complete Introduction to the Python Language PDF

644 Pages·2010·2.54 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Programming in Python 3: A Complete Introduction to the Python Language

Programming in Python 3 A Complete Introduction to the Python Language Second Edition ptg Mark Summerfield UpperSaddleRiver,NJ·Boston·Indianapolis·SanFrancisco pNewYork·Toronto·Montreal·London·Munich·Paris·Madridp Capetown·Sydney·Tokyo·Singapore·MexicoCity From the Library of STEPHEN EISEMAN Manyofthedesignationsusedbymanufacturersandsellerstodistinguishtheirproductsareclaimed astrademarks. Where those designationsappear in thisbook,and the publisher wasawareof a trademarkclaim,thedesignationshavebeenprintedwithinitialcapitallettersorinallcapitals. Theauthorandpublisherhavetakencareinthepreparationofthisbook,butmakenoexpressedor impliedwarrantyof anykindandassumenoresponsibilityforerrorsoromissions. Noliabilityis assumedforincidentalorconsequentialdamagesinconnectionwithorarisingoutoftheuseofthe informationorprogramscontainedherein. Thepublisheroffersexcellentdiscountsonthisbookwhenorderedinquantityforbulkpurchases orspecialsales,whichmayincludeelectronicversionsand/orcustomcoversandcontentparticular to your business,traininggoals,marketing focus,and branding interests. For more information, pleasecontact: U.S.CorporateandGovernmentSales (800)382-3419 [email protected] ForsalesoutsidetheUnitedStates,pleasecontact: InternationalSales [email protected] VisitusontheWeb:informit.com/aw Libraryof CongressCataloging-in-PublicationData Summerfield,Mark. ProgramminginPython3:acompleteintroductiontothePythonlanguage/Mark Summerfield.—2nded. p. cm. ptg Includesbibliographicalreferencesandindex. ISBN978-0-321-68056-3 (pbk.:alk. paper) 1. Python(Computerprogramlanguage) 2. Object-orientedprogramming(Computerscience) I. Title. QA76.73.P98S86 2010 005.13’3—dc22 2009035430 Copyright©2010PearsonEducation,Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright,andpermissionmustbeobtainedfromthepublisherpriortoanyprohibitedreproduction, storageinaretrievalsystem,ortransmissioninanyformorbyanymeans,electronic,mechanical, photocopying,recording,orlikewise. Forinformationregardingpermissions,writeto: PearsonEducation,Inc. RightsandContractsDepartment 501BoylstonStreet,Suite900 Boston,MA02116 Fax:(617)671-3447 ISBN-13: 978-0-321-68056-3 ISBN-10: 0-321-68056-1 TextprintedintheUnitedStatesonrecycledpaperatRRDonnelleyinCrawfordsville,Indiana. Firstprinting,November2009 From the Library of STEPHEN EISEMAN Inmemory of FrancoRabaiotti 1961–2001 ptg From the Library of STEPHEN EISEMAN Contents at a Glance Listof Tables ...................................................... xv Introduction ...................................................... 1 Chapter1. RapidIntroductiontoProceduralProgramming ... 9 Chapter2. DataTypes ............................................ 51 Chapter3. CollectionDataTypes ................................ 107 Chapter4. ControlStructuresandFunctions ................... 159 Chapter5. Modules ...............................................195 Chapter6. Object-OrientedProgramming ...................... 233 Chapter7. FileHandling ......................................... 287 ptg Chapter8. AdvancedProgrammingTechniques ................ 339 Chapter9. Debugging,Testing,andProfiling ................... 413 Chapter10. ProcessesandThreading ........................... 439 Chapter11. Networking ..........................................457 Chapter12. DatabaseProgramming ............................. 475 Chapter13. RegularExpressions ................................ 489 Chapter14. IntroductiontoParsing ............................. 513 Chapter15. IntroductiontoGUIProgramming ................. 569 Epilogue ...........................................................595 SelectedBibliography ............................................ 597 Index .............................................................. 599 www.qtrac.eu/py3book.html From the Library of STEPHEN EISEMAN Contents Listof Tables ..................................................... xv Introduction ...................................................... 1 Chapter1. RapidIntroductiontoProceduralProgramming ... 9 CreatingandRunningPythonPrograms ........................ 9 Python’s“BeautifulHeart” ...................................... 14 Piece#1:DataTypes ....................................... 14 Piece#2:ObjectReferences ................................. 16 Piece#3:CollectionDataTypes ............................. 18 Piece#4:LogicalOperations ................................ 21 Piece#5:ControlFlowStatements .......................... 26 Piece#6:ArithmeticOperators .............................. 30 Piece#7:Input/Output ..................................... 33 Piece#8:CreatingandCallingFunctions .................... 36 ptg Examples ...................................................... 39 bigdigits.py ................................................ 39 generate_grid.py ........................................... 42 Summary ...................................................... 44 Exercises ...................................................... 47 Chapter2. DataTypes ........................................... 51 IdentifiersandKeywords ....................................... 51 IntegralTypes.................................................. 54 Integers ................................................... 54 Booleans ................................................... 58 Floating-PointTypes ........................................... 58 Floating-PointNumbers .................................... 59 ComplexNumbers ......................................... 62 DecimalNumbers .......................................... 63 Strings......................................................... 65 ComparingStrings ......................................... 68 SlicingandStridingStrings ................................ 69 StringOperatorsandMethods .............................. 71 ix From the Library of STEPHEN EISEMAN StringFormattingwiththestr.format()Method .............. 78 CharacterEncodings ....................................... 91 Examples ...................................................... 94 quadratic.py ............................................... 94 csv2html.py ................................................ 97 Summary ...................................................... 102 Exercises ...................................................... 104 Chapter3. CollectionDataTypes ................................ 107 SequenceTypes ................................................ 107 Tuples ..................................................... 108 NamedTuples ............................................. 111 Lists....................................................... 113 SetTypes ...................................................... 120 Sets ....................................................... 121 FrozenSets ................................................ 125 MappingTypes ................................................. 126 Dictionaries................................................ 126 DefaultDictionaries ........................................ 135 OrderedDictionaries ....................................... 136 IteratingandCopyingCollections ............................... 138 ptg IteratorsandIterableOperationsandFunctions ............. 138 CopyingCollections ........................................ 146 Examples ...................................................... 148 generate_usernames.py .................................... 149 statistics.py ................................................ 152 Summary ...................................................... 156 Exercises ...................................................... 158 Chapter4. ControlStructuresandFunctions ................... 159 ControlStructures.............................................. 159 ConditionalBranching ..................................... 159 Looping .................................................... 161 ExceptionHandling ............................................ 163 CatchingandRaisingExceptions ........................... 163 CustomExceptions ......................................... 168 CustomFunctions .............................................. 171 NamesandDocstrings...................................... 176 ArgumentandParameterUnpacking ....................... 177 x From the Library of STEPHEN EISEMAN AccessingVariablesintheGlobalScope ..................... 180 LambdaFunctions ......................................... 182 Assertions ................................................. 183 Example:make_html_skeleton.py ............................... 185 Summary ...................................................... 191 Exercise ....................................................... 192 Chapter5. Modules .............................................. 195 ModulesandPackages .......................................... 195 Packages .................................................. 199 CustomModules ........................................... 202 Overviewof Python’sStandardLibrary.......................... 212 StringHandling ........................................... 213 Command-LineProgramming .............................. 214 MathematicsandNumbers ................................. 216 TimesandDates ........................................... 216 AlgorithmsandCollectionDataTypes....................... 217 FileFormats,Encodings,andDataPersistence............... 219 File,Directory,andProcessHandling ........................ 222 NetworkingandInternetProgramming ..................... 225 XML....................................................... 226 ptg OtherModules ............................................. 228 Summary ...................................................... 230 Exercise ....................................................... 231 Chapter6. Object-OrientedProgramming ...................... 233 TheObject-OrientedApproach .................................. 234 Object-OrientedConceptsandTerminology .................. 235 CustomClasses ................................................ 238 AttributesandMethods .................................... 238 InheritanceandPolymorphism ............................. 243 UsingPropertiestoControlAttributeAccess ................ 246 CreatingCompleteFullyIntegratedDataTypes ............. 248 CustomCollectionClasses ...................................... 261 CreatingClassesThatAggregateCollections ................ 261 CreatingCollectionClassesUsingAggregation .............. 269 CreatingCollectionClassesUsingInheritance............... 276 Summary ...................................................... 283 Exercises ...................................................... 285 xi From the Library of STEPHEN EISEMAN Chapter7. FileHandling ......................................... 287 WritingandReadingBinaryData ............................... 292 PickleswithOptionalCompression.......................... 292 RawBinaryDatawithOptionalCompression ............... 295 WritingandParsingTextFiles .................................. 305 WritingText ............................................... 305 ParsingText ............................................... 307 ParsingTextUsingRegularExpressions .................... 310 WritingandParsingXMLFiles ................................. 312 ElementTrees ............................................. 313 DOM(DocumentObjectModel) ............................. 316 ManuallyWritingXML..................................... 319 ParsingXMLwithSAX(SimpleAPIforXML) ............... 321 RandomAccessBinaryFiles .................................... 324 AGenericBinaryRecordFileClass .......................... 324 Example:TheBikeStockModule’sClasses ................... 332 Summary ...................................................... 336 Exercises ...................................................... 337 Chapter8. AdvancedProgrammingTechniques ................ 339 FurtherProceduralProgramming ............................... 340 ptg BranchingUsingDictionaries............................... 340 GeneratorExpressionsandFunctions ....................... 341 DynamicCodeExecutionandDynamicImports.............. 344 LocalandRecursiveFunctions .............................. 351 FunctionandMethodDecorators............................ 356 FunctionAnnotations ...................................... 360 FurtherObject-OrientedProgramming .......................... 363 ControllingAttributeAccess ................................ 363 Functors ................................................... 367 ContextManagers.......................................... 369 Descriptors ................................................ 372 ClassDecorators ........................................... 378 AbstractBaseClasses ...................................... 380 MultipleInheritance ....................................... 388 Metaclasses ................................................ 390 Functional-StyleProgramming ................................. 395 PartialFunctionApplication ................................ 398 xii From the Library of STEPHEN EISEMAN Coroutines ................................................. 399 Example:Valid.py .............................................. 407 Summary ...................................................... 410 Exercises ...................................................... 411 Chapter9. Debugging,Testing,andProfiling ................... 413 Debugging ..................................................... 414 DealingwithSyntaxErrors................................. 414 DealingwithRuntimeErrors ............................... 415 ScientificDebugging ....................................... 420 UnitTesting ................................................... 425 Profiling ....................................................... 432 Summary ...................................................... 437 Chapter10. ProcessesandThreading ........................... 439 UsingtheMultiprocessingModule .............................. 440 UsingtheThreadingModule .................................... 444 Example:AThreadedFindWordProgram ................... 446 Example:AThreadedFindDuplicateFilesProgram ......... 449 Summary ...................................................... 454 Exercises ...................................................... 455 ptg Chapter11. Networking ......................................... 457 CreatingaTCPClient .......................................... 458 CreatingaTCPServer ......................................... 464 Summary ...................................................... 471 Exercises ...................................................... 471 Chapter12. DatabaseProgramming............................. 475 DBMDatabases ................................................ 476 SQLDatabases................................................. 480 Summary ...................................................... 487 Exercise ....................................................... 488 Chapter13. RegularExpressions ................................ 489 Python’sRegularExpressionLanguage .......................... 490 CharactersandCharacterClasses .......................... 490 Quantifiers ................................................ 491 GroupingandCapturing ................................... 494 AssertionsandFlags ....................................... 496 TheRegularExpressionModule................................. 499 xiii From the Library of STEPHEN EISEMAN

Description:
A Fully Revised Edition Featuring New Material on Coroutines, Debugging, Testing, Parsing, String Formatting, and More   Python 3 is the best version of the language yet: It is more powerful, convenient, consistent, and expressive than ever before. Now, leading Python programmer Mark Summerfield de
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.