Severance, Charles

Python for Everybody : Exploring Data Using Python 3 Charles Severance, - Mumbai Shroff Publishers & Distributors Pvt. Ltd. 2020 - 235

1 Why should you learn to write programs? 1
1.1 Creativity and motivation . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Computer hardware architecture . . . . . . . . . . . . . . . . . . . 3
1.3 Understanding programming . . . . . . . . . . . . . . . . . . . . . 4
1.4 Words and sentences . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5 Conversing with Python . . . . . . . . . . . . . . . . . . . . . . . . 6
1.6 Terminology: interpreter and compiler . . . . . . . . . . . . . . . . 8
1.7 Writing a program . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.8 What is a program? . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.9 The building blocks of programs . . . . . . . . . . . . . . . . . . . . 11
1.10 What could possibly go wrong? . . . . . . . . . . . . . . . . . . . . 12
1.11 The learning journey . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.12 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.13 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2 Variables, expressions, and statements 17
2.1 Values and types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.3 Variable names and keywords . . . . . . . . . . . . . . . . . . . . . 19
2.4 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.5 Operators and operands . . . . . . . . . . . . . . . . . . . . . . . . 20
2.6 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.7 Order of operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.8 Modulus operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.9 String operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.10 Asking the user for input . . . . . . . . . . . . . . . . . . . . . . . 22
v
vi CONTENTS
2.11 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.12 Choosing mnemonic variable names . . . . . . . . . . . . . . . . . . 24
2.13 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.14 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.15 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3 Conditional execution 29
3.1 Boolean expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.2 Logical operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.3 Conditional execution . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.4 Alternative execution . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.5 Chained conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.6 Nested conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.7 Catching exceptions using try and except . . . . . . . . . . . . . . 34
3.8 Short-circuit evaluation of logical expressions . . . . . . . . . . . . 35
3.9 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.10 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
4 Functions 41
4.1 Function calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.2 Built-in functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.3 Type conversion functions . . . . . . . . . . . . . . . . . . . . . . . 42
4.4 Random numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.5 Math functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
4.6 Adding new functions . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.7 Definitions and uses . . . . . . . . . . . . . . . . . . . . . . . . . . 46
4.8 Flow of execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
4.9 Parameters and arguments . . . . . . . . . . . . . . . . . . . . . . 48
4.10 Fruitful functions and void functions . . . . . . . . . . . . . . . . . 49
4.11 Why functions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
4.12 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
4.13 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.14 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
CONTENTS vii
5 Iteration 55
5.1 Updating variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
5.2 The while statement . . . . . . . . . . . . . . . . . . . . . . . . . . 55
5.3 Infinite loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
5.4 “Infinite loops” and break ....................... 56
5.5 Finishing iterations with continue .................. 57
5.6 Definite loops using for ........................ 58
5.7 Loop patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
5.7.1 Counting and summing loops . . . . . . . . . . . . . . . . . 59
5.7.2 Maximum and minimum loops . . . . . . . . . . . . . . . . 60
5.8 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
5.9 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
5.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
6 Strings 65
6.1 A string is a sequence . . . . . . . . . . . . . . . . . . . . . . . . . 65
6.2 Getting the length of a string using len ............... 66
6.3 Traversal through a string with a loop . . . . . . . . . . . . . . . . 66
6.4 String slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.5 Strings are immutable . . . . . . . . . . . . . . . . . . . . . . . . . 68
6.6 Looping and counting . . . . . . . . . . . . . . . . . . . . . . . . . 68
6.7 The in operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
6.8 String comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
6.9 string methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
6.10 Parsing strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
6.11 Format operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
6.12 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
6.13 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
6.14 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
7 Files 77
7.1 Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
7.2 Opening files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
7.3 Text files and lines . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
7.4 Reading files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
viii CONTENTS
7.5 Searching through a file . . . . . . . . . . . . . . . . . . . . . . . . . 81
7.6 Letting the user choose the file name . . . . . . . . . . . . . . . . . 83
7.7 Using try, except, and open .................... 84
7.8 Writing files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
7.9 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
7.10 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
7.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
8 Lists 89
8.1 A list is a sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
8.2 Lists are mutable . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
8.3 Traversing a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
8.4 List operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
8.5 List slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
8.6 List methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
8.7 Deleting elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
8.8 Lists and functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
8.9 Lists and strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
8.10 Parsing lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
8.11 Objects and values . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
8.12 Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
8.13 List arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
8.14 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
8.15 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
8.16 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
9 Dictionaries 105
9.1 Dictionary as a set of counters . . . . . . . . . . . . . . . . . . . . 107
9.2 Dictionaries and files . . . . . . . . . . . . . . . . . . . . . . . . . . 108
9.3 Looping and dictionaries . . . . . . . . . . . . . . . . . . . . . . . . 109
9.4 Advanced text parsing . . . . . . . . . . . . . . . . . . . . . . . . . . 111
9.5 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
9.6 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
9.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
CONTENTS ix
10 Tuples 115
10.1 Tuples are immutable . . . . . . . . . . . . . . . . . . . . . . . . . 115
10.2 Comparing tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
10.3 Tuple assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
10.4 Dictionaries and tuples . . . . . . . . . . . . . . . . . . . . . . . . . 119
10.5 Multiple assignment with dictionaries . . . . . . . . . . . . . . . . 120
10.6 The most common words . . . . . . . . . . . . . . . . . . . . . . . . 121
10.7 Using tuples as keys in dictionaries . . . . . . . . . . . . . . . . . . 122
10.8 Sequences: strings, lists, and tuples - Oh My! . . . . . . . . . . . . 122
10.9 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
10.10Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
10.11Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
11 Regular expressions 127
11.1 Character matching in regular expressions . . . . . . . . . . . . . . 128
11.2 Extracting data using regular expressions . . . . . . . . . . . . . . 129
11.3 Combining searching and extracting . . . . . . . . . . . . . . . . . 132
11.4 Escape character . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
11.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
11.6 Bonus section for Unix / Linux users . . . . . . . . . . . . . . . . . 137
11.7 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
11.8 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
11.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
12 Networked programs 141
12.1 HyperText Transport Protocol - HTTP . . . . . . . . . . . . . . . . 141
12.2 The World’s Simplest Web Browser . . . . . . . . . . . . . . . . . . 142
12.3 Retrieving an image over HTTP . . . . . . . . . . . . . . . . . . . 143
12.4 Retrieving web pages with urllib .................. 146
12.5 Parsing HTML and scraping the web . . . . . . . . . . . . . . . . . 147
12.6 Parsing HTML using regular expressions . . . . . . . . . . . . . . . 147
12.7 Parsing HTML using BeautifulSoup . . . . . . . . . . . . . . . . . 148
12.8 Reading binary files using urllib . . . . . . . . . . . . . . . . . . . . 150
12.9 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
12.10Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
x CONTENTS
13 Using Web Services 155
13.1 eXtensible Markup Language - XML . . . . . . . . . . . . . . . . . 155
13.2 Parsing XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
13.3 Looping through nodes . . . . . . . . . . . . . . . . . . . . . . . . . 157
13.4 JavaScript Object Notation - JSON . . . . . . . . . . . . . . . . . . 158
13.5 Parsing JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
13.6 Application Programming Interfaces . . . . . . . . . . . . . . . . . 159
13.7 Google geocoding web service . . . . . . . . . . . . . . . . . . . . . . 161
13.8 Security and API usage . . . . . . . . . . . . . . . . . . . . . . . . 163
13.9 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
13.10Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
14 Object-Oriented Programming 169
14.1 Managing Larger Programs . . . . . . . . . . . . . . . . . . . . . . 169
14.2 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
14.3 Using Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
14.4 Starting with Programs . . . . . . . . . . . . . . . . . . . . . . . . . 171
14.5 Subdividing a Problem - Encapsulation . . . . . . . . . . . . . . . 173
14.6 Our First Python Object . . . . . . . . . . . . . . . . . . . . . . . 174
14.7 Classes as Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
14.8 Object Lifecycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
14.9 Many Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
14.10Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
14.11Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
14.12Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
15 Using Databases and SQL 183
15.1 What is a database? . . . . . . . . . . . . . . . . . . . . . . . . . . 183
15.2 Database concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
15.3 Database Browser for SQLite . . . . . . . . . . . . . . . . . . . . . 184
15.4 Creating a database table . . . . . . . . . . . . . . . . . . . . . . . 184
15.5 Structured Query Language summary . . . . . . . . . . . . . . . . 187
15.6 Spidering Twitter using a database . . . . . . . . . . . . . . . . . . 189
15.7 Basic data modeling . . . . . . . . . . . . . . . . . . . . . . . . . . 194
15.8 Programming with multiple tables . . . . . . . . . . . . . . . . . . 195
CONTENTS xi
15.8.1 Constraints in database tables . . . . . . . . . . . . . . . . 198
15.8.2 Retrieve and/or insert a record . . . . . . . . . . . . . . . . 199
15.8.3 Storing the friend relationship . . . . . . . . . . . . . . . . . 200
15.9 Three kinds of keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
15.10Using JOIN to retrieve data . . . . . . . . . . . . . . . . . . . . . . 202
15.11Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
15.12Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
15.13Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
16 Visualizing data 207
16.1 Building a Google map from geocoded data . . . . . . . . . . . . . 207
16.2 Visualizing networks and interconnections . . . . . . . . . . . . . . 209
16.3 Visualizing mail data . . . . . . . . . . . . . . . . . . . . . . . . . . 212
A Contributions 219
A.1 Contributor List for Python for Everybody . . . . . . . . . . . . . 219
A.2 Contributor List for Python for Informatics . . . . . . . . . . . . . 219
A.3 Preface for “Think Python” . . . . . . . . . . . . . . . . . . . . . . 219
A.3.1 The strange history of “Think Python” . . . . . . . . . . . 219
A.3.2 Acknowledgements for “Think Python” . . . . . . . . . . . . 221
A.4 Contributor List for “Think Python” . . . . . . . . . . . . . . . . . . 221

Python for Everybody is designed to introduce students to programming and software development through the lens of exploring data. You can think of the Python programming language as your tool to solve data problems that are beyond the capability of a spreadsheet. Python is an easy to use and easy to learn programming language that is freely available on Macintosh, Windows, or Linux computers. So once you learn Python you can use it for the rest of your career without needing to purchase any software. This book uses the Python 3 language. The earlier Python 2 version of this book is titled ""Python for Informatics: Exploring Information."" There are free downloadable electronic copies of this book in various formats and supporting materials for the book at www.py4e.com. The course materials are available to you under a Creative Commons License so you can adapt them to teach your own Python course.

9789352136278

005.133 / SEV