The Quick Python Book. 4 Ed

The Quick Python Book. 4 Ed

The Quick Python Book. 4 Ed

Автор: Naomi Ceder
Дата выхода: 2025
Издательство: Manning Publications Co.
Количество страниц: 584
Размер файла: 2,4 МБ
Тип файла: PDF
Добавил: codelibs
 Проверить на вирусы  Дополнительные материалы 

foreword xviii
preface xx
acknowledgments xxii
about this book xxiv
about the author xxix
about the cover illustration xxx
Part 1 Starting out 1
1 About Python 3
1.1 Why should I use Python? 4
1.2 What Python does well 4
Python is easy to use 4 

Python is expressive 5

Python is readable 5

Python is complete: “Batteries included” 6
Python has a rich ecosystem of third-party libraries 7
Python is cross-platform 7

Python is free 7
1.3 What Python is improving 8
Python is getting faster 8

Python doesn’t enforce variable types at compile time 8

Python is improving mobile support 9
Python is improving support for multiple processors 9
2 Getting started 11
2.1 Paradox of choice: Which Python? 12
Python versions 12

Sources of Python 12

Devices, platforms, and operating systems 13 

Environments and tools 13
2.2 Colaboratory: Jupyter notebooks in the cloud 13
Getting the source notebooks 14

Getting started with Colaboratory 14

Colaboratory’s Python version 16
2.3 Writing and running code in Colaboratory 17
Hello, World 18

Dealing with errors in Jupyter 18
2.4 Using help and dir to explore Python 19
2.5 Using AI tools to write Python code 22
Benefits of AI tools 22

Negatives of using AI tools 22
AI options 23
3 The quick Python overview 24
3.1 Python synopsis 25
3.2 Built-in data types 25
Numbers 25

Lists 27

Tuples 30

Strings 30
Dictionaries 31

Sets, frozensets 32

File objects 32
3.3 Type hints in Python 34
3.4 Control flow structures 34
Boolean values and expressions 34

The if-elif-else statement 34
Structural pattern matching with match 35

The while loop 35
The for loop 36

Function definition 36

Exceptions 37
Context handling using the with keyword 38
3.5 Module creation 38
3.6 Object-oriented programming 40
Part 2 The essentials 43
4 The absolute basics 45
4.1 Indentation and block structuring 45
4.2 Differentiating comments 47
4.3 Variables and assignments 47
4.4 Optional type hints in Python 50
Why use type hints? 51

Why not use type hints? 51
Progressive typing 51
4.5 Expressions 52
4.6 Strings 52
4.7 Numbers 53
Built-in numeric functions 55

Advanced numeric
functions 55

Numeric computation 55

Complex numbers 55

Advanced complex-number functions 56
4.8 The None value 57
4.9 Getting input from the user 58
4.10 Built-in operators 58
4.11 Basic Python style 58
5 Lists, tuples, and sets 61
5.1 Lists are like arrays 62
5.2 List indices 63
5.3 Modifying lists 65
5.4 Sorting lists 68
Custom sorting 69

The sorted () function 70
5.5 Other common list operations 71
List membership with the in operator 71

List concatenation with the + operator 72

List initialization with the * operator 72

List minimum or maximum with min and max 72

List search with index 73

List matches with count 73

Summary of list operations 74
5.6 Nested lists and deep copies 75
5.7 Tuples 77
Tuple basics 77

One-element tuples need a comma 79
Packing and unpacking tuples 80

Converting between lists and tuples 82
5.8 Sets 82
Set operations 82

Frozen sets 83
5.9 Lab: Examining a list 84
Why solve it the old-fashioned way? 84

Solving the problem with AI code generation 85

Solutions and discussion 86
6 Strings 89
6.1 Strings as sequences of characters 89
6.2 Basic string operations 90
6.3 Special characters and escape sequences 91
Basic escape sequences 91

Numeric (octal and hexadecimal) and Unicode escape sequences 92

Printing vs. evaluating
strings with special characters 93
6.4 String methods 94
The split and join string methods 94

Converting strings to numbers 96

Getting rid of extra whitespace 97

