Statistics
11
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-02-25

AuthorBill Lubanovic

Stuck in a coding conundrum? Whether you're an advanced beginner, an intermediate developer, or a curious newcomer, the complexities of coding can often feel like a labyrinth with no exit. With Python, however, you can start writing real code quickly—but where should you start? In this updated third edition, Bill Lubanovic acts as your personal guide to Python, offering a clear path through the intricacies and capabilities of this much-beloved coding language, including new chapters on AI models and performance enhancements. Easy to understand and enjoyable to read, this book not only teaches you the core concepts but also dives into practical applications that bridge the gap between learning and doing. By reading it, you will: Understand everything from basic data structures to advanced features Gain insights into using Python for files, networking, databases, and data science Learn testing, debugging, code reuse, and other essential development tips Explore how Python can be utilized in business, science, and the arts

Tags
No tags
ISBN: 1098174402
Publisher: O'Reilly Media
Publish Year: 2025
Language: 英文
Pages: 661
File Format: PDF
File Size: 10.8 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Bill Lubanovic Introducing Python Modern Computing in Simple Packages 3rd Edition
9 7 8 1 0 9 8 1 7 4 4 0 8 5 4 9 9 9 ISBN: 978-1-098-17440-8 US $49.99 CAN $62.99 PY THON / PROGR AMMING The complexities of coding can often feel like a labyrinth with no exit. Hence the popularity of Python, which is among the easiest programming languages to learn, read, and write. Introducing Python has already proven to be an integral resource for advanced beginner and intermediate developers. Now in its third edition, with new chapters covering the discovery, use, and evaluation of real-world AI models and recent ways to remarkably improve Python’s performance, Bill Lubanovic’s book guides you through the intricacies and capabilities of this much-loved coding language. Easy to understand and enjoyable to read, Introducing Python not only teaches you core concepts but also features detailed, project-oriented walk-throughs of application areas such as the web, databases, networks, and more. • Understand Python data structures and operations • Write correct—and readable—Python code • Read the Python code of others—because developers do a lot of that • Take on workhorse jobs like web and database development • Try some exciting new things, like current AI models Bill Lubanovic has been a developer for over 40 years, specializing in Linux, the web, and Python. This is his third edition of Introducing Python, and he also authored FastAPI and coauthored Linux System Administration (all for O’Reilly). Bill lives with his family and cat in the Sangre de Sasquatch mountains of Minnesota. Introducing Python “Bill Lubanovic has achieved a tour de force, laying down the foundations for programming and then teaching you how to deal with real-life problems through the huge Python toolbox. This book is a sure path for learning how to solve problems the Python way.” Loïc Pefferkorn, open source systems engineer “This book ranges over an immense amount of the Python programming language and third-party packages. Delivered in a witty, conversational style, it entertains and informs.” Nathan Stocks, Agile Perception
Praise for Introducing Python, Third Edition Bill Lubanovic has achieved a tour de force, laying down the foundations for programming and then teaching you how to deal with real-life problems through the huge Python toolbox. This book is a sure path for learning how to solve problems the Python way. —Loïc Pefferkorn, open source systems engineer This book ranges over an immense amount of the Python programming language and third-party packages. Delivered in a witty, conversational style, it entertains and informs. —Nathan Stocks, Agile Perception The most valuable thing about this book is how it presents Python’s intersection with other crucial domains (AI, databases, webservers, and more) in today’s rapidly changing world. —Patrick Viafore, author of Robust Python (O’Reilly, 2021)
(This page has no text content)
Bill Lubanovic Introducing Python Modern Computing in Simple Packages THIRD EDITION
978-1-098-17440-8 [LSI] Introducing Python by Bill Lubanovic Copyright © 2025 Bill Lubanovic. All rights reserved. Published by O’Reilly Media, Inc., 141 Stony Circle, Suite 195, Santa Rosa, CA 95401. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (https://oreilly.com). For more information, contact our corporate/institu‐ tional sales department: 800-998-9938 or corporate@oreilly.com. Acquisitions Editor: Michelle Smith Development Editor: Corbin Collins Production Editor: Kristen Brown Copyeditor: Sharon Wilkey Proofreader: Piper Content Partners Indexer: BIM Creatives, LLC Cover Designer: Karen Montgomery Cover Illustrator: José Marzan Jr. Interior Designer: David Futato Interior Illustrator: Kate Dullea November 2014: First Edition November 2019: Second Edition September 2025: Third Edition Revision History for the Third Edition 2025-09-10: First Release See https://oreilly.com/catalog/errata.csp?isbn=9781098174408 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Introducing Python, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. The views expressed in this work are those of the author and do not represent the publisher’s views. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
With love to Lettie, Maeve, and Will. Oh, the things you’ll see!
(This page has no text content)
Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Part I. Stronghold 1. Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Mysteries 3 Little Python Programs 6 Setup 8 Install Python 9 Upgrade Python 9 Run Python Programs 9 The Python Interactive Interpreter 10 Python Files 10 Built-In Python Features 11 The Python Standard Library 11 Third-Party Python Packages 11 A Bigger Example 11 Review/Preview 14 2. Types and Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 A Computer 15 Bits and Bytes 16 Multibyte Types 19 Variables 21 Assign a Value to a Variable 21 Change the Value of a Variable 24 Delete a Variable 25 vii
Name Variables 26 Follow Naming Conventions 27 Python Types 27 Specify Values 28 Objects as Plastic Boxes in Memory 29 Review/Preview 29 Practice 29 3. Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Booleans 31 Integers 32 Literal Integers 33 Integer Operations 34 Integers and Variables 36 Precedence 38 Bases 39 Type Conversions 41 How Big Is an int? 43 Floats 44 Floats Are Not Exact 46 Fractions 46 Decimals 47 Math Functions 47 Review/Preview 47 Practice 47 4. Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Create with Quotes 50 Create with str() 52 Escape with \ 53 Combine with + 54 Duplicate with * 55 Get a Character by [ offset ] 55 Get a Substring with a Slice 56 Get Length with len() 58 Split with split() 59 Combine with join() 59 Substitute with replace() 59 Work with Prefixes and Suffixes 60 Strip with strip() 61 Search and Select 62 viii | Table of Contents
Change Case 63 Set Alignment 64 Apply Formatting 64 Old style: % 65 New style: {} and format() 68 Newest Style: f-strings 69 Review/Preview 70 Practice 71 5. Bytes and Bytearray. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Bytes 73 Create with Quotes 74 Create with bytes() 74 Create from a Hex String 75 Decode and Encode Bytes and Strings 76 Convert to a Hex String 76 Get One Byte by [ offset ] 76 Get a Slice 76 Combine with + 77 Repeat with * 77 Bytearray 77 Create with bytearray() 77 Get One Byte by [ offset ] 78 Get Multiple Bytes with a Slice 78 Modify One Byte by [ offset ] 78 Modify Multiple Bytes with replace() 78 Modify Multiple Bytes with a Slice 79 Insert a Byte with insert() 79 Append One Byte with append() 79 Append Multiple Bytes with extend() 79 Combine with + 80 Repeat with * 80 Review/Preview 80 Practice 80 6. If and Match. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Comment with # 81 Continue Lines with \ 82 Compare with if, elif, and else 83 What Is True? 86 Do Multiple Comparisons with in 87 Table of Contents | ix
New: I Am the Walrus 88 Match 89 Simple Matches 90 Structural Matches 91 Review/Preview 91 Practice 92 7. For and While. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Repeat with while 93 Cancel with break 94 Skip Ahead with continue 94 Check break Use with else 95 Iterate with for and in 95 Cancel with break 96 Skip with continue 96 Check break Use with else 96 Generate Number Sequences with range() 97 Review/Preview 98 Practice 98 8. Tuples and Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Tuples 100 Create with Commas and () 100 Create with tuple() 102 Get an Item by [ offset ] 102 Combine with + 102 Duplicate with * 102 Compare 102 Iterate with for and in 103 Modify? 103 Lists 104 Create with [] 104 Create or Convert with list() 104 Create from a String with split() 105 Get an Item by [ offset ] 105 Get Items with a Slice 106 Add an Item to the End with append() 107 Add an Item by Offset with insert() 107 Duplicate with * 108 Combine with extend() or + 108 Change an Item with [ offset ] 108 x | Table of Contents
Change Items with a Slice 109 Delete an Item by Offset with del 109 Delete an Item by Value with remove() 110 Get an Item by Offset and Delete It with pop() 110 Delete All Items with clear() 111 Find an Item’s Offset by Value with index() 111 Test for a Value with in 111 Count Occurrences of a Value with count() 112 Convert a List to a String with join() 112 Reorder Items with sort() or sorted() 113 Get Length with len() 113 Assign with = 114 Copy with copy(), list(), or a Slice 114 Copy Everything with deepcopy() 115 Compare Lists 116 Iterate with for and in 116 Iterate Multiple Sequences with zip() 117 Iterate Multiple Sequences with zip_longest() 118 Create a List with a Comprehension 119 Create Lists of Lists 121 Tuples Versus Lists 122 There Are No Tuple Comprehensions 122 Review/Preview 123 Practice 123 9. Dictionaries and Sets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Dictionaries 125 Create with {} 126 Create with dict() 126 Convert with dict() 127 Add or Change an Item by [ key ] 128 Get an Item by [ key ] or with get() 129 Iterate with for and in 130 Get Length with len() 131 Combine/update dicts 131 Delete an Item by Key with del 133 Get an Item by Key and Delete It with pop( key ) 133 Delete All Items with clear() 134 Test for a Key with in 134 Assign with = 134 Copy with copy() 135 Table of Contents | xi
Copy Everything with deepcopy() 135 Compare Dictionaries 136 Use Dictionary Comprehensions 137 Sets 138 Create with set() or {} 138 Get Length with len() 139 Add an Item with add() 140 Delete an Item with remove() 140 Combine with | 140 Iterate with for and in 140 Test for a Value with in 141 Use Combinations and Operators 141 Create Set Comprehensions 145 Create an Immutable Set with frozenset() 145 Review/Preview 146 Practice 146 10. Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Define a Function with def 149 Call a Function with Parentheses 150 Arguments and Parameters 151 None, Truthiness, and Falsiness 152 Positional Arguments 154 Keyword Arguments 155 Default Parameter Values 155 Pack/Unpack Positional Arguments with * 156 Pack/Unpack Keyword Arguments with ** 158 Keyword-Only (*) and Position-Only (/) Arguments 159 Mutable and Immutable Arguments 161 Docstrings 162 Functions Are First-Class Citizens 162 Function Arguments Are Not a Tuple 165 Inner Functions 165 Closures 166 Anonymous Functions: Lambda 167 Generators 168 Generator Functions 168 Generator Comprehensions 170 Decorators 170 Namespaces and Scope 173 Dunder Names 175 xii | Table of Contents
Recursion 175 Async Functions, Briefly 177 Exceptions 177 Handle Errors with try and except 178 Use Finally 179 Make Your Own Exceptions 180 Review/Preview 180 Practice 181 11. Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 What Are Objects? 183 Simple Objects 184 Define a Class with class 184 Assign Attributes 185 Methods 186 Initialization 186 Inheritance 188 Inherit from a Parent Class 188 Override a Method 189 Add a Method 191 Get Help from Your Parent with super() 191 Use Multiple Inheritance 192 Include Mixins 194 In self Defense 195 Attribute Access 195 Direct Access 195 Getters and Setters 196 Properties for Attribute Access 197 Properties for Computed Values 198 Name Mangling for Privacy 199 Class and Object Attributes 200 Method Types 201 Instance Methods 201 Class Methods 201 Static Methods 202 Duck Typing 202 Magic Methods 205 Aggregation and Composition 208 When to Use Objects or Something Else 208 Named Tuples 209 Dataclasses 211 Table of Contents | xiii
Attrs 212 Review/Preview 212 Practice 213 12. Modules and Packages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Modules and the import Statement 215 Import a Module 216 Import a Module with Another Name 218 Import Only What You Want from a Module 218 Packages 218 The Module Search Path 220 Relative and Absolute Imports 221 Namespace Packages 221 Modules Versus Objects 222 Goodies in the Python Standard Library 223 Handle Missing Keys with setdefault() and defaultdict() 223 Count Items with Counter() 225 Order by Key with OrderedDict() 227 Stack + Queue == deque 227 Iterate over Code Structures with itertools 228 Get Random 230 More Batteries: Get Other Python Code 231 Review/Preview 231 Practice 231 Part II. Tools 13. Development Environment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Find Python Code 235 Install Packages 236 Use Pip 236 Install with a Native Package Manager 237 Install from Source 238 Virtual Environments 238 Virtualenv and Venv 239 Pipenv 240 Poetry 240 Conda 240 uv 240 Integrated Development Environments 241 xiv | Table of Contents
IPython 242 Jupyter Notebook 244 JupyterLab 244 Source Control 244 Mercurial 245 Git 245 Review/Preview 248 Practice 248 14. Type Hints and Documentation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Type Hints 249 Variable Hints 250 Function Hints 251 Mypy 251 Documentation 252 Comments 253 Docstrings 253 Markup Text Files 253 Review/Preview 253 Practice 254 15. Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Pylint 255 Ruff 257 Unittest 258 Doctest 262 Pytest 263 Examples 264 Fixtures 265 Parametrization 267 Hypothesis 268 Nox 269 Continuous Integration 269 Review/Preview 270 Practice 270 16. Debugging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Assert 272 Print 273 F-strings 273 Pprint() 274 Table of Contents | xv
IceCream 274 Decorators 275 Logging 276 Pdb 279 Breakpoint() 284 Review/Preview 285 Practice 285 Part III. Quests 17. Text Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Text Strings: Unicode 289 Python Unicode Strings 290 UTF-8 293 Encode 294 Decode 295 HTML Entities 297 Normalization 298 Text Strings: Regular Expressions 299 Find Exact Beginning Match with match() 300 Find First Match with search() 302 Find All Matches with findall() 302 Split at Matches with split() 302 Replace at Matches with sub() 303 Patterns 303 Using Special Characters 303 Using Specifiers 305 Specifying match() Output 307 Review/Preview 308 Practice 308 18. Binary Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 Convert Binary Data with struct 309 Extraction with Binary Data Tools 312 Convert Bytes/Strings with binascii() 313 Use Bit Operators 313 Review/Preview 314 Practice 314 xvi | Table of Contents
19. Dates and Times. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 Leap Year 316 The datetime Module 317 The time Module 319 Read and Write Dates and Times 321 All the Conversions 325 Alternative Modules 326 Review/Preview 326 Practice 327 20. Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 File Input and Output 329 Create or Open with open() 330 Write a Text File with print() 331 Write a Text File with write() 331 Read a Text File with read(), readline(), or readlines() 333 Write a Binary File with write() 334 Read a Binary File with read() 335 Close Files Automatically by Using with 335 Change Position with seek() 335 Memory Mapping 337 File Operations 338 Check Existence with exists() 338 Check Type with isfile() 338 Copy with copy() 339 Change Name with rename() 339 Link with link() or symlink() 339 Change Permissions with chmod() 340 Change Ownership with chown() 340 Delete a File with remove() 340 Directory Operations 340 Create with mkdir() 341 Delete with rmdir() 341 List Contents with listdir() 341 Change Current Directory with chdir() 342 List Matching Files with glob() 342 Pathnames 343 Get a Pathname with abspath() 343 Get a symlink Pathname with realpath() 344 Build a Pathname with os.path.join() 344 Use pathlib 344 Table of Contents | xvii
BytesIO and StringIO 345 File Formats: Determination 347 Review/Preview 347 Practice 347 21. Data in Time: Concurrency. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 Programs and Processes 349 Create a Process with subprocess 350 Create a Process with multiprocessing 351 Kill a Process with terminate() 352 Get System Info with os 353 Get Process Info with psutil 353 Command Automation 354 Invoke 354 Other Command Helpers 355 Concurrency 356 Queues 357 Processes 357 Threads 359 The GIL 361 Concurrent.futures 362 Green Threads and gevent 365 Twisted 367 asyncio 369 Coroutines and Event Loops 370 Asyncio Alternatives 372 Async Versus… 373 Async Frameworks and Servers 374 Redis 375 Beyond Queues 378 Review/Preview 379 Practice 379 22. Data in Space: Networks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381 TCP/IP 381 Sockets 382 Scapy 387 Netcat 387 Networking Patterns 388 The Request-Reply Pattern 388 Request-Reply: ZeroMQ 389 xviii | Table of Contents