eBook
Related collections and offers
Overview
Portable, powerful, and a breeze to use, Python is ideal for both standalone programs and scripting applications. With this hands-on book, you can master the fundamentals of the core Python language quickly and efficiently, whether you're new to programming or just new to Python. Once you finish, you will know enough about the language to use it in any application domain you choose.
Learning Python is based on material from author Mark Lutz's popular training courses, which he's taught over the past decade. Each chapter is a self-contained lesson that helps you thoroughly understand a key component of Python before you continue. Along with plenty of annotated examples, illustrations, and chapter summaries, every chapter also contains Brain Builder, a unique section with practical exercises and review quizzes that let you practice new skills and test your understanding as you go.
This book covers:
- Types and Operations -- Python's major built-in object types in depth: numbers, lists, dictionaries, and more
- Statements and Syntax -- the code you type to create and process objects in Python, along with Python's general syntax model
- Functions -- Python's basic procedural tool for structuring and reusing code
- Modules -- packages of statements, functions, and other tools organized into larger components
- Classes and OOP -- Python's optional object-oriented programming tool for structuring code for customization and reuse
- Exceptions and Tools -- exception handling model and statements, plus a look at development tools for writing larger programs
Product Details
ISBN-13: | 9780596554491 |
---|---|
Publisher: | O'Reilly Media, Incorporated |
Publication date: | 10/22/2007 |
Sold by: | Barnes & Noble |
Format: | eBook |
Pages: | 752 |
File size: | 7 MB |
About the Author
Mark is the author of the three O’Reilly books: Learning Python, Programming Python, and Python Pocket Reference, all currently in fourth or fifth editions. He has been using and promoting Python since 1992, started writing Python books in 1995, and began teaching Python classes in 1997. As of Spring 2013, Mark has instructed 260 Python training sessions, taught roughly 4,000 students in live classes, and written Python books that have sold 400,000 units and been translated to at least a dozen languages.
Together, his two decades of Python efforts have helped to establish it as one of the most widely used programming languages in the world today. In addition, Mark has been in the software field for 30 years. He holds BS and MS degrees in computer science from the University of Wisconsin where he explored implementations of the Prolog language, and over his career has worked as a professional software developer on compilers, programming tools, scripting applications, and assorted client/server systems.
Mark maintains a training website (http://learning-python.com) and an additional book support site on the Web (http://www.rmi.net/~lutz).
Table of Contents
Preface | xv | |
Part I. | Getting Started | |
1. | A Python Q&A Session | 3 |
Why Do People Use Python? | 3 | |
Is Python a Scripting Language? | 5 | |
Okay, But What's the Downside? | 6 | |
Who Uses Python Today? | 7 | |
What Can I Do with Python? | 8 | |
What Are Python's Technical Strengths? | 11 | |
How Does Python Stack Up to Language X? | 14 | |
2. | How Python Runs Programs | 16 |
Introducing the Python Interpreter | 16 | |
Program Execution | 17 | |
Execution Model Variations | 21 | |
3. | How You Run Programs | 25 |
Interactive Coding | 25 | |
System Command Lines and Files | 28 | |
Clicking Windows File Icons | 32 | |
Module Imports and Reloads | 36 | |
The IDLE User Interface | 40 | |
Other IDEs | 44 | |
Embedding Calls | 46 | |
Frozen Binary Executables | 47 | |
Text Editor Launch Options | 47 | |
Other Launch Options | 47 | |
Future Possibilities? | 47 | |
Which Option Should I Use? | 48 | |
Part I Exercises | 48 | |
Part II. | Types and Operations | |
4. | Numbers | 53 |
Python Program Structure | 53 | |
Why Use Built-in Types? | 53 | |
Numbers | 55 | |
Python Expression Operators | 57 | |
Numbers in Action | 60 | |
The Dynamic Typing Interlude | 68 | |
5. | Strings | 74 |
String Literals | 75 | |
Strings in Action | 81 | |
String Formatting | 87 | |
String Methods | 90 | |
General Type Categories | 95 | |
6. | Lists and Dictionaries | 97 |
Lists | 97 | |
Lists in Action | 99 | |
Dictionaries | 103 | |
Dictionaries in Action | 105 | |
7. | Tuples, Files, and Everything Else | 112 |
Tuples | 112 | |
Files | 115 | |
Type Categories Revisited | 117 | |
Object Generality | 118 | |
References Versus Copies | 118 | |
Comparisons, Equality, and Truth | 121 | |
Python's Type Hierarchies | 123 | |
Other Types in Python | 125 | |
Built-in Type Gotchas | 125 | |
Part II Exercises | 127 | |
Part III. | Statements and Syntax | |
8. | Assignment, Expressions, and Print | 133 |
Assignment Statements | 134 | |
Expression Statements | 140 | |
Print Statements | 142 | |
9. | if Tests | 146 |
if Statements | 146 | |
Python Syntax Rules | 149 | |
Truth Tests | 152 | |
10. | while and for Loops | 155 |
while Loops | 155 | |
break, continue, pass, and the Loop else | 156 | |
for Loops | 160 | |
Loop Variations | 164 | |
11. | Documenting Python Code | 171 |
The Python Documentation Interlude | 171 | |
Common Coding Gotchas | 182 | |
Part III Exercises | 184 | |
Part IV. | Functions | |
12. | Function Basics | 189 |
Why Use Functions? | 189 | |
Coding Functions | 190 | |
A First Example: Definitions and Calls | 193 | |
A Second Example: Intersecting Sequences | 194 | |
13. | Scopes and Arguments | 197 |
Scope Rules | 197 | |
The global Statement | 202 | |
Scopes and Nested Functions | 203 | |
Passing Arguments | 207 | |
Special Argument Matching Modes | 210 | |
14. | Advanced Function Topics | 219 |
Anonymous Functions: lambda | 219 | |
Applying Functions to Arguments | 224 | |
Mapping Functions Over Sequences | 227 | |
Functional Programming Tools | 228 | |
List Comprehensions | 229 | |
Generators and Iterators | 233 | |
Function Design Concepts | 236 | |
Function Gotchas | 239 | |
Part IV Exercises | 242 | |
Part V. | Modules | |
15. | Modules: The Big Picture | 247 |
Why Use Modules? | 247 | |
Python Program Architecture | 248 | |
How Imports Work | 251 | |
16. | Module Coding Basics | 257 |
Module Creation | 257 | |
Module Usage | 258 | |
Module Namespaces | 261 | |
Reloading Modules | 266 | |
17. | Module Packages | 270 |
Package Import Basics | 270 | |
Package Import Example | 273 | |
Why Use Package Imports? | 275 | |
A Tale of Three Systems | 275 | |
18. | Advanced Module Topics | 279 |
Data Hiding in Modules | 279 | |
Enabling Future Language Features | 280 | |
Mixed Usage Modes: __name__ and __main__ | 280 | |
Changing the Module Search Path | 281 | |
The import as Extension | 282 | |
Module Design Concepts | 282 | |
Module Gotchas | 285 | |
Part V Exercises | 292 | |
Part VI. | Classes and OOP | |
19. | OOP: The Big Picture | 297 |
Why Use Classes? | 297 | |
OOP from 30,000 Feet | 299 | |
20. | Class Coding Basics | 307 |
Classes Generate Multiple Instance Objects | 307 | |
Classes Are Customized by Inheritance | 310 | |
Classes Can Intercept Python Operators | 314 | |
21. | Class Coding Details | 317 |
The Class Statement | 317 | |
Methods | 320 | |
Inheritance | 322 | |
Operator Overloading | 327 | |
Namespaces: The Whole Story | 337 | |
22. | Designing with Classes | 343 |
Python and OOP | 343 | |
Classes as Records | 344 | |
OOP and Inheritance: "is-a" Relationships | 346 | |
OOP and Composition: "has-a" Relationships | 348 | |
OOP and Delegation | 352 | |
Multiple Inheritance | 353 | |
Classes Are Objects: Generic Object Factories | 356 | |
Methods Are Objects: Bound or Unbound | 358 | |
Documentation Strings Revisited | 360 | |
Classes Versus Modules | 362 | |
23. | Advanced Class Topics | 363 |
Extending Built-in Types | 363 | |
Pseudo-Private Class Attributes | 366 | |
"New Style" Classes in Python 2.2 | 369 | |
Class Gotchas | 376 | |
Part VI Exercises | 384 | |
Part VII. | Exceptions and Tools | |
24. | Exception Basics | 393 |
Why Use Exceptions? | 393 | |
Exception Handling: The Short Story | 395 | |
The try/except/else Statement | 399 | |
The try/finally Statement | 404 | |
The raise Statement | 405 | |
The assert Statement | 408 | |
25. | Exception Objects | 410 |
String-Based Exceptions | 410 | |
Class-Based Exceptions | 411 | |
General raise Statement Forms | 418 | |
26. | Designing with Exceptions | 420 |
Nesting Exception Handlers | 420 | |
Exception Idioms | 423 | |
Exception Design Tips | 426 | |
Exception Gotchas | 430 | |
Core Language Summary | 432 | |
Part VII Exercises | 436 | |
Part VIII. | The Outer Layers | |
27. | Common Tasks in Python | 439 |
Conversions, Numbers, and Comparisons | 443 | |
Manipulating Strings | 447 | |
Data Structure Manipulations | 452 | |
Manipulating Files and Directories | 458 | |
Internet-Related Modules | 473 | |
Executing Programs | 477 | |
Debugging, Testing, Timing, Profiling | 480 | |
Exercises | 483 | |
28. | Frameworks | 485 |
An Automated Complaint System | 486 | |
Interfacing with COM: Cheap Public Relations | 492 | |
A Tkinter-Based GUI Editor for Managing Form Data | 496 | |
Jython: The Felicitous Union of Python and Java | 504 | |
Exercises | 511 | |
29. | Python Resources | 513 |
Layers of Community | 513 | |
The Process | 517 | |
Services and Products | 518 | |
The Legal Framework: The Python Software Foundation | 518 | |
Software | 518 | |
Popular Third-Party Software | 520 | |
Web Application Frameworks | 529 | |
Tools for Python Developers | 531 | |
Part IX. | Appendixes | |
A. | Installation and Configuration | 535 |
B. | Solutions to Exercises | 541 |
Index | 577 |