String searching 99

Modifying strings 101

Modifying strings with list manipulations 102

Useful methods and constants 103
6.5 Converting objects to strings 105
6.6 Using the format method 106
The format method and positional parameters 106

The format method and named parameters 107

Format specifiers 107
6.7 String interpolation with f-strings 108
6.8 Formatting strings with % 109
Using formatting sequences 110

Named parameters and formatting sequences 111
6.9 Bytes 112
6.10 Preprocessing text 113
Solving the problem with AI-generated code 114

Solutions and discussion 114
7 Dictionaries 118
7.1 What is a dictionary? 119
7.2 Other dictionary operations 121
7.3 Word counting 124
7.4 What can be used as a key? 125
7.5 Sparse matrices 126
7.6 Dictionaries as caches 127
7.7 Efficiency of dictionaries 128
7.8 Word counting 128
Solving the problem with AI-generated code 129

Solutions and discussion 129
8 Control flow 133
8.1 The if-elif-else statement 133
8.2 Structural pattern matching with match 135
8.3 The while loop 135
8.4 The for loop 137
The range function 137
8.5 Controlling range with starting and stepping values 138
8.6 The for loop and tuple unpacking 139
8.7 The enumerate function 139
8.8 The zip function 140
8.9 List, set, and dictionary comprehensions 140
Generator expressions 142
8.10 Statements, blocks, and indentation 142
8.11 Boolean values and expressions 145
Most Python objects can be used as Booleans 146

Comparison and Boolean operators 146
8.12 Writing a simple program to analyze a text file 148
8.13 Refactoring word_count 149
Solving the problem with AI-generated code 149

Solutions and discussion 149
9 Functions 153
9.1 Basic function definitions 153
9.2 Function parameter options 155
Positional parameters 155

Passing arguments by parameter name 156

Variable numbers of arguments 157
Mixing argument-passing techniques 158
9.3 Mutable objects as arguments 159
Mutable objects as default values 160
9.4 Local, nonlocal, and global variables 161
9.5 Assigning functions to variables 164
9.6 lambda expressions 164
9.7 Generator functions 165
9.8 Decorators 167
9.9 Useful functions 168
Solving the problem with AI-generated code 168

Solutions and discussion 168
10 Modules and scoping rules 176
10.1 What is a module? 177
10.2 A first module 177
10.3 The import statement 181
10.4 The module search path 182
Where to place your own modules 183
10.5 Private names in modules 184
10.6 Library and third-party modules 185
10.7 Python scoping rules and namespaces 186
The built-in namespace 190
10.8 Creating a module 193
Solving the problem with AI-generated code 193

Solutions and discussion 194
11 Python programs 203
11.1 Creating a very basic program 204
Starting a script from a command line 204

Command-line arguments 205

Executing code only as main script 206
Redirecting the input and output of a script 206

The argparse module 208

Using the fileinput module 209
11.2 Running scripts in different operating systems 211
Making a script directly executable on UNIX 211

Scripts on macOS 212

Script execution options in Windows 212
11.3 Programs and modules 213
11.4 Distributing Python applications 219
Wheels packages 219

zipapp and pex 219

py2exe and py2app 220

Creating executable programs with freeze 220
11.5 Creating a program 220
Solving the problem with AI-generated code 220

Solutions and discussion 221
12 Using the filesystem 228
12.1 os and os.path vs. pathlib 229
12.2 Paths and pathnames 229
Absolute and relative paths 230

The current working directory 231

Accessing directories with pathlib 232
Manipulating pathnames 232

Manipulating pathnames with pathlib 235

Useful constants and functions 236
12.3 Getting information about files 237
Getting information about files with scandir 239
12.4 More filesystem operations 239
More filesystem operations with pathlib 241
12.5 Processing all files in a directory subtree 244
12.6 More file operations 245
Solving the problem with AI-generated code 245

