Preface....6
Contents....8
Chapter 1 Basics of Python....10
1.1 Basic Arithmetic....10
1.2 Integers, Reals and Complex Numbers....13
1.3 Objects and their Types....19
1.4 Importing Libraries in Python....22
1.5 Variables in Python....23
1.6 Equalities and Boolean Statements....24
1.7 Strings....27
1.8 Strings as Objects....28
1.9 Input and Output....29
Problems....31
Chapter 2 Lists and Tuples....32
2.1 Data: Lists....32
2.1.1 Accessing entries of a list....33
2.2 Lists as Objects....38
2.3 A First Glimpse of Graphics....47
2.4 A First Glimpse of Importing Data....48
2.5 Tuples....50
2.6 More Examples of Working with Data....51
2.6.1 Languages....51
Problems....55
Chapter 3 Decisions and Repetitions....57
3.1 Decision Making: The Fork on the Road....57
3.2 Decision Making: The Forks on the Road....60
3.3 Loops and Repetitions....64
3.3.1 For-loop....64
3.3.2 Nested For-loops....79
3.3.3 While Loops....84
Problems....89
Chapter 4 Functions....92
4.1 Functions....92
4.1.1 The scope of functions....96
4.1.2 Functions, default values....108
4.1.3 Functions, specific types....109
4.2 Functional Programming: Anonymous (lambda) Functions....110
4.2.1 Selecting from a collection....117
4.2.2 Functional programming, reduce....120
Problems....126
Chapter 5 List Comprehension and Generators....129
5.1 List Comprehension....129
5.1.1 Putting conditions on parameters....131
5.2 Sets and Dictionaries....137
5.2.1 Sets....137
5.2.2 Dictionaries....145
5.3 Generators....150
5.3.1 Generator functions....153
Problems....156
Chapter 6 The sympy Library....159
6.1 sympy, Symbolic Python....159
6.2 Graphics in sympy....164
6.3 Three-Dimensional Graphs....171
6.4 Calculus with sympy....176
6.4.1 Solving equations....176
6.4.2 Limits, derivation and integration....179
Problems....186
Chapter 7 The numpy Library....189
7.1 numpy, Numerical Python....189
7.1.1 Calculus on arrays....190
7.1.2 Generating arrays....191
7.1.3 Accessing entries of an array....193
7.1.4 Vector calculus with arrays....199
7.2 Universal Functions....213
Problems....214
Chapter 8 The matplotlib Library and Projects....216
8.1 matplotlib, Plotting Data....216
8.2 Plots as Objects....223
8.3 Animation....227
8.4 Case Study, Persian Carpets....233
8.5 Case Study, Predictive Estimation....239
8.6 Case Study, the Thue–Morse Sequence....247
Problems....249
Further Reading....252
Index....254
This textbook introduces Python and its programming through a multitude of clearly presented examples and worked-out exercises.
Based on a course taught to undergraduate students of mathematics, science, engineering and finance, the book includes chapters on handling data, calculus, solving equations, and graphics, thus covering all of the basic topics in Python. Each section starts with a description of a new topic and some basic examples. The author then demonstrates the new concepts through worked out exercises. The intention is to enable the reader to learn from the codes, thus avoiding lengthy, exhausting explanations. With its strong focus on programming and problem solving, and an emphasis on numerical problems that do not require advanced mathematics, this textbook is also ideal for self-study, for instance for researchers who wish to use Python as a computational tool.