126x Filetype PDF File size 0.37 MB Source: gameprogrammingpatterns.com
Conceptual Programming
with Python
Thorsten Altenkirch Isaac Triguero
©2019Thorsten&Isaac(StandardCopyrightLicence)
ISBN978-0-244-82276-7
ii
Contents
1 Introduction 1
1.1 WhyPython? . . . . . . . . . . . . . . . . . . . . 1
1.2 HowtousePython? . . . . . . . . . . . . . . . . 3
1.3 Wheretofindmoreinformation? . . . . . . . . . 3
1.4 Overviewoverthebook . . . . . . . . . . . . . . 4
1.5 Abouttheauthors . . . . . . . . . . . . . . . . . . 5
1.6 Acknowledgments . . . . . . . . . . . . . . . . . 6
2 Python from the top-level 9
2.1 Basic types and operations . . . . . . . . . . . . . 9
2.1.1 Coercions . . . . . . . . . . . . . . . . . . 11
2.1.2 Bool . . . . . . . . . . . . . . . . . . . . . . 13
2.1.3 Functions . . . . . . . . . . . . . . . . . . 15
2.2 Datastructures . . . . . . . . . . . . . . . . . . . 18
2.2.1 Strings . . . . . . . . . . . . . . . . . . . . 18
2.2.2 Lists . . . . . . . . . . . . . . . . . . . . . 21
2.2.3 Modifyinglists . . . . . . . . . . . . . . . 25
2.3 Mysteries of Python . . . . . . . . . . . . . . . . . 31
2.4 Summary . . . . . . . . . . . . . . . . . . . . . . . 33
2.5 Solution to top-level challenge . . . . . . . . . . . 35
2.6 Quizzes . . . . . . . . . . . . . . . . . . . . . . . . 35
2.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . 36
3 Imperative Programming 37
3.1 Blocks of Code . . . . . . . . . . . . . . . . . . . . 37
3.2 Inputs/Outputs . . . . . . . . . . . . . . . . . . . 38
3.3 Error handling . . . . . . . . . . . . . . . . . . . . 44
3.4 Loops . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.4.1 Anexampleoftheuseofwhileloops . . 46
3.5 TheHaltingproblem . . . . . . . . . . . . . . . . 48
3.6 Iterating through data structures . . . . . . . . . 51
iii
Contents
3.7 Theguessinggame . . . . . . . . . . . . . . . . . 56
3.8 Summary . . . . . . . . . . . . . . . . . . . . . . . 63
3.8.1 Conditionals . . . . . . . . . . . . . . . . . 63
3.8.2 HandlingExceptions . . . . . . . . . . . . 66
3.8.3 Whileloops . . . . . . . . . . . . . . . . . 66
3.8.4 For loops . . . . . . . . . . . . . . . . . . . 67
3.9 Solution to Challenges . . . . . . . . . . . . . . . 69
3.9.1 Challenge 1 imperative programming . . 69
3.9.2 Challenge 2 imperative programming . . 70
3.10 Quizzes . . . . . . . . . . . . . . . . . . . . . . . . 71
3.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . 72
4 Recursion and backtracking 75
4.1 Prelude: functions calling functions . . . . . . . 75
4.2 TheTowerofHanoi . . . . . . . . . . . . . . . . . 76
4.3 Howisrecursionexecuted? . . . . . . . . . . . . 82
4.4 Somecombinatorics . . . . . . . . . . . . . . . . . 86
4.4.1 Factorial . . . . . . . . . . . . . . . . . . . 86
4.4.2 Binomial coefficents . . . . . . . . . . . . 88
4.5 Solving sudoku: Using backtracking . . . . . . . 89
4.6 Summary . . . . . . . . . . . . . . . . . . . . . . . 98
4.7 Solution to recursion challenge . . . . . . . . . . 98
4.8 Quizzes . . . . . . . . . . . . . . . . . . . . . . . . 101
4.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . 102
5 Object Oriented Programming 105
5.1 First example: a class for accounts . . . . . . . . 107
5.1.1 Operations on objects . . . . . . . . . . . 109
5.1.2 Class variables . . . . . . . . . . . . . . . 114
5.1.3 Inheritance . . . . . . . . . . . . . . . . . . 117
5.1.4 The__str__method . . . . . . . . . . . 120
5.2 Example: ImplementingExpressions . . . . . . . 123
5.2.1 Printing expressions . . . . . . . . . . . . 129
5.2.2 Evaluate expressions . . . . . . . . . . . . 131
5.3 Example: Creating a Knowledge Base . . . . . . 136
5.4 Summary . . . . . . . . . . . . . . . . . . . . . . . 146
5.4.1 Classes . . . . . . . . . . . . . . . . . . . . 146
5.4.2 Objects . . . . . . . . . . . . . . . . . . . . 146
iv
no reviews yet
Please Login to review.