Solutions and discussion 246
13 Reading and writing files 256
13.1 Opening files and file objects 257
13.2 Closing files 257
13.3 Opening files in write or other modes 258
13.4 Functions to read and write text or binary data 258
Using binary mode 260
13.5 Reading and writing with pathlib 261
13.6 Terminal input/output and redirection 262
13.7 Handling structured binary data with the struct module 265
13.8 Pickling objects to files 267
Reasons not to pickle 271
13.9 Shelving objects 271
13.10 Final fixes to wc 273
Solving the problem with AI-generated code 274

Solutions and discussion 274
14 Exceptions 281
14.1 Introduction to exceptions 282
General philosophy of errors and exception handling 282
A more formal definition of exceptions 284

Handling different types of exceptions 284
14.2 Exceptions in Python 285
Types of Python exceptions 286

Raising exceptions 288
Catching and handling exceptions 289

Defining new exceptions 290

Exception groups 292

Debugging programs with the assert statement 292

The exception inheritance hierarchy 293

Example: A disk-writing program in Python 294

Example: Exceptions in normal evaluation 295
Where to use exceptions 296
14.3 Context managers using the with keyword 296
14.4 Adding exceptions 298
Solving the problem with AI-generated code 298

Solutions and discussion 298
Part 3 Advanced language features 307
15 Classes and object-oriented programming 309
15.1 Defining classes 310
Using a class instance as a structure or record 310
15.2 Instance variables 311
15.3 Methods 312
15.4 Class variables 314
An oddity with class variables 315
15.5 Static methods and class methods 316
Static methods 316

Class methods 318
15.6 Inheritance 319
15.7 Inheritance with class and instance variables 321
15.8 Recap: Basics of Python classes 322
15.9 Private variables and private methods 325
15.10 Using @property for more flexible instance variables 326
15.11 Scoping rules and namespaces for class instances 328
15.12 Destructors and memory management 332
15.13 Multiple inheritance 332
15.14 HTML classes 334
Solving the problem with AI-generated code 334

Solutions and discussion 334
16 Regular expressions 339
16.1 What is a regular expression? 340
16.2 Regular expressions with special characters 340
16.3 Regular expressions and raw strings 341
Raw strings to the rescue 342
16.4 Extracting matched text from strings 343
16.5 Substituting text with regular expressions 347
Using a function with sub 347
16.6 Phone number normalizer 348
Solving the problem with AI-generated code 349

Solutions and discussion 349
17 Data types as objects 355
17.1 Types are objects too 356
17.2 Using types 356
17.3 Types and user-defined classes 357
17.4 Duck typing 359
17.5 What is a special method attribute? 360
17.6 Making an object behave like a list 361
17.7 The __getitem__ special method attribute 362
How it works 363

Implementing full list functionality 364
17.8 Giving an object full list capability 364
17.9 Subclassing from built-in types 367
Subclassing list 367

Subclassing UserList 368
17.10 When to use special method attributes 369
17.11 Creating a string-only key-value dictionary 370
Solving the problem with AI-generated code 370

Solutions and discussion 371
18 Packages 375
18.1 What is a package? 376
18.2 A first example: mathproj 376
18.3 Implementing the mathproj package 377
__init__.py files in packages 379

Basic use of the mathproj package 380

Loading subpackages and submodules 380
import statements within packages 381
18.4 The __all__ attribute 382
18.5 Proper use of packages 383
18.6 Creating a package 384
Solving the problem with AI-generated code 384

Solutions and discussion 384
19 Using Python libraries 397
19.1 “Batteries included”: The standard library 398
Managing various data types 398

Manipulating files and storage 400

Accessing operating system services 400
Using internet protocols and formats 401

Development and debugging tools and runtime services 402
19.2 Moving beyond the standard library 403
19.3 Adding more Python libraries 403
19.4 The Python Package Index 403
19.5 Installing Python libraries using pip and venv 404
Installing with the --user flag 405

Virtual environments 405
Other options 406
Part 4 Working with data.................................... 409
20 Basic file wrangling 411
20.1 The problem: The never-ending flow of data files 411
20.2 Scenario: The product feed from hell 412
20.3 More organization 414
20.4 Saving storage space: Compression and grooming 416
Compressing files 416

Grooming files 418
21 Processing data files 420
21.1 Welcome to ETL 420
21.2 Reading text files 421
Text encoding: ASCII, Unicode, and others 421

