7
Programming in Python 3 A Complete Introduction to the Python Language Mark Summerfield .4.Addison-Wesley Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City

Programming in Python 3 - CERN · Programming in Python 3 A Complete Introduction to the Python Language Mark Summerfield.4.Addison-Wesley Upper Saddle River, NJ • Boston • Indianapolis

Embed Size (px)

Citation preview

Page 1: Programming in Python 3 - CERN · Programming in Python 3 A Complete Introduction to the Python Language Mark Summerfield.4.Addison-Wesley Upper Saddle River, NJ • Boston • Indianapolis

Programming in Python 3

A Complete Introduction to the Python Language

Mark Summerfield

.4.Addison-Wesley

Upper Saddle River, NJ • Boston • Indianapolis • San Francisco

New York • Toronto • Montreal • London • Munich • Paris • Madrid

Capetown • Sydney • Tokyo • Singapore • Mexico City

Page 2: Programming in Python 3 - CERN · Programming in Python 3 A Complete Introduction to the Python Language Mark Summerfield.4.Addison-Wesley Upper Saddle River, NJ • Boston • Indianapolis

Contents

Introduction 1

Chapter I. Rapid Introduction to Procedural Programming 7Creating and Running Python Programs 7Python's "Beautiful Heart" 12

Piece #1: Data Types 12Piece #2: Object References 14Piece #3: Collection Data Types 16Piece #4: Logical Operations 19Piece #5: Control Flow Statements 23Piece #6: Arithmetic Operators 28Piece #7: Input/Output 31Piece #8: Creating and Calling Functions 34

Examples 36bigdigits.py 36generate_grid.py 39

Summary 42Exercises 44

Chapter 2. Data Types 47Identifiers and Keywords 47Integral Types 50

Integers 51Booleans 53

Floating-Point Types 54Floating-Point Numbers 55Complex Numbers 58Decimal Numbers 59

Strings 61Comparing Strings 63Slicing and Striding Strings 64String Operators and Methods 67

vii

Page 3: Programming in Python 3 - CERN · Programming in Python 3 A Complete Introduction to the Python Language Mark Summerfield.4.Addison-Wesley Upper Saddle River, NJ • Boston • Indianapolis

String Formatting with the str.formatO Method 74

Character Encodings

85

Examples 88

quadratic.py

88

csv2html.py

90

Summary 95

Exercises

97

Chapter 3. Collection Data Types 99Sequence Types 99

Tuples 100Named Tuples 103Lists 104

Set Types 112Sets 112Frozen Sets 116

Mapping Types 117Dictionaries 118Default Dictionaries 126

Iterating and Copying Collections 127Iterators and Iterable Operations and Functions 128Copying Collections 136

Examples 138generate_usernames.py 138statistics.py 142

Summary 146Exercises 147

Chapter 4. Control Structures and Functions 149Control Structures 149

Conditional Branching 149Looping 151

Exception Handling 153Catching and Raising Exceptions 153Custom Exceptions 158

Custom Functions 161Names and Docstrings 166Argument and Parameter Unpacking 167Accessing Variables in the Global Scope 170

viii

Page 4: Programming in Python 3 - CERN · Programming in Python 3 A Complete Introduction to the Python Language Mark Summerfield.4.Addison-Wesley Upper Saddle River, NJ • Boston • Indianapolis

Lambda Functions 172Assertions 173

Example: make_html_skeleton.py 175Summary 181Exercise 182

Chapter 5. Modules 185Modules and Packages 185

Packages 189Custom Modules 192

Overview of Python's Standard Library 202String Handling 202Command-Line Programming 204Mathematics and Numbers 205Times and Dates 205Algorithms and Collection Data Types 207File Formats, Encodings, and Data Persistence 208File, Directory, and Process Handling 211Networking and Internet Programming 214XML 216Other Modules 217

Summary 219Exercise 220

Chapter 6. Object-Oriented Programming 223The Object-Oriented Approach 224

Object-Oriented Concepts and Terminology 225Custom Classes 228

Attributes and Methods 228Inheritance and Polymorphism 233Using Properties to Control Attribute Access 236Creating Complete and Fully Integrated Data Types 238

Custom Collection Classes 251Creating Classes That Aggregate Collections 251Creating Collection Classes Using Aggregation 258Creating Collection Classes Using Inheritance 265

Summary 272Exercises 274

ix

Page 5: Programming in Python 3 - CERN · Programming in Python 3 A Complete Introduction to the Python Language Mark Summerfield.4.Addison-Wesley Upper Saddle River, NJ • Boston • Indianapolis

Chapter 7. File Handling 277

Writing and Reading Binary Data 282

Pickles with Optional Compression 282

Raw Binary Data with Optional Compression 285

Writing and Parsing Text Files 294

Writing Text 295Parsing Text 296Parsing Text Using Regular Expressions 299

Writing and Parsing XML Files 302Element Trees 302DOM (Document Object Model) 306Manually Writing XML 309Parsing XML with SAX (Simple API for XML) 310

Random Access Binary Files 313A Generic BinaryRecordFile Class 314Example: The BikeStock Module's Classes 322

Summary 326Exercises 327

Chapter 8. Advanced Programming Techniques 329Further Procedural Programming 330

Branching Using Dictionaries 330Generator Expressions and Functions 331Dynamic Code Execution and Dynamic Imports 334Local and Recursive Functions 341Function and Method Decorators 346Function Annotations 350

Further Object-Oriented Programming 353Controlling Attribute Access 353Functors 357Context Managers 359Descriptors 362Class Decorators 367Abstract Base Classes 370Multiple Inheritance 377Metaclasses 380

Functional-Style Programming 384Partial Function Application 387

Page 6: Programming in Python 3 - CERN · Programming in Python 3 A Complete Introduction to the Python Language Mark Summerfield.4.Addison-Wesley Upper Saddle River, NJ • Boston • Indianapolis

Example: Valid.py 388Summary 390Exercises 392

Chapter 9. Processes and Threading 395Delegating Work to Processes 396Delegating Work to Threads 400

Example: A Threaded Find Word Program 401Example: A Threaded Find Duplicate Files Program 404

Summary 409Exercises 410

Chapter 10. Networking 413Creating a TCP Client 414Creating a TCP Server 420Summary 427Exercises 427

Chapter 11. Database Programming 431DBM Databases 432SQL Databases 436Summary 443Exercise 444

Chapter 12. Regular Expressions 445Python's Regular Expression Language 446

Characters and Character Classes 446Quantifiers 447Grouping and Capturing 450Assertions and Flags 451

The Regular Expression Module 455Summary 464Exercises 465

Chapter 13. Introduction to GUI Programming 467Dialog-Style Programs 470Main-Window-Style Programs 476

Creating a Main Window 477Creating a Custom Dialog 488

Summary 491

xi

Page 7: Programming in Python 3 - CERN · Programming in Python 3 A Complete Introduction to the Python Language Mark Summerfield.4.Addison-Wesley Upper Saddle River, NJ • Boston • Indianapolis

Exercises 491

Epilogue 493

Index 495

xli