Table Of ContentTreading on Python: Volume 1
Foundations
Treading on Python: Volume 1
Foundations
MattHarrison
TechnicalEditor: Shannon-jjBehrens
hairysun.com
COPYRIGHT©2013
Whileeveryprecautionhasbeentakeninthepreparationofthis
book,thepublisherandauthorassumesnoresponsibilityforerrors
oromissions,orfordamagesresultingfromtheuseofthe
informationcontainedherein
Contents
Contents
1 WhyPython? 3
2 WhichVersionofPython? 5
2.1 Pythoninstallation . . . . . . . . . . . . . . . . . . . 5
2.2 Whicheditor? . . . . . . . . . . . . . . . . . . . . . . 5
3 TheInterpreter 7
3.1 Interactiveinterpreter . . . . . . . . . . . . . . . . . . 8
3.2 AREPLexample. . . . . . . . . . . . . . . . . . . . . 8
4 RunningPrograms 11
4.1 Unixyembellishments . . . . . . . . . . . . . . . . . 12
5 WritingandReadingData 15
5.1 Simpleoutput . . . . . . . . . . . . . . . . . . . . . . 15
5.2 Gettinguserinput . . . . . . . . . . . . . . . . . . . . 16
6 Variables 17
6.1 Mutationandstate . . . . . . . . . . . . . . . . . . . 17
6.2 Pythonvariablesareliketags . . . . . . . . . . . . . 17
6.3 Cattletags . . . . . . . . . . . . . . . . . . . . . . . . 18
7 BasicTypes 21
7.1 Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . 21
7.2 Integersandfloats . . . . . . . . . . . . . . . . . . . . 22
7.3 Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . 22
7.4 Rebindingvariables . . . . . . . . . . . . . . . . . . . 22
7.5 Namingvariables . . . . . . . . . . . . . . . . . . . . 23
7.6 Additionalnamingconsiderations . . . . . . . . . . . 23
8 Moreaboutobjects 27
8.1 Identity . . . . . . . . . . . . . . . . . . . . . . . . . . 27
8.2 Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
8.3 Mutability . . . . . . . . . . . . . . . . . . . . . . . . 29
v
Contents
9 Numbers 31
9.1 Addition . . . . . . . . . . . . . . . . . . . . . . . . . 31
9.2 Subtraction . . . . . . . . . . . . . . . . . . . . . . . . 33
9.3 Multiplication . . . . . . . . . . . . . . . . . . . . . . 33
9.4 Division . . . . . . . . . . . . . . . . . . . . . . . . . 34
9.5 Modulo . . . . . . . . . . . . . . . . . . . . . . . . . . 35
9.6 Power . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
9.7 Orderofoperations . . . . . . . . . . . . . . . . . . . 38
10 Strings 39
11 FormattingStrings 43
11.1 Formatstringsyntax . . . . . . . . . . . . . . . . . . 43
11.2 formatexamples . . . . . . . . . . . . . . . . . . . . . 45
12 dir,help,andpdb 47
12.1 Dundermethods . . . . . . . . . . . . . . . . . . . . 48
12.2 help . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
12.3 pdb . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
13 Stringsandmethods 51
13.1 Commonstringmethods . . . . . . . . . . . . . . . . 52
13.2 endswith . . . . . . . . . . . . . . . . . . . . . . . . . 53
13.3 find . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
13.4 format . . . . . . . . . . . . . . . . . . . . . . . . . . 54
13.5 join . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
13.6 startswith . . . . . . . . . . . . . . . . . . . . . . . . 56
13.7 strip . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
14 Comments,Booleans,andNone 57
14.1 Comments . . . . . . . . . . . . . . . . . . . . . . . . 57
14.2 Booleans . . . . . . . . . . . . . . . . . . . . . . . . . 58
14.3 None . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
15 Conditionalsandwhitespace 63
15.1 Combiningconditionals . . . . . . . . . . . . . . . . 64
15.2 ifstatements. . . . . . . . . . . . . . . . . . . . . . . 66
15.3 elsestatements . . . . . . . . . . . . . . . . . . . . . 66
15.4 Morechoices . . . . . . . . . . . . . . . . . . . . . . . 66
15.5 Whitespace . . . . . . . . . . . . . . . . . . . . . . . . 67
16 Sequences:Lists,tuples,andsets 69
16.1 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
16.2 Sequenceindices. . . . . . . . . . . . . . . . . . . . . 70
16.3 Listinsertion . . . . . . . . . . . . . . . . . . . . . . . 70
16.4 Listdeletion . . . . . . . . . . . . . . . . . . . . . . . 71
16.5 Sortinglists. . . . . . . . . . . . . . . . . . . . . . . . 71
vi
Contents
16.6 Usefullisthints . . . . . . . . . . . . . . . . . . . . . 72
16.7 Tuples. . . . . . . . . . . . . . . . . . . . . . . . . . . 73
16.8 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
17 Iteration 79
17.1 Loopingwithanindex . . . . . . . . . . . . . . . . . 79
17.2 Breakingoutofaloop. . . . . . . . . . . . . . . . . . 80
17.3 Skippingoveritemsinaloop . . . . . . . . . . . . . 80
17.4 Removingitemsfromlistsduringiteration . . . . . . 81
17.5 elseclauses . . . . . . . . . . . . . . . . . . . . . . . 82
18 Dictionaries 83
18.1 Dictionaryassignment . . . . . . . . . . . . . . . . . 83
18.2 Retrievingvaluesfromadictionary . . . . . . . . . . 83
18.3 Theinoperator . . . . . . . . . . . . . . . . . . . . . 84
18.4 Dictionaryshortcuts. . . . . . . . . . . . . . . . . . . 84
18.5 setdefault . . . . . . . . . . . . . . . . . . . . . . . . 85
18.6 Deletingkeys. . . . . . . . . . . . . . . . . . . . . . . 87
18.7 Dictionaryiteration . . . . . . . . . . . . . . . . . . . 87
19 Functions 89
19.1 Invokingfunctions . . . . . . . . . . . . . . . . . . . 91
19.2 Multipleparameters. . . . . . . . . . . . . . . . . . . 91
19.3 Defaultparameters . . . . . . . . . . . . . . . . . . . 92
19.4 Namingconventionsforfunctions . . . . . . . . . . . 94
20 IndexingandSlicing 95
20.1 Indexing . . . . . . . . . . . . . . . . . . . . . . . . . 95
20.2 Slicingsublists . . . . . . . . . . . . . . . . . . . . . 96
20.3 Stridingslices . . . . . . . . . . . . . . . . . . . . . . 97
21 FileInputandOutput 99
21.1 Openingfiles . . . . . . . . . . . . . . . . . . . . . . . 99
21.2 Readingfiles . . . . . . . . . . . . . . . . . . . . . . . 99
21.3 Iterationwithfiles . . . . . . . . . . . . . . . . . . . . 100
21.4 Writingfiles . . . . . . . . . . . . . . . . . . . . . . . 101
21.5 Closingfiles . . . . . . . . . . . . . . . . . . . . . . . 102
21.6 Designingaroundfiles . . . . . . . . . . . . . . . . . 103
22 Classes 105
22.1 Definingaclass . . . . . . . . . . . . . . . . . . . . . 106
22.2 Creatinganinstanceofaclass . . . . . . . . . . . . . 107
22.3 Callingamethodonaclass. . . . . . . . . . . . . . . 108
22.4 Examininganinstance . . . . . . . . . . . . . . . . . 108
23 SubclassingaClass 109
23.1 Superclasses . . . . . . . . . . . . . . . . . . . . . . . 110
vii
Contents
23.2 Callingparentclassmethods . . . . . . . . . . . . . . 110
24 Exceptions 113
24.1 Lookbeforeyouleap . . . . . . . . . . . . . . . . . . 113
24.2 Easiertoaskforforgiveness . . . . . . . . . . . . . . 114
24.3 Multipleexceptionalcases . . . . . . . . . . . . . . . 115
24.4 finallyclause . . . . . . . . . . . . . . . . . . . . . . 115
24.5 elseclause . . . . . . . . . . . . . . . . . . . . . . . . 116
24.6 Raisingexceptions . . . . . . . . . . . . . . . . . . . . 116
24.7 Definingyourownexceptions . . . . . . . . . . . . . 116
25 Importinglibraries 119
25.1 Multiplewaystoimport . . . . . . . . . . . . . . . . 119
25.2 Conflictingimportnames. . . . . . . . . . . . . . . . 120
25.3 Starimports . . . . . . . . . . . . . . . . . . . . . . . 121
25.4 Nestedlibraries . . . . . . . . . . . . . . . . . . . . . 122
25.5 Importorganization . . . . . . . . . . . . . . . . . . . 123
26 Libraries:PackagesandModules 125
26.1 Modules . . . . . . . . . . . . . . . . . . . . . . . . . 125
26.2 Packages . . . . . . . . . . . . . . . . . . . . . . . . . 125
26.3 Importingpackages . . . . . . . . . . . . . . . . . . . 126
26.4 PYTHONPATH . . . . . . . . . . . . . . . . . . . . . . . . 126
26.5 sys.path . . . . . . . . . . . . . . . . . . . . . . . . . 127
27 Acompleteexample 129
27.1 cat.py . . . . . . . . . . . . . . . . . . . . . . . . . . 129
27.2 Commonlayout . . . . . . . . . . . . . . . . . . . . . 131
27.3 Shebang . . . . . . . . . . . . . . . . . . . . . . . . . 132
27.4 Docstring . . . . . . . . . . . . . . . . . . . . . . . . . 132
27.5 Imports . . . . . . . . . . . . . . . . . . . . . . . . . . 133
27.6 Metadataandglobals . . . . . . . . . . . . . . . . . . 133
27.7 Logging. . . . . . . . . . . . . . . . . . . . . . . . . . 135
27.8 Otherglobals. . . . . . . . . . . . . . . . . . . . . . . 135
27.9 Implementation . . . . . . . . . . . . . . . . . . . . . 135
27.10Testing . . . . . . . . . . . . . . . . . . . . . . . . . . 135
27.11if __name__ == ’__main__’: . . . . . . . . . . . . . 136
27.12__name__ . . . . . . . . . . . . . . . . . . . . . . . . . 136
28 OntoBiggerandBetter 139
AppendixA:TestinginPython 141
TheStigmaofTesting . . . . . . . . . . . . . . . . . . . . . 141
Detourtounittest . . . . . . . . . . . . . . . . . . . . . . 142
unittestdetails . . . . . . . . . . . . . . . . . . . . . . . . 142
AssertionMethods . . . . . . . . . . . . . . . . . . . . . . 144
Runningthetestcode . . . . . . . . . . . . . . . . . . . . . 144
viii
Contents
Startwithcommas . . . . . . . . . . . . . . . . . . . . . . . 144
Beingcontrolledbytesting . . . . . . . . . . . . . . . . . . 145
HandlingWhitespace . . . . . . . . . . . . . . . . . . . . . 145
ExceptionHandling . . . . . . . . . . . . . . . . . . . . . . 145
TDDvsCodefirst . . . . . . . . . . . . . . . . . . . . . . . 146
Turningtestingonitshead . . . . . . . . . . . . . . . . . . 146
Testmydocstrings . . . . . . . . . . . . . . . . . . . . . . . 147
integrdoctests. . . . . . . . . . . . . . . . . . . . . . . . . 149
unittestordoctest? . . . . . . . . . . . . . . . . . . . . . 149
Organizingdoctestfiles . . . . . . . . . . . . . . . . . . . 150
Morefunwithdoctest . . . . . . . . . . . . . . . . . . . . 150
Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Blanklines . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Spacing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Dealingwithexceptions. . . . . . . . . . . . . . . . . . . . 152
StartTesting . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Abouttheauthor 155
AbouttheTechnicalEditor . . . . . . . . . . . . . . . . . . 156
Index 157
ix
Description:Introducing the Treading on Python Series: Beginning Python Programming: Learn Python in 7 Days This book is designed to bring developers and others who are anxious to learn how to program Python up to speed quickly. Not only does it provide an introduction to Python and teach the basics of syntax,