\contentsline {chapter}{License}{xi}
\contentsline {chapter}{Foreword}{xiii}
\contentsline {chapter}{Preface}{xv}
\contentsline {chapter}{Contributor List}{xix}
\contentsline {chapter}{Notes on the Otago Edition}{xxiii}
\contentsline {chapter}{Course Information}{xxv}
\contentsline {chapter}{\numberline {1}The way of the program}{1}
\contentsline {section}{\numberline {1.1}The Python programming language}{1}
\contentsline {section}{\numberline {1.2}What is a program?}{4}
\contentsline {section}{\numberline {1.3}What is debugging?}{5}
\contentsline {section}{\numberline {1.4}Experimental debugging}{5}
\contentsline {section}{\numberline {1.5}Formal and natural languages}{6}
\contentsline {section}{\numberline {1.6}The first program}{7}
\contentsline {section}{\numberline {1.7}Glossary}{7}
\contentsline {section}{\numberline {1.8}The COMP150 lab}{9}
\contentsline {subsection}{\numberline {1.8.1}Logging-on }{9}
\contentsline {subsection}{\numberline {1.8.2}The OSX desktop}{9}
\contentsline {subsection}{\numberline {1.8.3}Menus}{9}
\contentsline {subsection}{\numberline {1.8.4}The dock}{11}
\contentsline {subsection}{\numberline {1.8.5}The Finder window}{11}
\contentsline {subsection}{\numberline {1.8.6}Your home directory}{11}
\contentsline {subsection}{\numberline {1.8.7}Applications}{12}
\contentsline {subsection}{\numberline {1.8.8}IDLE}{12}
\contentsline {subsection}{\numberline {1.8.9}Getting help}{13}
\contentsline {subsection}{\numberline {1.8.10}Coursework files}{14}
\contentsline {subsection}{\numberline {1.8.11}Terms requirements}{14}
\contentsline {section}{\numberline {1.9}Blackboard}{14}
\contentsline {section}{\numberline {1.10}Laboratory exercises}{15}
\contentsline {chapter}{\numberline {2}Variables, expressions and statements}{19}
\contentsline {section}{\numberline {2.1}Values and types}{19}
\contentsline {section}{\numberline {2.2}Variables}{20}
\contentsline {section}{\numberline {2.3}Variable names and keywords}{22}
\contentsline {section}{\numberline {2.4}Statements}{23}
\contentsline {section}{\numberline {2.5}Evaluating expressions}{23}
\contentsline {section}{\numberline {2.6}Operators and operands}{24}
\contentsline {section}{\numberline {2.7}The modulus operator}{24}
\contentsline {section}{\numberline {2.8}Order of operations}{25}
\contentsline {section}{\numberline {2.9}Operations on strings}{25}
\contentsline {section}{\numberline {2.10}Glossary}{26}
\contentsline {section}{\numberline {2.11}Laboratory exercises}{27}
\contentsline {chapter}{\numberline {3}Python built-ins (batteries included)}{31}
\contentsline {section}{\numberline {3.1}Function calls}{31}
\contentsline {section}{\numberline {3.2}Type conversion}{32}
\contentsline {section}{\numberline {3.3}Input}{33}
\contentsline {section}{\numberline {3.4}Methods}{34}
\contentsline {section}{\numberline {3.5}Examining things}{35}
\contentsline {section}{\numberline {3.6}More types}{35}
\contentsline {section}{\numberline {3.7}Strings and lists}{36}
\contentsline {section}{\numberline {3.8}Glossary}{37}
\contentsline {section}{\numberline {3.9}Laboratory exercises}{37}
\contentsline {chapter}{\numberline {4}Functions: part 1}{41}
\contentsline {section}{\numberline {4.1}Composing expressions}{41}
\contentsline {section}{\numberline {4.2}Function definitions and use}{41}
\contentsline {section}{\numberline {4.3}Flow of execution}{43}
\contentsline {section}{\numberline {4.4}Parameters and arguments}{44}
\contentsline {section}{\numberline {4.5}Function composition}{45}
\contentsline {section}{\numberline {4.6}Glossary}{45}
\contentsline {section}{\numberline {4.7}Laboratory exercises}{46}
\contentsline {chapter}{\numberline {5}Functions: part 2}{49}
\contentsline {section}{\numberline {5.1}Variables and parameters are local}{49}
\contentsline {section}{\numberline {5.2}Stack diagrams}{50}
\contentsline {section}{\numberline {5.3}Comments}{51}
\contentsline {section}{\numberline {5.4}Glossary}{51}
\contentsline {section}{\numberline {5.5}Laboratory exercises}{52}
\contentsline {chapter}{\numberline {6}Conditionals}{57}
\contentsline {section}{\numberline {6.1}Boolean values and expressions}{57}
\contentsline {section}{\numberline {6.2}Logical operators}{58}
\contentsline {section}{\numberline {6.3}Conditional execution}{58}
\contentsline {section}{\numberline {6.4}Alternative execution}{58}
\contentsline {section}{\numberline {6.5}Chained conditionals}{59}
\contentsline {section}{\numberline {6.6}Nested conditionals}{60}
\contentsline {section}{\numberline {6.7}Booleans and type conversion}{61}
\contentsline {section}{\numberline {6.8}Glossary}{61}
\contentsline {section}{\numberline {6.9}Laboratory exercises}{62}
\contentsline {chapter}{\numberline {7}Fruitful functions}{67}
\contentsline {section}{\numberline {7.1}The return statement}{67}
\contentsline {section}{\numberline {7.2}Return values}{67}
\contentsline {section}{\numberline {7.3}Program development}{69}
\contentsline {section}{\numberline {7.4}Composition}{70}
\contentsline {section}{\numberline {7.5}Boolean functions}{71}
\contentsline {section}{\numberline {7.6}The function type}{72}
\contentsline {section}{\numberline {7.7}Glossary}{73}
\contentsline {section}{\numberline {7.8}Laboratory exercises}{74}
\contentsline {chapter}{\numberline {8}Test driven development}{75}
\contentsline {section}{\numberline {8.1}Modules, files and the \texttt {import} statement}{75}
\contentsline {section}{\numberline {8.2}Programming with style}{76}
\contentsline {section}{\numberline {8.3}Triple quoted strings}{76}
\contentsline {section}{\numberline {8.4}Unit testing with \texttt {doctest}}{77}
\contentsline {section}{\numberline {8.5}Test-driven development demonstrated}{79}
\contentsline {section}{\numberline {8.6}Glossary}{79}
\contentsline {section}{\numberline {8.7}Laboratory exercises}{80}
\contentsline {chapter}{\numberline {9}Files and modules}{83}
\contentsline {section}{\numberline {9.1}Files}{83}
\contentsline {section}{\numberline {9.2}Processing things from a file}{84}
\contentsline {section}{\numberline {9.3}Directories}{86}
\contentsline {section}{\numberline {9.4}Modules}{86}
\contentsline {section}{\numberline {9.5}Creating modules}{87}
\contentsline {section}{\numberline {9.6}Namespaces}{88}
\contentsline {section}{\numberline {9.7}Attributes and the dot operator}{88}
\contentsline {section}{\numberline {9.8}Glossary}{88}
\contentsline {section}{\numberline {9.9}Laboratory Test}{90}
\contentsline {chapter}{\numberline {10}Iteration: part 1}{95}
\contentsline {section}{\numberline {10.1}Multiple assignment}{95}
\contentsline {section}{\numberline {10.2}Updating variables}{96}
\contentsline {section}{\numberline {10.3}The \texttt {while} statement}{96}
\contentsline {section}{\numberline {10.4}Tracing a program}{97}
\contentsline {section}{\numberline {10.5}Counting digits}{98}
\contentsline {section}{\numberline {10.6}Abbreviated assignment}{99}
\contentsline {section}{\numberline {10.7}Tables}{99}
\contentsline {section}{\numberline {10.8}Glossary}{100}
\contentsline {section}{\numberline {10.9}Laboratory exercises}{101}
\contentsline {chapter}{\numberline {11}Iteration: part 2}{103}
\contentsline {section}{\numberline {11.1}Two-dimensional tables}{103}
\contentsline {section}{\numberline {11.2}Encapsulation and generalization}{103}
\contentsline {section}{\numberline {11.3}More encapsulation}{104}
\contentsline {section}{\numberline {11.4}Local variables}{105}
\contentsline {section}{\numberline {11.5}More generalization}{105}
\contentsline {section}{\numberline {11.6}Functions}{107}
\contentsline {section}{\numberline {11.7}Newton's method}{107}
\contentsline {section}{\numberline {11.8}Algorithms}{107}
\contentsline {section}{\numberline {11.9}Glossary}{108}
\contentsline {section}{\numberline {11.10}Laboratory exercises}{108}
\contentsline {chapter}{\numberline {12}In-class test}{111}
\contentsline {chapter}{\numberline {13}Graphical user interface programming}{113}
\contentsline {section}{\numberline {13.1}Event driven programming}{113}
\contentsline {section}{\numberline {13.2}TkInter introduction}{113}
\contentsline {section}{\numberline {13.3}Introducing callbacks}{116}
\contentsline {section}{\numberline {13.4}User input}{118}
\contentsline {section}{\numberline {13.5}Mini-case study}{119}
\contentsline {section}{\numberline {13.6}Glossary}{120}
\contentsline {section}{\numberline {13.7}Laboratory exercises}{121}
\contentsline {chapter}{\numberline {14}Case study: Catch}{123}
\contentsline {section}{\numberline {14.1}Graphics}{123}
\contentsline {section}{\numberline {14.2}Moving the ball}{123}
\contentsline {section}{\numberline {14.3}Adding randomness}{125}
\contentsline {section}{\numberline {14.4}Glossary}{127}
\contentsline {section}{\numberline {14.5}Laboratory exercises}{128}
\contentsline {chapter}{\numberline {15}Case study: Catch continued}{129}
\contentsline {section}{\numberline {15.1}Keyboard input}{129}
\contentsline {section}{\numberline {15.2}Checking for collisions}{130}
\contentsline {section}{\numberline {15.3}Keeping score}{133}
\contentsline {section}{\numberline {15.4}Glossary}{135}
\contentsline {section}{\numberline {15.5}Laboratory exercises}{136}
\contentsline {section}{\numberline {15.6}Optional extension project: pong.py}{136}
\contentsline {chapter}{\numberline {16}Strings part 1}{137}
\contentsline {section}{\numberline {16.1}A compound data type}{137}
\contentsline {section}{\numberline {16.2}Length}{137}
\contentsline {section}{\numberline {16.3}Traversal and the \texttt {for} loop}{138}
\contentsline {section}{\numberline {16.4}String slices}{139}
\contentsline {section}{\numberline {16.5}String comparison}{141}
\contentsline {section}{\numberline {16.6}Strings are immutable}{141}
\contentsline {section}{\numberline {16.7}The \texttt {in} operator}{141}
\contentsline {section}{\numberline {16.8}A find function}{142}
\contentsline {section}{\numberline {16.9}Looping and counting}{143}
\contentsline {section}{\numberline {16.10}Optional parameters}{143}
\contentsline {section}{\numberline {16.11}Glossary}{144}
\contentsline {section}{\numberline {16.12}Laboratory exercises}{145}
\contentsline {chapter}{\numberline {17}Strings part 2}{147}
\contentsline {section}{\numberline {17.1}\texttt {str} Methods}{147}
\contentsline {section}{\numberline {17.2}Character classification}{149}
\contentsline {section}{\numberline {17.3}String formatting}{149}
\contentsline {section}{\numberline {17.4}Glossary}{151}
\contentsline {section}{\numberline {17.5}Laboratory exercises}{152}
\contentsline {chapter}{\numberline {18}Lists part 1}{155}
\contentsline {section}{\numberline {18.1}List values}{155}
\contentsline {section}{\numberline {18.2}Accessing elements}{156}
\contentsline {section}{\numberline {18.3}List length}{157}
\contentsline {section}{\numberline {18.4}List membership}{157}
\contentsline {section}{\numberline {18.5}List operations}{158}
\contentsline {section}{\numberline {18.6}List slices}{158}
\contentsline {section}{\numberline {18.7}The range function}{158}
\contentsline {section}{\numberline {18.8}Lists are mutable}{159}
\contentsline {section}{\numberline {18.9}List deletion}{160}
\contentsline {section}{\numberline {18.10}Objects and values}{160}
\contentsline {section}{\numberline {18.11}Aliasing}{161}
\contentsline {section}{\numberline {18.12}Cloning lists}{162}
\contentsline {section}{\numberline {18.13}Glossary}{162}
\contentsline {section}{\numberline {18.14}Laboratory exercises}{164}
\contentsline {chapter}{\numberline {19}Lists part 2}{167}
\contentsline {section}{\numberline {19.1}Lists and for loops}{167}
\contentsline {section}{\numberline {19.2}List parameters}{168}
\contentsline {section}{\numberline {19.3}Pure functions and modifiers}{169}
\contentsline {section}{\numberline {19.4}Which is better?}{169}
\contentsline {section}{\numberline {19.5}Nested lists}{170}
\contentsline {section}{\numberline {19.6}Matrices}{170}
\contentsline {section}{\numberline {19.7}Strings and lists}{171}
\contentsline {section}{\numberline {19.8}Glossary}{171}
\contentsline {section}{\numberline {19.9}Laboratory exercises}{172}
\contentsline {chapter}{\numberline {20}Tuples}{173}
\contentsline {section}{\numberline {20.1}Tuples and mutability}{173}
\contentsline {section}{\numberline {20.2}Tuple assignment}{174}
\contentsline {section}{\numberline {20.3}Tuples as return values}{175}
\contentsline {section}{\numberline {20.4}Why tuples?}{175}
\contentsline {section}{\numberline {20.5}When should you use tuples?}{175}
\contentsline {section}{\numberline {20.6}Sets}{176}
\contentsline {section}{\numberline {20.7}Glossary}{176}
\contentsline {section}{\numberline {20.8}Laboratory Test}{177}
\contentsline {chapter}{\numberline {21}Dictionaries}{183}
\contentsline {section}{\numberline {21.1}Dictionary operations}{184}
\contentsline {section}{\numberline {21.2}Dictionary methods}{184}
\contentsline {section}{\numberline {21.3}Aliasing and copying}{185}
\contentsline {section}{\numberline {21.4}Sparse matrices}{185}
\contentsline {section}{\numberline {21.5}Counting letters}{186}
\contentsline {section}{\numberline {21.6}Glossary}{187}
\contentsline {section}{\numberline {21.7}Laboratory exercises}{188}
\contentsline {chapter}{\numberline {22}System programming}{189}
\contentsline {section}{\numberline {22.1}The sys module and argv}{189}
\contentsline {section}{\numberline {22.2}The \texttt {os} and \texttt {glob} module}{190}
\contentsline {section}{\numberline {22.3}A mini-case study}{192}
\contentsline {subsection}{\numberline {22.3.1}ImageMagick}{192}
\contentsline {subsection}{\numberline {22.3.2}Scripting ImageMagick}{193}
\contentsline {section}{\numberline {22.4}Glossary}{196}
\contentsline {section}{\numberline {22.5}Laboratory exercises}{197}
\contentsline {chapter}{\numberline {23}Classes and objects}{199}
\contentsline {section}{\numberline {23.1}Object-oriented programming}{199}
\contentsline {section}{\numberline {23.2}User-defined compound types}{199}
\contentsline {section}{\numberline {23.3}The \texttt {\_\_init\_\_} Method and \texttt {self}}{200}
\contentsline {section}{\numberline {23.4}Attributes}{200}
\contentsline {section}{\numberline {23.5}Methods}{201}
\contentsline {section}{\numberline {23.6}Sameness}{202}
\contentsline {section}{\numberline {23.7}Rectangles}{203}
\contentsline {section}{\numberline {23.8}Instances as return values}{204}
\contentsline {section}{\numberline {23.9}Objects are mutable}{205}
\contentsline {section}{\numberline {23.10}Copying}{205}
\contentsline {section}{\numberline {23.11}Glossary}{206}
\contentsline {section}{\numberline {23.12}Laboratory exercises}{208}
\contentsline {chapter}{\numberline {24}Case study 2}{211}
\contentsline {section}{\numberline {24.1}Program Design}{211}
\contentsline {section}{\numberline {24.2}The Initial Program}{212}
\contentsline {section}{\numberline {24.3}Opening a File}{212}
\contentsline {section}{\numberline {24.4}Saving to a File}{214}
\contentsline {section}{\numberline {24.5}Encrypting the Contents}{214}
\contentsline {section}{\numberline {24.6}Putting it All Together}{217}
\contentsline {section}{\numberline {24.7}Glossary}{219}
\contentsline {section}{\numberline {24.8}Laboratory Exercises}{220}
\contentsline {chapter}{\numberline {25}The last lecture}{221}
\contentsline {section}{\numberline {25.1}Stuff we haven't covered}{221}
\contentsline {section}{\numberline {25.2}What you can expect from COMP160 and Computer Science}{221}
\contentsline {chapter}{Extra Extension Exercises from Part 1}{223}
\contentsline {chapter}{GNU Free Documentation License}{227}
\contentsline {section}{1. APPLICABILITY AND DEFINITIONS}{227}
\contentsline {section}{2. VERBATIM COPYING}{228}
\contentsline {section}{3. COPYING IN QUANTITY}{229}
\contentsline {section}{4. MODIFICATIONS}{229}
\contentsline {section}{5. COMBINING DOCUMENTS}{230}
\contentsline {section}{6. COLLECTIONS OF DOCUMENTS}{231}
\contentsline {section}{7. AGGREGATION WITH INDEPENDENT WORKS}{231}
\contentsline {section}{8. TRANSLATION}{231}
\contentsline {section}{9. TERMINATION}{231}
\contentsline {section}{10. FUTURE REVISIONS OF THIS LICENSE}{232}
\contentsline {section}{ADDENDUM: How to use this License for your documents}{232}