Unstructured text 423

Delimited flat files 425

The csv module 427
Reading a csv file as a list of dictionaries 430
21.3 Excel files 430
21.4 Data cleaning 432
Cleaning 432

Sorting 433

Data cleaning problems and pitfalls 435
21.5 Writing data files 435
CSV and other delimited files 435

Writing Excel files 437
Packaging data files 437
21.6 Weather observations 437
Solving the problem with AI-generated code 438

Solutions and discussion 438
22 Data over the network 443
22.1 Fetching files 443
Using Python to fetch files from an FTP server 444

Fetching files with SFTP 445

Retrieving files over HTTP/HTTPS 446
22.2 Fetching data via an API 447
22.3 Structured data formats 450
JSON data 450

XML data 454
22.4 Scraping web data 457
22.5 Tracking the weather 461
Solving the problem with AI-generated code 461

Solutions and discussion 461
23 Saving data 466
23.1 Relational databases 467
The Python database API 467
23.2 SQLite: Using the sqlite3 database 467
23.3 Using MySQL, PostgreSQL, and other relational databases 470
23.4 Making database handling easier with an object relational mapper 470
SQLAlchemy 471

Using Alembic for database schema changes 475
23.5 NoSQL databases 478
23.6 Key-value stores with Redis 478
23.7 Documents in MongoDB 482
23.8 Creating a database 486
24 Exploring data 488
24.1 Python tools for data exploration 488
Python’s advantages for exploring data 489

Python can be better than a spreadsheet 489
24.2 Python and pandas 489
Why you might want to use pandas 489

Installing pandas 490

Data frames 490
24.3 Data cleaning 492
Loading and saving data with pandas 492

Data cleaning with a data frame 495
24.4 Data aggregation and manipulation 497
Merging data frames 497

Selecting data 498
Grouping and aggregation 500
24.5 Plotting data 501
24.6 Why you might not want to use pandas 502
Case study 504
appendix A guide to Python’s documentation 521
index 539

 A fast-paced introduction to Python for intermediate developers–now with coverage of generative AI!

 For over 25 years, The Quick Python Book has been one of the best Python books money can buy. It concisely covers programming basics, while introducing Python's comprehensive standard library and unique features in depth and detail. In this fourth edition, you’ll find new coverage of AI coding tools like Copilot and Google's Colaboratory (Colab), and develop a mindset that can make the most of AI.

The Quick Python Book, Fourth Edition includes:

  • Python syntax, data structures, and best practices
  • Python as an object oriented language
  • Common Python libraries
  • Basic data handling with Python
  • Using AI code generation tools with Python

 Whether you’re new to Python or looking to advance your basic skills, The Quick Python Book, Fourth Edition will get you writing effective Python code fast. Python authority and former Chair of the Python Software Foundation Board or Directors Naomi Ceder has returned to author this extensively revised fourth edition. With the personal touch of a skilled teacher, Naomi beautifully balances details of the language with the insights and advice you need to handle any task.

About the technology

 System automation. High-performance web apps. Cloud and back-end services. Cutting edge AI. No matter what you’re building, it pays to know how to read and write Python! The Quick Python Book has helped over 100,000 developers get up to speed with the Python programming language. This revised Fourth Edition, fully updated for Python 3.13, explores the latest features and libraries and shows you how to code smarter with AI tools like ChatGPT.

About the book

 The Quick Python Book, Fourth Edition teaches you the essential Python features and techniques you need for most common scripting, application programming, and data science tasks. Written for developers comfortable with another programming language, it dives right into the good stuff. New interactive notebooks, quick-check questions, and end-of-chapter labs all help practice and consolidate your new skills. Plus, you’ll find practical advice on writing prompts and using AI assistants to accelerate your day-to-day work.

What's inside

  • Python syntax, data structures, and best practices
  • Object-oriented Python
  • Must-know Python libraries
  • Data handling

About the reader

 For beginning-intermediate programmers. No prior experience with Python required.


Похожее:

Список отзывов:

Нет отзывов к книге.