|
| 1 | +Preface |
| 2 | +======= |
| 3 | + |
| 4 | +Computers have revolutionised mathematics, as well as the many scientific, |
| 5 | +engineering and economic fields in which mathematics is applied. In |
| 6 | +the applications of mathematics the role of computation has long been obvious |
| 7 | +and prominent. Now, the development of theorem proving software is increasing |
| 8 | +the prominence of computing in pure mathematics. What this means is that |
| 9 | +the ability to write computer programs is an indispensible part of the toolkit |
| 10 | +of a 21st century mathematician and, indeed, scientist or engineer. |
| 11 | + |
| 12 | +Regrettably, university courses in mathematics and its sibling disciplines have |
| 13 | +often failed to reflect this revolution in the way that mathematics is |
| 14 | +practised. Far too often the sum total of programming education in an |
| 15 | +undergraduate degree is a computational methods module in which students are |
| 16 | +shown elementary programming constructs such as functions, loops, and perhaps |
| 17 | +plotting. These are introduced in the context of solving particular |
| 18 | +computational mathematics or statistics problems, and the programming |
| 19 | +constructs introduced are driven by what is needed to solve those problems. |
| 20 | + |
| 21 | +The assumptions underpinning this approach seem to be some combination of a |
| 22 | +belief that maths degrees should only teach maths, as well a feeling that the |
| 23 | +mathematical algorithms are some how the difficult part and that programming is |
| 24 | +a mere technical skill that students will somehow pick up along the way. The |
| 25 | +regrettable consequence of this approach is that many maths students and |
| 26 | +graduates end up without the programming, software development, and debugging |
| 27 | +skills that they need to make effective use of computers in their further |
| 28 | +studies or working careers. |
| 29 | + |
| 30 | +What is this book for? |
| 31 | +---------------------- |
| 32 | + |
| 33 | +This book is the result of a significant change in the mathematics curriculum |
| 34 | +at Imperial College London. Rather than assume that students will somehow |
| 35 | +acquire programming skills along the way, we have introduced first and second |
| 36 | +year courses with the sole objective of teaching students to programme well. |
| 37 | +This book is the text for the second of these courses. Named "Principles of |
| 38 | +Programming", the course aims to take students with a knowledge of basic Python |
| 39 | +programming (functions, loops, plotting and so forth) and introduce them to |
| 40 | +higher level programming concepts. The course has also been taught to masters |
| 41 | + |
| 42 | +The objective of this course is to graduate better programmers, so material on |
| 43 | +new programming constructs and concepts is accompanied by chapters on good |
| 44 | +programming style, so that students learn how to write code that they and |
| 45 | +others can understand, and on errors, exceptions and debugging, so that |
| 46 | +students learn how to get themselves out of trouble. |
| 47 | + |
| 48 | +An underlying theme throughout this book is that programming has strong |
| 49 | +connections with mathematics. In particular, both mathematics and programming |
| 50 | +depend on building higher level, more abstract objects which encapsulate and |
| 51 | +hide the underlying complexity of operations. It is in taking this mathematical |
| 52 | +approach to programming that this book is "for mathematicians". |
| 53 | + |
| 54 | +The examples are chosen from across mathematics. For example, it appears to be |
| 55 | +traditional in object oriented programming books to use a telephone directory |
| 56 | +as the initial example of classes. We eschew this in favour of a class |
| 57 | +implementing polynomials. This provides the opportunity to introduce |
| 58 | +encapsulation and operator overloading for a familiar mathematical object. In |
| 59 | +contrast to the traditional computational methods courses, the examples are |
| 60 | +chosen to illustrate and explain the programming concepts we study rather than |
| 61 | +the converse. |
| 62 | + |
| 63 | +Who is this book for? |
| 64 | +--------------------- |
| 65 | + |
| 66 | +This book is for anyone with mathematical, scientific, or engineering interests |
| 67 | +who would like to learn to be a more capable programmer. The mathematical |
| 68 | +examples assume that you know how to differentiate functions of one variable, |
| 69 | +but very little beyond that. This is not an introduction to basic Python: it's |
| 70 | +assumed that the reader knows the sort of basic Python usually covered in a |
| 71 | +first programming or computational methods course. In particular the reader |
| 72 | +will be assumed to be familiar with writing functions, variable assignments, |
| 73 | +loops, and list comprehensions. The reader is also assumed to have used numeric |
| 74 | +and string data values, as well as dictionaries, lists, and tuples. |
| 75 | + |
| 76 | +Many introductory Python courses exclusively use Jupyter notebooks, so nothing |
| 77 | +beyond that is assumed. Getting set up with a working Python installation is |
| 78 | +covered in :numref:`introduction` while the Python command line and using a |
| 79 | +text editor to create programmes in files are introduced from scratch in |
| 80 | +:numref:`programs_files`. |
| 81 | + |
| 82 | +How to use this book |
| 83 | +-------------------- |
| 84 | + |
| 85 | +You can, of course, simply sit down and read this book from cover to cover, or |
| 86 | +dip in to see what it has to say on particular subjects. However, reading a |
| 87 | +book about programming will not teach you to program. For that, you need to get |
| 88 | +your hands dirty writing code and debugging your mistakes. To help with this |
| 89 | +there is a book website at: |
| 90 | + |
| 91 | + `https://object-oriented-programming.github.io |
| 92 | + <https://object-oriented-programming.github.io>`__ |
| 93 | + |
| 94 | +As well as a web version of the full text of the book, the website contains |
| 95 | +videos and the code needed to do the programming exercises at the end of each |
| 96 | +chapter. |
| 97 | + |
| 98 | +The videos |
| 99 | +.......... |
| 100 | + |
| 101 | +The videos were created to accompany the course at Imperial College London. |
| 102 | +They're not primarily lecture videos but are instead practical demonstrations |
| 103 | +of the programming concepts being introduced at the relevant point. Usually |
| 104 | +it's better to watch the video *after* reading the relevant section. |
| 105 | + |
| 106 | +.. only:: latex |
| 107 | + |
| 108 | + The videos are marked in the text by a blue box at the right hand side, |
| 109 | + containing the video number. This corresponds to the list of links at: |
| 110 | + |
| 111 | + `https://object-oriented-programming.github.io/edition1/videos.html |
| 112 | + <https://object-oriented-programming.github.io/edition1/videos.html>`__ |
| 113 | + |
| 114 | +The exercises |
| 115 | +............. |
| 116 | + |
| 117 | +At the end of each chapter are exercises. These usually depend on a skeleton |
| 118 | +code which is available on GitHub. Sometimes you might be asked to complete a piece of code while on other |
| 119 | +occasions you'll need to write a whole Python module from scratch. Each set of |
| 120 | +exercises will come with a matching set of tests. These are small programs which |
| 121 | +check whether your code produces the correct responses to a range of inputs. |
| 122 | +Tests like this provide immediate feedback and enable you to know how you are |
| 123 | +doing. Links to the skeleton code for each chapter are provided at: |
| 124 | + |
| 125 | + `https://object-oriented-programming.github.io/edition1/exercises.html |
| 126 | + <https://object-oriented-programming.github.io/edition1/exercises.html>`__ |
| 127 | + |
| 128 | +The mechanism for accessing the exercises will be covered in |
| 129 | +:numref:`introduction`. |
| 130 | + |
| 131 | +Conventions employed |
| 132 | +-------------------- |
| 133 | + |
| 134 | +Each chapter starts by introducing new material, supported by the videos and |
| 135 | +exercises. At the end of each chapter is a glossary containing many of the key |
| 136 | +concepts introduced in that chapter. Terms to be found in a glossary are given |
| 137 | +*in italics* and can be looked up in the index. |
| 138 | + |
| 139 | +.. only:: not book |
| 140 | + |
| 141 | + Python has excellent `official online documentation |
| 142 | + <https://docs.python.org/3/>`_, and we link to that throughout the text. |
| 143 | + External links show up in orange while :ref:`internal links to other parts |
| 144 | + of the notes <introduction>` are red. |
| 145 | + |
| 146 | +The text sometimes introduces counterexamples: illustrations of code errors or |
| 147 | +bad implementation ideas. These will be flagged with a big red cross: |
| 148 | + |
| 149 | +.. container:: badcode |
| 150 | + |
| 151 | + .. code-block:: python3 |
| 152 | +
|
| 153 | + print "Hello World" |
| 154 | +
|
| 155 | +Conversely, if it's necessary in context to highlight which approach is the |
| 156 | +correct one, the code will come with a big green tick: |
| 157 | + |
| 158 | +.. container:: goodcode |
| 159 | + |
| 160 | + .. code-block:: python3 |
| 161 | +
|
| 162 | + print("Hello World") |
| 163 | +
|
| 164 | +
|
| 165 | +Teaching this course elsewhere |
| 166 | +------------------------------ |
| 167 | + |
| 168 | +The course of which this book forms the text has been given to master's |
| 169 | +students at the University of Oxford, as well as to undergraduate students at |
| 170 | +Imperial College London. Instructors are welcome to use this material to teach |
| 171 | +elsewhere, and are encouraged to contact the author for assistance with access |
| 172 | +to materials. |
| 173 | + |
| 174 | +Acknowledgements |
| 175 | +---------------- |
| 176 | + |
| 177 | +The course Principles of Programming, and the notes on which this book is |
| 178 | +based, were first delivered in spring 2020, when university teaching was |
| 179 | +completely online during the COVID pandemic. I'd like to thank teaching fellow |
| 180 | +Dr Matthew Woolway who worked tirelessly with me on the module and who put |
| 181 | +together many of the tests on the exercies, and the graduate teaching |
| 182 | +assistants Miguel Boland, Sophia Vorderwuelbecke and Connor Ward whose |
| 183 | +professionalism in delivering the course in very complex circumstances was |
| 184 | +outstanding. Pulling out all the stops to deliver the written and video |
| 185 | +materials for online learning meant a lot of evenings and weekends. I am |
| 186 | +exceptionally grateful to my wife Gebina Ham for disproportionately picking up |
| 187 | +our childcare responsibilities in that period in order to make this possible. |
| 188 | + |
| 189 | +This is a textbook about programming in Python, so it would be remiss of me not |
| 190 | +to also thank the developers of the Python language, its CPython reference |
| 191 | +implementation, and all the third party packages which on which this book |
| 192 | +depends. In that regard, the developers of Numpy, Flake8, Pytest, PDB++ and |
| 193 | +IPython deserve particular mention. |
| 194 | + |
| 195 | +This book is typeset using the Sphinx documentation system. Among other things |
| 196 | +this facilitates generating the web, PDF, and print versions of the book from a |
| 197 | +single source. Thanks are due to its authors as well as those of the underlying |
| 198 | +LaTeX and TeX typesetting systems. |
0 commit comments