Python tutorial
120 pages
Français

Découvre YouScribe en t'inscrivant gratuitement

Je m'inscris

Découvre YouScribe en t'inscrivant gratuitement

Je m'inscris
Obtenez un accès à la bibliothèque pour le consulter en ligne
En savoir plus
120 pages
Français
Obtenez un accès à la bibliothèque pour le consulter en ligne
En savoir plus

Informations

Publié par
Nombre de lectures 700
Langue Français

Extrait

Python Tutorial Release 2.5 Guido van Rossum Fred L. Drake, Jr., editor 19thSeptember,2006 PythonSoftwareFoundation Email: docs@python.org Copyright©2001 2006PythonSoftwareFoundation. Allrightsreserved. Copyright©2000BeOpen.com. Allrightsreserved. Copyright©1995 2000CorporationforNationalResearchInitiatives. Allrightsreserved. Copyright©1991 1995StichtingMathematischCentrum. Allrightsreserved. Seetheendofthisdocumentforcompletelicenseandpermissionsinformation. Abstract Pythonisaneasytolearn,powerfulprogramminglanguage. Ithasefficienthigh leveldatastructuresandasimple but effective approach to object oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areasonmostplatforms. ThePythoninterpreterandtheextensivestandardlibraryarefreelyavailableinsourceorbinaryformforallmajor platforms from the Python Web site, http://www.python.org/, and may be freely distributed. The same site also containsdistributionsofandpointerstomanyfreethirdpartyPythonmodules,programsandtools,andadditional documentation. The Python interpreter is easily extended with new functions and data types implemented in C or C++ (or other languagescallablefromC).Pythonisalsosuitableasanextensionlanguageforcustomizableapplications. ThistutorialintroducesthereaderinformallytothebasicconceptsandfeaturesofthePythonlanguageandsystem. It helps to have a Python interpreter handy for hands on experience, but all examples are self contained, so the tutorialcanbereadoff lineaswell. For a description of standard objects and modules, see the Python Library Reference document. The Python ReferenceManualgivesamoreformaldefinitionofthelanguage. TowriteextensionsinCorC++,readExtending andEmbeddingthePythonInterpreterandPython/CAPIReference. TherearealsoseveralbookscoveringPython indepth. This tutorial does not attempt to be comprehensive and cover every single feature, or even every commonly used feature. Instead, it introduces many of Python’s most noteworthy features, and will give you a good idea of the language’s flavor and style. After reading it, you will be able to read and write Python modules and programs, and you will be ready to learn more about the various Python library modules described in the Python Library Reference. CONTENTS 1 WhettingYourAppetite 1 2 UsingthePythonInterpreter 3 2.1 InvokingtheInterpreter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2.2 TheInterpreterandItsEnvironment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 3 AnInformalIntroductiontoPython 7 3.1 UsingPythonasaCalculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 3.2 FirstStepsTowardsProgramming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 4 MoreControlFlowTools 19 4.1 ifStatements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 4.2 for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 4.3 Therange()Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 4.4 breakandcontinueStatements,andelseClausesonLoops . . . . . . . . . . . . . . . . . 20 4.5 passStatements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 4.6 DefiningFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 4.7 MoreonDefiningFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 5 DataStructures 29 5.1 MoreonLists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 5.2 Thedelstatement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 5.3 TuplesandSequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 5.4 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 5.5 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 5.6 LoopingTechniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 5.7 MoreonConditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 5.8 ComparingSequencesandOtherTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 6 Modules 39 6.1 MoreonModules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 6.2 Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 6.3 Thedir()Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 6.4 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 7 InputandOutput 47 7.1 FancierOutputFormatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 7.2 ReadingandWritingFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 8 ErrorsandExceptions 53 8.1 SyntaxErrors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 8.2 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 8.3 HandlingExceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 8.4 Raising . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 i 8.5 User definedExceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 8.6 DefiningClean upActions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 8.7 Predefined . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 9 Classes 61 9.1 AWordAboutTerminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 9.2 PythonScopesandNameSpaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 9.3 AFirstLookatClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 9.4 RandomRemarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 9.5 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 9.6 PrivateVariables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 9.7 OddsandEnds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 9.8 ExceptionsAreClassesToo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 9.9 Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 9.10 Generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 9.11 GeneratorExpressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 10 BriefTouroftheStandardLibrary 73 10.1 OperatingSystemInterface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 10.2 FileWildcards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 10.3 CommandLineArguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 10.4 ErrorOutputRedirectionandProgramTermination . . . . . . . . . . . . . . . . . . . . . . . . . 74 10.5 StringPatternMatching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 10.6 Mathematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 10.7 InternetAccess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 10.8 DatesandTimes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 10.9 DataCompression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 10.10 PerformanceMeasurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 10.11 QualityControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 10.12 BatteriesIncluded . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 11 BriefTouroftheStandardLibrary–PartII 79 11.1 OutputFormatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 11.2 Templating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 11.3 WorkingwithBinaryDataRecordLayouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 11.4 Multi threading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 11.5 Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 11.6 WeakReferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 11.7 ToolsforWorkingwithLists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 11.8 DecimalFloatingPointArithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 12 WhatNow? 87 A InteractiveInputEditingandHistorySubstitution 89 A.1 LineEditing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 A.2 HistorySubstitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 A.3 KeyBindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 A.4 Commentary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 B FloatingPointArithmetic: IssuesandLimitations 93 B.1 RepresentationError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 C HistoryandLicense
  • Univers Univers
  • Ebooks Ebooks
  • Livres audio Livres audio
  • Presse Presse
  • Podcasts Podcasts
  • BD BD
  • Documents Documents