Contents

  1. Cover Page
  2. About This eBook
  3. Halftitle Page
  4. Title Page
  5. Copyright Page
  6. Contents
  7. Preface
  8. 1. Python Basics
    1. 1.1 Running Python
    2. 1.2 Python Programs
    3. 1.3 Primitives, Variables, and Expressions
    4. 1.4 Arithmetic Operators
    5. 1.5 Conditionals and Control Flow
    6. 1.6 Text Strings
    7. 1.7 File Input and Output
    8. 1.8 Lists
    9. 1.9 Tuples
    10. 1.10 Sets
    11. 1.11 Dictionaries
    12. 1.12 Iteration and Looping
    13. 1.13 Functions
    14. 1.14 Exceptions
    15. 1.15 Program Termination
    16. 1.16 Objects and Classes
    17. 1.17 Modules
    18. 1.18 Script Writing
    19. 1.19 Packages
    20. 1.20 Structuring an Application
    21. 1.21 Managing Third-Party Packages
    22. 1.22 Python: It Fits Your Brain
  9. 2. Operators, Expressions, and Data Manipulation
    1. 2.1 Literals
    2. 2.2 Expressions and Locations
    3. 2.3 Standard Operators
    4. 2.4 In-Place Assignment
    5. 2.5 Object Comparison
    6. 2.6 Ordered Comparison Operators
    7. 2.7 Boolean Expressions and Truth Values
    8. 2.8 Conditional Expressions
    9. 2.9 Operations Involving Iterables
    10. 2.10 Operations on Sequences
    11. 2.11 Operations on Mutable Sequences
    12. 2.12 Operations on Sets
    13. 2.13 Operations on Mappings
    14. 2.14 List, Set, and Dictionary Comprehensions
    15. 2.15 Generator Expressions
    16. 2.16 The Attribute (.) Operator
    17. 2.17 The Function Call () Operator
    18. 2.18 Order of Evaluation
    19. 2.19 Final Words: The Secret Life of Data
  10. 3. Program Structure and Control Flow
    1. 3.1 Program Structure and Execution
    2. 3.2 Conditional Execution
    3. 3.3 Loops and Iteration
    4. 3.4 Exceptions
    5. 3.5 Context Managers and the with Statement
    6. 3.6 Assertions and __debug__
    7. 3.7 Final Words
  11. 4. Objects, Types, and Protocols
    1. 4.1 Essential Concepts
    2. 4.2 Object Identity and Type
    3. 4.3 Reference Counting and Garbage Collection
    4. 4.4 References and Copies
    5. 4.5 Object Representation and Printing
    6. 4.6 First-Class Objects
    7. 4.7 Using None for Optional or Missing Data
    8. 4.8 Object Protocols and Data Abstraction
    9. 4.9 Object Protocol
    10. 4.10 Number Protocol
    11. 4.11 Comparison Protocol
    12. 4.12 Conversion Protocols
    13. 4.13 Container Protocol
    14. 4.14 Iteration Protocol
    15. 4.15 Attribute Protocol
    16. 4.16 Function Protocol
    17. 4.17 Context Manager Protocol
    18. 4.18 Final Words: On Being Pythonic
  12. 5. Functions
    1. 5.1 Function Definitions
    2. 5.2 Default Arguments
    3. 5.3 Variadic Arguments
    4. 5.4 Keyword Arguments
    5. 5.5 Variadic Keyword Arguments
    6. 5.6 Functions Accepting All Inputs
    7. 5.7 Positional-Only Arguments
    8. 5.8 Names, Documentation Strings, and Type Hints
    9. 5.9 Function Application and Parameter Passing
    10. 5.10 Return Values
    11. 5.11 Error Handling
    12. 5.12 Scoping Rules
    13. 5.13 Recursion
    14. 5.14 The lambda Expression
    15. 5.15 Higher-Order Functions
    16. 5.16 Argument Passing in Callback Functions
    17. 5.17 Returning Results from Callbacks
    18. 5.18 Decorators
    19. 5.19 Map, Filter, and Reduce
    20. 5.20 Function Introspection, Attributes, and Signatures
    21. 5.21 Environment Inspection
    22. 5.22 Dynamic Code Execution and Creation
    23. 5.23 Asynchronous Functions and await
    24. 5.24 Final Words: Thoughts on Functions and Composition
  13. 6. Generators
    1. 6.1 Generators and yield
    2. 6.2 Restartable Generators
    3. 6.3 Generator Delegation
    4. 6.4 Using Generators in Practice
    5. 6.5 Enhanced Generators and yield Expressions
    6. 6.6 Applications of Enhanced Generators
    7. 6.7 Generators and the Bridge to Awaiting
    8. 6.8 Final Words: A Brief History of Generators and Looking Forward
  14. 7. Classes and Object-Oriented Programming
    1. 7.1 Objects
    2. 7.2 The class Statement
    3. 7.3 Instances
    4. 7.4 Attribute Access
    5. 7.5 Scoping Rules
    6. 7.6 Operator Overloading and Protocols
    7. 7.7 Inheritance
    8. 7.8 Avoiding Inheritance via Composition
    9. 7.9 Avoiding Inheritance via Functions
    10. 7.10 Dynamic Binding and Duck Typing
    11. 7.11 The Danger of Inheriting from Built-in Types
    12. 7.12 Class Variables and Methods
    13. 7.13 Static Methods
    14. 7.14 A Word about Design Patterns
    15. 7.15 Data Encapsulation and Private Attributes
    16. 7.16 Type Hinting
    17. 7.17 Properties
    18. 7.18 Types, Interfaces, and Abstract Base Classes
    19. 7.19 Multiple Inheritance, Interfaces, and Mixins
    20. 7.20 Type-Based Dispatch
    21. 7.21 Class Decorators
    22. 7.22 Supervised Inheritance
    23. 7.23 The Object Life Cycle and Memory Management
    24. 7.24 Weak References
    25. 7.25 Internal Object Representation and Attribute Binding
    26. 7.26 Proxies, Wrappers, and Delegation
    27. 7.27 Reducing Memory Use with __slots__
    28. 7.28 Descriptors
    29. 7.29 Class Definition Process
    30. 7.30 Dynamic Class Creation
    31. 7.31 Metaclasses
    32. 7.32 Built-in Objects for Instances and Classes
    33. 7.33 Final Words: Keep It Simple
  15. 8. Modules and Packages
    1. 8.1 Modules and the import Statement
    2. 8.2 Module Caching
    3. 8.3 Importing Selected Names from a Module
    4. 8.4 Circular Imports
    5. 8.5 Module Reloading and Unloading
    6. 8.6 Module Compilation
    7. 8.7 The Module Search Path
    8. 8.8 Execution as the Main Program
    9. 8.9 Packages
    10. 8.10 Imports Within a Package
    11. 8.11 Running a Package Submodule as a Script
    12. 8.12 Controlling the Package Namespace
    13. 8.13 Controlling Package Exports
    14. 8.14 Package Data
    15. 8.15 Module Objects
    16. 8.16 Deploying Python Packages
    17. 8.17 The Penultimate Word: Start with a Package
    18. 8.18 The Final Word: Keep It Simple
  16. 9. Input and Output
    1. 9.1 Data Representation
    2. 9.2 Text Encoding and Decoding
    3. 9.3 Text and Byte Formatting
    4. 9.4 Reading Command-Line Options
    5. 9.5 Environment Variables
    6. 9.6 Files and File Objects
    7. 9.7 I/O Abstraction Layers
    8. 9.8 Standard Input, Output, and Error
    9. 9.9 Directories
    10. 9.10 The print() function
    11. 9.11 Generating Output
    12. 9.12 Consuming Input
    13. 9.13 Object Serialization
    14. 9.14 Blocking Operations and Concurrency
    15. 9.15 Standard Library Modules
    16. 9.15.25 threading Module
    17. 9.16 Final Words
  17. 10. Built-in Functions and Standard Library
    1. 10.1 Built-in Functions
    2. 10.2 Built-in Exceptions
    3. 10.3 Standard Library
    4. 10.4 Final Words: Use the Built-Ins
  18. Index
  19. Code Snippets
  1. i
  2. ii
  3. iii
  4. iv
  5. v
  6. vi
  7. vii
  8. viii
  9. ix
  10. x
  11. xi
  12. xii
  13. xiii
  14. xiv
  15. 1
  16. 2
  17. 3
  18. 4
  19. 5
  20. 6
  21. 7
  22. 8
  23. 9
  24. 10
  25. 11
  26. 12
  27. 13
  28. 14
  29. 15
  30. 16
  31. 17
  32. 18
  33. 19
  34. 20
  35. 21
  36. 22
  37. 23
  38. 24
  39. 25
  40. 26
  41. 27
  42. 28
  43. 29
  44. 30
  45. 31
  46. 32
  47. 33
  48. 34
  49. 35
  50. 36
  51. 37
  52. 38
  53. 39
  54. 40
  55. 41
  56. 42
  57. 43
  58. 44
  59. 45
  60. 46
  61. 47
  62. 48
  63. 49
  64. 50
  65. 51
  66. 52
  67. 53
  68. 54
  69. 55
  70. 56
  71. 57
  72. 58
  73. 59
  74. 60
  75. 61
  76. 62
  77. 63
  78. 64
  79. 65
  80. 66
  81. 67
  82. 68
  83. 69
  84. 70
  85. 71
  86. 72
  87. 73
  88. 74
  89. 75
  90. 76
  91. 77
  92. 78
  93. 79
  94. 80
  95. 81
  96. 82
  97. 83
  98. 84
  99. 85
  100. 86
  101. 87
  102. 88
  103. 89
  104. 90
  105. 91
  106. 92
  107. 93
  108. 94
  109. 95
  110. 96
  111. 97
  112. 98
  113. 99
  114. 100
  115. 101
  116. 102
  117. 103
  118. 104
  119. 105
  120. 106
  121. 107
  122. 108
  123. 109
  124. 110
  125. 111
  126. 112
  127. 113
  128. 114
  129. 115
  130. 116
  131. 117
  132. 118
  133. 119
  134. 120
  135. 121
  136. 122
  137. 123
  138. 124
  139. 125
  140. 126
  141. 127
  142. 128
  143. 129
  144. 130
  145. 131
  146. 132
  147. 133
  148. 134
  149. 135
  150. 136
  151. 137
  152. 138
  153. 139
  154. 140
  155. 141
  156. 142
  157. 143
  158. 144
  159. 145
  160. 146
  161. 147
  162. 148
  163. 149
  164. 150
  165. 151
  166. 152
  167. 153
  168. 154
  169. 155
  170. 156
  171. 157
  172. 158
  173. 159
  174. 160
  175. 161
  176. 162
  177. 163
  178. 164
  179. 165
  180. 166
  181. 167
  182. 168
  183. 169
  184. 170
  185. 171
  186. 172
  187. 173
  188. 174
  189. 175
  190. 176
  191. 177
  192. 178
  193. 179
  194. 180
  195. 181
  196. 182
  197. 183
  198. 184
  199. 185
  200. 186
  201. 187
  202. 188
  203. 189
  204. 190
  205. 191
  206. 192
  207. 193
  208. 194
  209. 195
  210. 196
  211. 197
  212. 198
  213. 199
  214. 200
  215. 201
  216. 202
  217. 203
  218. 204
  219. 205
  220. 206
  221. 207
  222. 208
  223. 209
  224. 210
  225. 211
  226. 212
  227. 213
  228. 214
  229. 215
  230. 216
  231. 217
  232. 218
  233. 219
  234. 220
  235. 221
  236. 222
  237. 223
  238. 224
  239. 225
  240. 226
  241. 227
  242. 228
  243. 229
  244. 230
  245. 231
  246. 232
  247. 233
  248. 234
  249. 235
  250. 236
  251. 237
  252. 238
  253. 239
  254. 240
  255. 241
  256. 242
  257. 243
  258. 244
  259. 245
  260. 246
  261. 247
  262. 248
  263. 249
  264. 250
  265. 251
  266. 252
  267. 253
  268. 254
  269. 255
  270. 256
  271. 257
  272. 258
  273. 259
  274. 260
  275. 261
  276. 262
  277. 263
  278. 264
  279. 265
  280. 266
  281. 267
  282. 268
  283. 269
  284. 270
  285. 271
  286. 272
  287. 273
  288. 274
  289. 275
  290. 276
  291. 277
  292. 278
  293. 279
  294. 280
  295. 281
  296. 282
  297. 283
  298. 284
  299. 285
  300. 286
  301. 287
  302. 288
  303. 289
  304. 290
  305. 291
  306. 292
  307. 293
  308. 294
  309. 295
  310. 296
  311. 297
  312. 298
  313. 299
  314. 300
  315. 301
  316. 302
  317. 303
  318. 304
  319. 305
  320. 306
  321. 307
  322. 308
  323. 309
  324. 310
  325. 311
  326. 312
  327. 313
  328. 314
  329. 315
  330. 316
  331. 317
  332. 318
  333. 319
  334. 320
  335. 321
  336. 322
  337. 323
  338. 324
  339. 325
  340. 326
  341. 327
  342. 328
  343. 329
  344. 330
  345. 331
  346. 332
  347. 333
  348. 334
  349. 335
  350. 336
  351. 337
  352. 338
  353. 339
  354. 340