PythonTutorialRelease2.4GuidovanRossumFredL.Drake,Jr.,editorDecember21,2004PythonSoftwareFoundationEmail:docs@python.orgCopyrightc°2001-2004PythonSoftwareFoundation.Allrightsreserved.Copyrightc°2000BeOpen.com.Allrightsreserved.Copyrightc°1995-2000CorporationforNationalResearchInitiatives.Allrightsreserved.Copyrightc°1991-1995StichtingMathematischCentrum.Allrightsreserved.Seetheendofthisdocumentforcompletelicenseandpermissionsinformation.AbstractPythonisaneasytolearn,powerfulprogramminglanguage.Ithasefficienthigh-leveldatastructuresandasimplebuteffectiveapproachtoobject-orientedprogramming.Python’selegantsyntaxanddynamictyping,togetherwithitsinterpretednature,makeitanideallanguageforscriptingandrapidapplicationdevelopmentinmanyareasonmostplatforms.PythonPythonThePythoninterpreterandtheextensivestandardlibraryarefreelyavailableinsourceorbinaryformforallmajorplatformsfromthePythonWebsite,://(orotherlanguagescallablefromC).Pythonisalsosuitableasanextensionlanguageforcustomizableapplications.PythonCC++CPythonThistutorialintroducesthereaderinformallytothebasicconceptsandfeaturesofthePythonlanguageandsystem.IthelpstohaveaPythoninterpreterhandyforhands-onexperience,butallexamplesareself-contained,sothetutorialcanbereadoff-lineaswell.PythonPythonForadescriptionofstandardobjectsandmodules,seethePythonLibraryReferencedocument.ThePythonRefer-enceManualgivesamoreformaldefinitionofthelanguage.TowriteextensionsinCorC++,readExtendingandEmbeddingthePythonInterpreterandPython/CAPIReference.TherearealsoseveralbookscoveringPythonindepth.PythonPythonCC++PythonPython/CAPIPythonThistutorialdoesnotattempttobecomprehensiveandcovereverysinglefeature,oreveneverycommonlyusedfeature.Instead,itintroducesmanyofPython’smostnoteworthyfeatures,andwillgiveyouagoodideaofthelanguage’sflavorandstyle.Afterreadingit,youwillbeabletoreadandwritePythonmodulesandprograms,andyouwillbereadytolearnmoreaboutthevariousPythonlibrarymodulesdescribedinthePythonLibraryReference.PythonPythonPythonPythonPythonCONTENTS1WhettingYourAppetite12UsingthePythonInterpreter32.1InvokingtheInterpreter..................................32.2TheInterpreterandItsEnvironment.........................53PythonAnInformalIntroductiontoPython93.1PythonUsingPythonasaCalculator........................93.2FirstStepsTowardsProgramming..............................214MoreControlFlowTools234.1ififStatements.........................................234.2forforStatements........................................234.3range()Therange()Function................................244.4breakcontinue,elsebreakandcontinueStatements,andelseClausesonLoops.........................................254.5passpassStatements......................................264.6DefiningFunctions...........................................264.7MoreonDefiningFunctions...............................285DataStructures355.1MoreonLists.........................................355.2del.................................................405.3TuplesSequencesTuplesandSequences....................415.4Dictionaries.............................................425.5LoopingTechniques.....................................435.6MoreonConditions...................................445.7ComparingSequencesandOtherTypes.....................456Modules476.1MoreonModules.......................................486.2StandardModules.......................................516.3dir()dir()Function......................................526.4Packages...............................................537InputandOutput597.1FancierOutputFormatting...............................597.2ReadingandWritingFiles..................................638ErrorsandExceptions67i8.1Exceptions.............................................678.2HandlingExceptions.....................................688.3RaisingExceptions......................................718.4User-definedExceptions...............................728.5DefiningClean-upActions...............................739Classes759.1AWordAboutTerminology.............................759.2PythonPythonScopesandNameSpaces.....................769.3AFirstLookatClasses.....................................789.4RandomRemarks.......................................829.5Inheritance.............................................849.6PrivateVariables.......................................859.7OddsandEnds...........................................869.8ExceptionsAreClassesToo................................879.9Iterators..............................................889.10Generators............................................8910BriefTouroftheStandardLibrary9110.1OperatingSystemInterface...............................9110.2FileWildcards......................................