Python Tutorial
Release 2.6.4
Guido van Rossum
Fred L. Drake, Jr., editor
November 25, 2009
Python Software Foundation
Email: docs@python.org
CONTENTS
1 Whetting Your Appetite 3
2 Using the Python Interpreter 5
2.1 InvokingtheInterpreter ....................................... 5
2.2 TheInterpreterandItsEnvironment ................................. 6
3 An Informal Introduction to Python 9
3.1 UsingPythonasaCalculator .................................... 9
3.2 FirstStepsTowardsProgramming .................................. 17
4 More Control Flow Tools 19
4.1 ifStatements ............................................ 19
4.2 forStatements ........................................... 19
4.3 The range()Function....................................... 20
4.4 breakand continueStatements, and elseClausesonLoops ................. 20
4.5 passStatements........................................... 21
4.6 DefiningFunctions .......................................... 21
4.7 MoreonDefiningFunctions ..................................... 23
4.8 Intermezzo:CodingStyle ...................................... 26
5 Data Structures 29
5.1 MoreonLists ............................................. 29
5.2 The delstatement.......................................... 33
5.3 TuplesandSequences ........................................ 33
5.4 Sets.................................................. 34
5.5 Dictionaries.............................................. 35
5.6 LoopingTechniques ......................................... 35
5.7 MoreonConditions ......................................... 36
5.8 ComparingSequencesandOtherTypes ............................... 37
6 Modules 39
6.1 MoreonModules .......................................... 40
6.2 StandardModules .......................................... 42
6.3 The dir()Function......................................... 42
6.4 Packages ............................................... 43
7 Input and Output 47
7.1 FancierOutputFormatting ...................................... 47
7.2 ReadingandWritingFiles ...................................... 50
8 Errors and Exceptions 53
8.1 SyntaxErrors ............................................. 53
8.2 Exceptions .............................................. 53
8.3 HandlingExceptions ......................................... 54
8.4 RaisingExceptions .......................................... 55
8.5 User-definedExceptions ....................................... 56
8.6 DefiningClean-upActions ...................................... 57
8.7 PredefinedClean-upActions ..................................... 58
9 Classes 59
9.1 AWordAboutNamesandObjects ................................. 59
9.2 PythonScopesandNamespaces ................................... 59
9.3 AFirstLookatClasses ........................................ 61
9.4 RandomRemarks .......................................... 63
9.5 Inheritance .............................................. 64
9.6 PrivateVariables ........................................... 65
9.7 OddsandEnds ............................................ 66
9.8 ExceptionsAreClassesToo ..................................... 66
9.9 Iterators................................................ 67
9.10 Generators .............................................. 68
9.11 GeneratorExpressions ........................................ 68
10 Brief Tour of the Standard Library 71
10.1 OperatingSystemInterface ..................................... 71
10.2 FileWildcards ............................................ 71
10.3 CommandLineArguments ..................................... 71
10.4 ErrorOutputRedirectionandProgramTermination . . . . . . . . . . . . . . . . . . . . . . . . . 72
10.5 StringPatternMatching ....................................... 72
10.6 Mathematics ............................................. 72
10.7 InternetAccess ............................................ 73
10.8 DatesandTimes ........................................... 73
10.9 DataCompression .......................................... 73
10.10PerformanceMeasurement ...................................... 74
10.11QualityControl ............................................ 74
Add to your private library
My LibraryAdd this story to your public reading lists