a python book: beginning python, advanced python, and python

Download A Python Book: Beginning Python, Advanced Python, and Python

Post on 13-Feb-2017

824 views

Category:

Documents

160 download

Embed Size (px)

TRANSCRIPT

  • APythonBook

    APythonBook:BeginningPython,AdvancedPython,andPythonExercises

    Author:

    DaveKuhlmanContact:

    dkuhlman@davekuhlman.orgAddress:

    http://www.davekuhlman.org

    Page1

  • APythonBook

    Revision

    1.3aDate

    December15,2013Copyright

    Copyright(c)2009DaveKuhlman.AllRightsReserved.ThisdocumentissubjecttotheprovisionsoftheOpenSourceMITLicensehttp://www.opensource.org/licenses/mitlicense.php.

    Abstract

    ThisdocumentisaselflearningdocumentforacourseinPythonprogramming.Thiscoursecontains(1)apartforbeginners,(2)adiscussionofseveraladvancedtopicsthatareofinteresttoPythonprogrammers,and(3)aPythonworkbookwithlotsofexercises.

    Page2

  • APythonBook

    Contents1Part1BeginningPython...........................................................................................10

    1.1IntroductionsEtc...................................................................................................101.1.1Resources.......................................................................................................111.1.2AgeneraldescriptionofPython....................................................................121.1.3InteractivePython..........................................................................................15

    1.2Lexicalmatters......................................................................................................151.2.1Lines..............................................................................................................151.2.2Comments......................................................................................................161.2.3Namesandtokens..........................................................................................161.2.4Blocksandindentation..................................................................................161.2.5Docstrings.....................................................................................................171.2.6Programstructure..........................................................................................171.2.7Operators.......................................................................................................181.2.8Alsosee.........................................................................................................191.2.9Codeevaluation.............................................................................................19

    1.3Statementsandinspectionpreliminaries...........................................................201.4Builtindatatypes.................................................................................................21

    1.4.1Numerictypes................................................................................................211.4.2Tuplesandlists..............................................................................................211.4.3Strings............................................................................................................24

    1.4.3.1Thenewstring.formatmethod...............................................................261.4.3.2Unicodestrings......................................................................................27

    1.4.4Dictionaries....................................................................................................291.4.5Files...............................................................................................................321.4.6Otherbuiltintypes........................................................................................35

    1.4.6.1TheNonevalue/type..............................................................................351.4.6.2Booleanvalues.......................................................................................361.4.6.3Setsandfrozensets.................................................................................36

    1.5FunctionsandClassesAPreview......................................................................361.6Statements.............................................................................................................37

    1.6.1Assignmentstatement....................................................................................371.6.2importstatement............................................................................................391.6.3printstatement...............................................................................................411.6.4if:elif:else:statement...................................................................................431.6.5for:statement.................................................................................................441.6.6while:statement.............................................................................................48

    Page3

  • APythonBook

    1.6.7continueandbreakstatements.......................................................................481.6.8try:except:statement.....................................................................................491.6.9raisestatement...............................................................................................511.6.10with:statement.............................................................................................52

    1.6.10.1Writingacontextmanager...................................................................521.6.10.2Usingthewith:statement....................................................................53

    1.6.11del................................................................................................................541.6.12casestatement..............................................................................................55

    1.7Functions,Modules,Packages,andDebugging....................................................551.7.1Functions.......................................................................................................55

    1.7.1.1Thedefstatement...................................................................................551.7.1.2Returningvalues....................................................................................551.7.1.3Parameters..............................................................................................561.7.1.4Arguments..............................................................................................561.7.1.5Localvariables.......................................................................................571.7.1.6Otherthingstoknowaboutfunctions....................................................571.7.1.7Globalvariablesandtheglobalstatement.............................................581.7.1.8Docstringsforfunctions.......................................................................601.7.1.9Decoratorsforfunctions........................................................................60

    1.7.2lambda...........................................................................................................611.7.3Iteratorsandgenerators.................................................................................621.7.4Modules.........................................................................................................67

    1.7.4.1Docstringsformodules.........................................................................681.7.5Packages........................................................................................................68

    1.8Classes...................................................................................................................691.8.1Asimpleclass................................................................................................691.8.2Definingmethods..........................................................................................701.8.3Theconstructor..............................................................................................701.8.4Membervariables..........................................................................................701.8.5Callingmethods.............................................................................................711.8.6Addinginheritance........................................................................................711.8.7Classvariables...............................................................................................721.8.8Classmethodsandstaticmethods.................................................................721.8.9Properties.......................................................................................................741.8.10Interfaces.....................................................................................................751.8.11Newstyleclasses.........................................................................................751.8.12Docstringsforclasses.................................................................................771.8.13Privatemembers..........................................................................................77

    1.9SpecialTasks.........................................................................................................771.9.1Debuggingtools.............................................................................................77

    Page4

  • APythonBook

    1.9.2Fileinputandoutput......................................................................................781.9.3Unittests........................................................................................................80

    1.9.3.1Asimpleexample..................................................................................801.9.3.2Unittestsuites........................................................................................811.9.3.3Additionalunittestfeatures............