1、APythonBookAPythonBook:BeginningPython,AdvancedPython,andPythonExercisesAuthor:DaveKuhlmanContact:dkuhlmandavekuhlman.orgAddress:http:/www.davekuhlman.orgPage1APythonBookRevision1.3aDateDecember15,2013CopyrightCopyright(c)2009DaveKuhlman.AllRightsReserved.ThisdocumentissubjecttotheprovisionsoftheOpe
2、nSourceMITLicensehttp:/www.opensource.org/licenses/mitlicense.php.AbstractThisdocumentisaselflearningdocumentforacourseinPythonprogramming.Thiscoursecontains(1)apartforbeginners,(2)adiscussionofseveraladvancedtopicsthatareofinteresttoPythonprogrammers,and(3)aPythonworkbookwithlotsofexercises.Page2AP
3、ythonBookContents1Part1BeginningPython.101.1IntroductionsEtc.101.1.1Resources.111.1.2AgeneraldescriptionofPython.121.1.3InteractivePython.151.2Lexicalmatters.151.2.1Lines.151.2.2Comments.161.2.3Namesandtokens.161.2.4Blocksandindentation.161.2.5Docstrings.171.2.6Programstructure.171.2.7Operators.181.
4、2.8Alsosee.191.2.9Codeevaluation.191.3Statementsandinspectionpreliminaries.201.4Builtindatatypes.211.4.1Numerictypes.211.4.2Tuplesandlists.211.4.3Strings.241.4.3.1Thenewstring.formatmethod.261.4.3.2Unicodestrings.271.4.4Dictionaries.291.4.5Files.321.4.6Otherbuiltintypes.351.4.6.1TheNonevalue/type.35
5、1.4.6.2Booleanvalues.361.4.6.3Setsandfrozensets.361.5FunctionsandClassesAPreview.361.6Statements.371.6.1Assignmentstatement.371.6.2importstatement.391.6.3printstatement.411.6.4if:elif:else:statement.431.6.5for:statement.441.6.6while:statement.48Page3APythonBook1.6.7continueandbreakstatements.481.6.8
6、try:except:statement.491.6.9raisestatement.511.6.10with:statement.521.6.10.1Writingacontextmanager.521.6.10.2Usingthewith:statement.531.6.11del.541.6.12casestatement.551.7Functions,Modules,Packages,andDebugging.551.7.1Functions.551.7.1.1Thedefstatement.551.7.1.2Returningvalues.551.7.1.3Parameters.56
7、1.7.1.4Arguments.561.7.1.5Localvariables.571.7.1.6Otherthingstoknowaboutfunctions.571.7.1.7Globalvariablesandtheglobalstatement.581.7.1.8Docstringsforfunctions.601.7.1.9Decoratorsforfunctions.601.7.2lambda.611.7.3Iteratorsandgenerators.621.7.4Modules.671.7.4.1Docstringsformodules.681.7.5Packages.681
8、.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.1
9、3Privatemembers.771.9SpecialTasks.771.9.1Debuggingtools.77Page4APythonBook1.9.2Fileinputandoutput.781.9.3Unittests.801.9.3.1Asimpleexample.801.9.3.2Unittestsuites.811.9.3.3Additionalunittestfeatures.831.9.3.4GuidanceonUnitTesting.851.9.4doctest.851.9.5ThePythondatabaseAPI.871.9.6InstallingPythonpack
10、ages.881.10MorePythonFeaturesandExercises.892Part2AdvancedPython.902.1IntroductionPython201(Slightly)AdvancedPythonTopics.902.2RegularExpressions.902.2.1Definingregularexpressions.902.2.2Compilingregularexpressions.912.2.3Usingregularexpressions.912.2.4Usingmatchobjectstoextractavalue.922.2.5Extract
11、ingmultipleitems.932.2.6Replacingmultipleitems.942.3IteratorObjects.962.3.1ExampleAgeneratorfunction.982.3.2ExampleAclasscontainingageneratormethod.1002.3.3ExampleAniteratorclass.1022.3.4ExampleAniteratorclassthatusesyield.1042.3.5ExampleAlistcomprehension.1052.3.6ExampleAgeneratorexpression.1052.4U
12、nitTests.1062.4.1Definingunittests.1062.4.1.1Createatestclass.1062.5ExtendingandembeddingPython.1092.5.1Introductionandconcepts.1092.5.2Extensionmodules.1102.5.3SWIG.1122.5.4Pyrex.1152.5.5SWIGvs.Pyrex.1202.5.6Cython.1202.5.7Extensiontypes.1222.5.8Extensionclasses.1222.6Parsing.1222.6.1Specialpurpose
13、parsers.123Page5欢迎加入非盈利Py t h o n 学习交流编程QQ群783462347,群里免费提供500+本Py t h o n 书籍!APythonBook2.6.2Writingarecursivedescentparserbyhand.1242.6.3Creatingalexer/tokenizerwithPlex.1312.6.4Asurveyofexistingtools.1412.6.5CreatingaparserwithPLY.1412.6.6Creatingaparserwithpyparsing.1482.6.6.1Parsingcommadelimit
14、edlines.1482.6.6.2Parsingfunctors.1492.6.6.3Parsingnames,phonenumbers,etc.1502.6.6.4Amorecomplexexample.1512.7GUIApplications.1532.7.1Introduction.1532.7.2PyGtk.1532.7.2.1Asimplemessagedialogbox.1532.7.2.2Asimpletextinputdialogbox.1562.7.2.3Afileselectiondialogbox.1582.7.3EasyGUI.1602.7.3.1AsimpleEa
15、syGUIexample.1612.7.3.2AnEasyGUIfileopendialogexample.1612.8GuidanceonPackagesandModules.1612.8.1Introduction.1612.8.2ImplementingPackages.1622.8.3UsingPackages.1622.8.4DistributingandInstallingPackages.1622.9EndMatter.1642.9.1AcknowledgementsandThanks.1642.9.2SeeAlso.1643Part3PythonWorkbook.1653.1I
16、ntroduction.1653.2LexicalStructures.1653.2.1Variablesandnames.1653.2.2Linestructure.1673.2.3Indentationandprogramstructure.1683.3ExecutionModel.1693.4BuiltinDataTypes.1703.4.1Numbers.1703.4.1.1Literalrepresentationsofnumbers.1713.4.1.2Operatorsfornumbers.1733.4.1.3Methodsonnumbers.1753.4.2Lists.1753
17、.4.2.1Literalrepresentationoflists.176Page6欢迎加入非盈利Py t h o n 学习交流编程QQ群783462347,群里免费提供500+本Py t h o n 书籍!APythonBook3.4.2.2Operatorsonlists.1783.4.2.3Methodsonlists.1783.4.2.4Listcomprehensions.1803.4.3Strings.1823.4.3.1Characters.1833.4.3.2Operatorsonstrings.1843.4.3.3Methodsonstrings.1853.4.3.4Raw
18、strings.1873.4.3.5Unicodestrings.1883.4.4Dictionaries.1903.4.4.1Literalrepresentationofdictionaries.1903.4.4.2Operatorsondictionaries.1913.4.4.3Methodsondictionaries.1923.4.5Files.1953.4.6Afewmiscellaneousdatatypes.1973.4.6.1None.1973.4.6.2ThebooleansTrueandFalse.1973.5Statements.1983.5.1Assignments
19、tatement.1983.5.2printstatement.2003.5.3if:statementexercises.2013.5.4for:statementexercises.2023.5.5while:statementexercises.2053.5.6breakandcontinuestatements.2063.5.7Exceptionsandthetry:except:andraisestatements.2073.6Functions.2103.6.1Optionalargumentsanddefaultvalues.2113.6.2Passingfunctionsasa
20、rguments.2133.6.3Extraargsandkeywordargs.2143.6.3.1Orderofarguments(positional,extra,andkeywordargs).2163.6.4Functionsandducktypingandpolymorphism.2163.6.5Recursivefunctions.2173.6.6Generatorsanditerators.2193.7Objectorientedprogrammingandclasses.2233.7.1Theconstructor.2243.7.2InheritanceImplementin
21、gasubclass.2253.7.3Classesandpolymorphism.2273.7.4Recursivecallstomethods.2283.7.5Classvariables,classmethods,andstaticmethods.2303.7.5.1Decoratorsforclassmethodandstaticmethod.233Page7欢迎加入非盈利Py t h o n 学习交流编程QQ群783462347,群里免费提供500+本Py t h o n 书籍!APythonBook3.8AdditionalandAdvancedTopics.2343.8.1Dec
22、oratorsandhowtoimplementthem.2343.8.1.1Decoratorswitharguments.2353.8.1.2Stackeddecorators.2363.8.1.3Morehelpwithdecorators.2383.8.2Iterables.2393.8.2.1AfewpreliminariesonIterables.2393.8.2.2Morehelpwithiterables.2403.9ApplicationsandRecipes.2403.9.1XMLSAX,minidom,ElementTree,Lxml.2413.9.2Relational
23、databaseaccess.2493.9.3CSVcommaseparatedvaluefiles.2553.9.4YAMLandPyYAML.2563.9.5Json.2584Part4GeneratingPythonBindingsforXML.2604.1Introduction.2604.2Generatingthecode.2614.3UsingthegeneratedcodetoparseandexportanXMLdocument.2634.4Somecommandlineoptionsyoumightwanttoknow.2634.5Thegraphicalfrontend.
24、2644.6Addingapplicationspecificbehavior.2654.6.1Implementingcustomsubclasses.2654.6.2UsingthegeneratedAPIfromyourapplication.2664.6.3Acombinedapproach.2674.7Specialsituationsanduses.2694.7.1Generic,typeindependentprocessing.2694.7.1.1Step1generatethebindings.2704.7.1.2Step2addapplicationspecificcode
25、.2704.7.1.3Step3writeatest/driverharness.2744.7.1.4Step4runthetestapplication.2764.8Somehints.2764.8.1ChildrendefinedwithmaxOccursgreaterthan1.2764.8.2Childrendefinedwithsimplenumerictypes.2774.8.3Thetypeofanelementscharactercontent.2774.8.4Constructorsandtheirdefaultvalues.277Page8欢迎加入非盈利Py t h o n
26、 学习交流编程QQ群783462347,群里免费提供500+本Py t h o n 书籍!APythonBookPrefaceThisbookisacollectionofmaterialsthatIveusedwhenconductingPythontrainingandalsomaterialsfrommyWebsitethatareintendedforselfinstruction.Youmaypreferamachinereadablecopyofthisbook.Youcanfinditinvariousformatshere:HTMLhttp:/www.davekuhlman.o
27、rg/python_book_01.htmlPDFhttp:/www.davekuhlman.org/python_book_01.pdfODF/OpenOfficehttp:/www.davekuhlman.org/python_book_01.odtAnd,letmethankthestudentsinmyPythonclasses.Theirquestionsandsuggestionswereagreathelpinthepreparationofthesematerials.Page9欢迎加入非盈利Py t h o n 学习交流编程QQ群783462347,群里免费提供500+本Py
28、 t h o n 书籍!APythonBook1Part1BeginningPython1.1IntroductionsEtcIntroductionsPracticalmatters:restrooms,breakroom,lunchandbreaktimes,etc.StartingthePythoninteractiveinterpreter.Also,IPythonandIdle.RunningscriptsEditorsChooseaneditorwhichyoucanconfiguresothatitindentswith4spaces,nottabcharacters.Foral
29、istofeditorsforPython,see:http:/wiki.python.org/moin/PythonEditors.Afewpossibleeditors:SciTEhttp:/www.scintilla.org/SciTE.html.MSWindowsonly(1)TextPadhttp:/;(2)UltraEdithttp:/ t h o n 学习交流编程QQ群783462347,群里免费提供500+本Py t h o n 书籍!APythonBook1.1.1ResourcesWhereelsetogethelp:Pythonhomepagehttp:/www.pyth
30、on.orgPythonstandarddocumentationhttp:/www.python.org/doc/.Youwillalsofindlinkstotutorialsthere.FAQshttp:/www.python.org/doc/faq/.ThePythonWikihttp:/wiki.python.org/ThePythonPackageIndexLotsofPythonpackageshttps:/pypi.python.org/pypiSpecialinterestgroups(SIGs)http:/www.python.org/sigs/Otherpythonrel
31、atedmailinglistsandlistsforspecificapplications(forexample,Zope,Twisted,etc).Try:http:/dir.gmane.org/search.php?match=python.http:/Lotsofprojects.Searchforpython.USENETcomp.lang.python.CanalsobeaccessedthroughGmane:http:/dir.gmane.org/p.python.general.ThePythontutoremaillisthttp:/mail.python.org/mai
32、lman/listinfo/tutorLocaldocumentation:OnMSWindows,thePythondocumentationisinstalledwiththestandardinstallation.InstallthestandardPythondocumentationonyourmachinefromhttp:/www.python.org/doc/.pydoc.Example,onthecommandline,type:pydocre.Importamodule,thenviewits._doc_attribute.Attheinteractiveprompt,u
33、sehelp(obj).Youmightneedtoimportitfirst.Example:importurllibhelp(urllib)InIPython,thequestionmarkoperatorgiveshelp.Example:In13:open?Type:builtin_function_or_methodBaseClass:StringForm:Namespace:PythonbuiltinDocstring:open(name,mode,buffering)fileobjectOpenafileusingthefile()type,returnsafileobject.
34、ConstructorDocstring:x._init_(.)initializesx;seex._class_._doc_forsignaturePage11欢迎加入非盈利Py t h o n 学习交流编程QQ群783462347,群里免费提供500+本Py t h o n 书籍!APythonBookCallable:YesCalldef:Callingdefinitionnotavailable.Calldocstring:x._call_(.)x(.)1.1.2AgeneraldescriptionofPythonPythonisahighlevelgeneralpurposepro
35、gramminglanguage:Becausecodeisautomaticallycompiledtobytecodeandexecuted,Pythonissuitableforuseasascriptinglanguage,Webapplicationimplementationlanguage,etc.BecausePythoncanbeextendedinCandC+,Pythoncanprovidethespeedneededforevencomputeintensivetasks.Becauseofitsstrongstructuringconstructs(nestedcod
36、eblocks,functions,classes,modules,andpackages)anditsconsistentuseofobjectsandobjectorientedprogramming,Pythonenablesustowriteclear,logicalapplicationsforsmallandlargetasks.ImportantfeaturesofPython:Builtinhighleveldatatypes:strings,lists,dictionaries,etc.Theusualcontrolstructures:if,ifelse,ifelifels
37、e,while,plusapowerfulcollectioniterator(for).Multiplelevelsoforganizationalstructure:functions,classes,modules,andpackages.Theseassistinorganizingcode.AnexcellentandlargeexampleisthePythonstandardlibrary.CompileontheflytobytecodeSourcecodeiscompiledtobytecodewithoutaseparatecompilestep.Sourcecodemod
38、ulescanalsobeprecompiledtobytecodefiles.ObjectorientedPythonprovidesaconsistentwaytouseobjects:everythingisanobject.And,inPythonitiseasytoimplementnewobjecttypes(calledclassesinobjectorientedprogramming).ExtensionsinCandC+Extensionmodulesandextensiontypescanbewrittenbyhand.Therearealsotoolsthathelpw
39、iththis,forexample,SWIG,sip,Pyrex.JythonisaversionofPythonthatplayswellwithJava.See:TheJythonProjecthttp:/www.jython.org/Project/.Somethingsyouwillneedtoknow:Pythonusesindentationtoshowblockstructure.Indentoneleveltoshowthebeginningofablock.Outdentoneleveltoshowtheendofablock.Asanexample,thefollowin
40、gCstylecode:if(x)Page12欢迎加入非盈利Py t h o n 学习交流编程QQ群783462347,群里免费提供500+本Py t h o n 书籍!APythonBookif(y)f1()f2()inPythonwouldbe:ifx:ify:f1()f2()And,theconventionistousefourspaces(andnohardtabs)foreachlevelofindentation.Actually,itsmorethanaconvention;itspracticallyarequirement.Followingthatconventionwi
41、llmakeitsomucheasiertomergeyourPythoncodewithcodefromothersources.AnoverviewofPython:AscriptinglanguagePythonissuitable(1)forembedding,(2)forwritingsmallunstructuredscripts,(3)forquickanddirtyprograms.Notascriptinglanguage(1)Pythonscales.(2)Pythonencouragesustowritecodethatisclearandwellstructured.I
42、nterpreted,butalsocompiledtobytecode.Modulesareautomaticallycompiled(to.pyc)whenimported,butmayalsobeexplicitlycompiled.Providesaninteractivecommandlineandinterpretershell.Infact,thereareseveral.DynamicForexample:Typesareboundtovalues,nottovariables.Functionandmethodlookupisdoneatruntime.Valuesarein
43、spectable.Thereisaninteractiveinterpreter,morethanone,infact.Youcanlistthemethodssupportedbyanygivenobject.Stronglytypedatruntime,notcompiletime.Objects(values)haveatype,butvariablesdonot.ReasonablyhighlevelHighlevelbuiltindatatypes;highlevelcontrolstructures(forwalkinglistsanditerators,forexample).
44、ObjectorientedAlmosteverythingisanobject.Simpleobjectdefinition.Datahidingbyagreement.Multipleinheritance.Interfacesbyconvention.Polymorphism.HighlystructuredStatements,functions,classes,modules,andpackagesenableustowritelarge,wellstructuredapplications.Whystructure?Readability,locateability,modifia
45、bility.ExplicitnessPage13欢迎加入非盈利Py t h o n 学习交流编程QQ群783462347,群里免费提供500+本Py t h o n 书籍!APythonBookFirstclassobjects:Definition:Can(1)passtofunction;(2)returnfromfunction;(3)stuffintoadatastructure.Operatorscanbeappliedtovalues(notvariables).Example:f(x)3IndentedblockstructurePythonispseudocodethatru
46、ns.EmbeddingandextendingPythonPythonprovidesawelldocumentedandsupportedway(1)toembedthePythoninterpreterinC/C+applicationsand(2)toextendPythonwithmodulesandobjectsimplementedinC/C+.Insomecases,SWIGcangeneratewrappersforexistingC/C+codeautomatically.Seehttp:/www.swig.org/CythonenablesustogenerateCcod
47、efromPythonandtoeasilycreatewrappersforC/C+functions.Seehttp:/www.cosc.canterbury.ac.nz/greg/python/Pyrex/ToembedandextendPythonwithJava,thereisJython.Seehttp:/www.jython.org/Automaticgarbagecollection.(But,thereisagcmoduletoallowexplicitcontrolofgarbagecollection.)Comparisonwithotherlanguages:compi
48、ledlanguages(e.g.C/C+);Java;Perl,Tcl,andRuby.Pythonexcellsat:developmentspeed,executionspeed,clarityandmaintainability.VarietiesofPython:CPythonStandardPython2.ximplementedinC.JythonPythonfortheJavaenvironmenthttp:/www.jython.org/PyPyPythonwithaJITcompilerandstacklessmodehttp:/pypy.org/StacklessPyth
49、onwithenhancedthreadsupportandmicrothreadsetc.http:/ t h o n 学习交流编程QQ群783462347,群里免费提供500+本Py t h o n 书籍!APythonBookhttp:/docs.python.org/3/library/2to3.html#fixersThemigrationtool,2to3,easestheconversionof2.xcodeto3.x.AlsoseeTheZenofPythonhttp:/www.python.org/peps/pep0020.html.Or,atthePythoninterac
50、tiveprompt,type:importthis1.1.3InteractivePythonIfyouexecutePythonfromthecommandlinewithnoscript(noarguments),Pythongivesyouaninteractiveprompt.ThisisanexcellentfacilityforlearningPythonandfortryingsmallsnippetsofcode.ManyoftheexamplesthatfollowweredevelopedusingthePythoninteractiveprompt.StartthePy