ImageVerifierCode 换一换
格式:PDF , 页数:278 ,大小:1.35MB ,
资源ID:5710238      下载积分:10 文币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.wenkunet.com/d-5710238.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录   微博登录 

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(A-Python-Book-Beginning-Python-Advanced-Python-and-Python-Exercises.pdf)为本站会员(jintaihu)主动上传,文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知文库网(发送邮件至13560552955@163.com或直接QQ联系客服),我们立即给予删除!

A-Python-Book-Beginning-Python-Advanced-Python-and-Python-Exercises.pdf

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

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:文库网官方知乎号:文库网

经营许可证编号: 粤ICP备2021046453号世界地图

文库网官网©版权所有2025营业执照举报