Programming in Python 3: A Complete Introduction to the Python Language [NOOK Book]

Overview

A Fully Revised Edition Featuring New Material on Coroutines, Debugging, Testing, Parsing, String Formatting, and More

Python 3 is the best version of the language yet: It is more powerful, convenient, consistent, and expressive than ever before. Now, leading Python programmer Mark Summerfield demonstrates how to write code that takes full advantage of Python 3’s features and idioms. Programming in Python 3, Second Edition, brings together all the knowledge you need to write any...

See more details below
Programming in Python 3: A Complete Introduction to the Python Language

Available on NOOK devices and apps  
  • NOOK Devices
  • NOOK HD/HD+ Tablet
  • NOOK
  • NOOK Color
  • NOOK Tablet
  • Tablet/Phone
  • NOOK for Windows 8 Tablet
  • NOOK for iOS
  • NOOK for Android
  • NOOK Kids for iPad
  • PC/Mac
  • NOOK for Windows 8
  • NOOK for PC
  • NOOK for Mac
  • NOOK Study

Want a NOOK? Explore Now

NOOK Book (eBook)
$22.99
BN.com price
(Save 42%)$39.99 List Price

Overview

A Fully Revised Edition Featuring New Material on Coroutines, Debugging, Testing, Parsing, String Formatting, and More

Python 3 is the best version of the language yet: It is more powerful, convenient, consistent, and expressive than ever before. Now, leading Python programmer Mark Summerfield demonstrates how to write code that takes full advantage of Python 3’s features and idioms. Programming in Python 3, Second Edition, brings together all the knowledge you need to write any program, use any standard or third-party Python 3 library, and create new library modules of your own.

Summerfield draws on his many years of Python experience to share deep insights into Python 3 development you won’t find anywhere else. He begins by illuminating Python’s “beautiful heart”: the eight key elements of Python you need to write robust, high-performance programs. Building on these core elements, he introduces new topics designed to strengthen your practical expertise–one concept and hands-on example at a time. Coverage includes

  • Developing in Python using procedural, objectoriented, and functional programming paradigms
  • Creating custom packages and modules
  • Writing and reading binary, text, and XML files, including optional compression, random access, and text and XML parsing
  • Leveraging advanced data types, collections, control structures, and functions
  • Spreading program workloads across multiple processes and threads
  • Programming SQL databases and key—value DBM files
  • Debugging techniques–and using Test Driven Development to avoid bugs in the first place
  • Utilizing Python’s regular expression mini-language and module
  • Parsing techniques, including how to use the third-party PyParsing and PLY modules
  • Building usable, efficient, GUI-based applications
  • Advanced programming techniques, including generators, function and class decorators, context managers, descriptors, abstract base classes, metaclasses, coroutines, and more

Programming in Python 3, Second Edition, serves as both tutorial and language reference. It assumes some prior programming experience, and it is accompanied by extensive downloadable example code–all of it tested with Python 3 on Windows, Linux, and Mac OS X.

Read More Show Less

Editorial Reviews

From the Publisher

“…Fulfills the immediate market need for those developers seeking to learn this latest evolutionary version of the Python lineage in a succinct, well-written package.”

–Mike Riley, Contributing Editor for Dr. Dobb’s

“Beyond the introduction to programming in Python 3 in the first chapter, if you progress through the first six chapters in sequence, you’ll be well on your way to taking off with using Python independently.”

–James Pyles, Technical Writer and Author of the blog “A Million Chimpanzees”

“…A key recommendation for any serious computer library strong in web programming languages.”

–Jim Cox, Midwest Book Review

“Summerfield’s book is an excellent source to start learning Python 3.”

–Anthony J. Duben, Computing Reviews

Read More Show Less

Product Details

  • ISBN-13: 9780321699879
  • Publisher: Pearson Education
  • Publication date: 11/26/2009
  • Sold by: Barnes & Noble
  • Format: eBook
  • Edition number: 2
  • Pages: 648
  • Sales rank: 729,607
  • File size: 6 MB

Meet the Author

Mark Summerfield, owner of Qtrac Ltd., is an independent trainer, consultant, technical editor, and writer specializing in Python, C++, Qt, and PyQt. His books include Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming (Addison-Wesley, 2008) and, cowritten with Jasmin Blanchette, C++ GUI Programming with Qt 4 (Addison-Wesley, 2006). As Trolltech’s documentation manager, Mark founded and edited Trolltech’s technical journal, Qt Quarterly.

Read More Show Less

Table of Contents

List of Tables xv

Introduction 1

Chapter 1 : Rapid Introduction to Procedural Programming 9

Creating and Running Python Programs 9

Python’s “Beautiful Heart” 14

Examples 39

Summary 44

Exercises 47

Chapter 2 : Data Types 51

Identifiers and Keywords 51

Integral Types 54

Floating-Point Types 58

Strings 65

Examples 94

Summary 102

Exercises 104

Chapter 3 : Collection Data Types 107

Sequence Types 107

Set Types 120

Mapping Types 126

Iterating and Copying Collections 138

Examples 148

Summary 156

Exercises 158

Chapter 4 : Control Structures and Functions 159

Control Structures 159

Exception Handling 163

Custom Functions 171

Example: make_html_skeleton.py 185

Summary 191

Exercise 192

Chapter 5 : Modules 195

Modules and Packages 195

Overview of Python’s Standard Library 212

Summary 230

Exercise 231

Chapter 6 : Object-Oriented Programming 233

The Object-Oriented Approach 234

Custom Classes 238

Custom Collection Classes 261

Summary 283

Exercises 285

Chapter 7 : File Handling 287

Writing and Reading Binary Data 292

Writing and Parsing Text Files 305

Writing and Parsing XML Files 312

Random Access Binary Files 324

Summary 336

Exercises 337

Chapter 8 : Advanced Programming Techniques 339

Further Procedural Programming 340

Further Object-Oriented Programming 363

Functional-Style Programming 395

Example: Valid.py 407

Summary 410

Exercises 411

Chapter 9 : Debugging, Testing, and Profiling 413

Debugging 414

Unit Testing 425

Profiling 432

Summary 437

Chapter 10 : Processes and Threading 439

Using the Multiprocessing Module 440

Using the Threading Module 444

Summary 454

Exercises 455

Chapter 11 : Networking 457

Creating a TCP Client 458

Creating a TCP Server 464

Summary 471

Exercises 471

Chapter 12 : Database Programming 475

DBM Databases 476

SQL Databases 480

Summary 487

Exercise 488

Chapter 13 : Regular Expressions 489

Python’s Regular Expression Language 490

The Regular Expression Module 499

Summary 509

Exercises 510

Chapter 14 : Introduction to Parsing 513

BNF Syntax and Parsing Terminology 514

Writing Handcrafted Parsers 519

Pythonic Parsing with PyParsing 534

Lex/Yacc-Style Parsing with PLY 553

Summary 566

Exercise 568

Chapter 15 : Introduction to GUI Programming 569

Dialog-Style Programs 572

Main-Window-Style Programs 578

Summary 593

Exercises 593

Epilogue 595

Selected Bibliography 597

Index 599

Read More Show Less

Preface

Introduction

Python is probably the easiest-to-learn and nicest-to-use programming language in widespread use. Python code is clear to read and write, and is concise without being cryptic. Python is a very expressive language, which means that we can usually write far fewer lines of Python code than would be required for an equivalent application written in, say, C++ or Java.

Python is a cross-platform language: In general, the same Python program can be run on Windows and Unix-like systems such as Linux, BSD, and Mac OS X, simply by copying the file or files that make up the program to the target machine, with no “building” or compiling necessary. It is possible to create Python programs that use platform-specific functionality, but this is rarely necessary since almost all of Python’s standard library and most third-party libraries are fully and transparently cross-platform.

One of Python’s great strengths is that it comes with a very complete standard library—this allows us to do such things as download a file from the Internet, unpack a compressed archive file, or create a Web server, all with just one or a few lines of code. And in addition to the standard library, thousands of third-party libraries are available, some providing more powerful and sophisticated facilities than the standard library—for example, the Twisted networking library and the NumPy numeric library—while others provide functionality that is too specialized to be included in the standard library—for example, the SimPy simulation package. Most of the third-party libraries are available from the Python Package Index,pypi.python.org/pypi.

Python can be used to program in procedural, object-oriented, and to a lesser extent, in functional style, although at heart Python is an object-oriented language. This book shows how to write both procedural and object-oriented programs, and also teaches Python¿s functional programming features.

The purpose of this book is to show you how to write Python programs in good idiomatic Python 3 style, and to be a useful reference for the Python 3 language after the initial reading. Although Python 3 is an evolutionary rather than revolutionary advance on Python 2, some older practices are no longer appropriate or necessary in Python 3, and new practices have been introduced to take advantage of Python 3 features. Python 3 is a better language than Python 2—it builds on the many years of experience with Python 2 and adds lots of new features (and omits Python 2’s misfeatures), to make it even more of a pleasure to use than Python 2, as well as more convenient, easier, and more consistent.

The book’s aim is to teach the Python language, and although many of the standard Python libraries are used, not all of them are. This is not a problem, because once you have read the book, you will have enough Python knowledge to be able to make use of any of the standard libraries, or any third-party Python library, and be able to create library modules of your own.

The book is designed to be useful to several different audiences, including self-taught and hobbyist programmers, students, scientists, engineers and others who need to program as part of their work, and of course, computing professionals and computer scientists. To be of use to such a wide range of people without boring the knowledgeable or losing the less-experienced, the book assumes at least some programming experience (in any language). In particular, it assumes a basic knowledge of data types (such as numbers and strings), collection data types (such as sets and lists), control structures (such as if and while statements), and functions. In addition, some examples and exercises assume a basic knowledge of HTML markup, and some of the more specialized chapters at the end assume a basic knowledge of their subject area; for example, the databases chapter assumes a basic knowledge of SQL.

The book is structured in such a way as to make you as productive as possible as quickly as possible. By the end of the first chapter you will be able to write small but useful Python programs. Each successive chapter introduces new topics, and often both broadens and deepens the coverage of topics introduced in earlier chapters. This means that if you read the chapters in sequence, you can stop at any point and you’ll be able to write complete programs with what you have learned up to that point, and then, of course, resume reading to learn more advanced and sophisticated techniques when you are ready. For this reason, some topics are introduced in one chapter, and then are explored further in one or more later chapters.

Two key problems arise when teaching a new programming language. The first is that sometimes when it is necessary to teach one particular concept, that concept depends on another concept, which in turn depends either directly or indirectly on the first. The second is that, at the beginning, the reader may know little or nothing of the language, so it is very difficult to present interesting or useful examples and exercises. In this book, we seek to solve both of these problems, first by assuming some prior programming experience, and second by presenting Python’s “beautiful heart” in Chapter 1—eight key pieces of Python that are sufficient on their own to write decent programs. One consequence of this approach is that in the early chapters some of the examples are a bit artificial in style since they use only what has been taught up to the point where they are presented; this effect diminishes chapter by chapter, until by the end of Chapter 7, all the examples are written in completely natural and idiomatic Python 3 style.

The book’s approach is wholly practical, and you are encouraged to try out the examples and exercises for yourself to get hands-on experience. Wherever possible, small but complete programs are used as examples, to provide realistic use cases. The examples and excercise solutions are available online at www.qtrac.eu/py3book.html—all of them have been tested with Python 3.0 final on Windows and Unix.The Structure of the Book

Chapter 1 presents eight key pieces of Python that are sufficient for writing complete programs. It also describes some of the Python programming environments that are available, and presents two tiny example programs both built using the eight key pieces of Python covered earlier in the chapter.

Chapters 2 through 5 introduce Python’s procedural programming features, including its basic data types and collection data types, and many useful built-in functions and control structures, as well as very simple text file handling.

Chapter 5 shows how to create custom modules and packages and provides an overview of Python’s standard library so that you will have a good idea of the functionality that Python provides out of the box and can avoid reinventing the wheel.

Chapter 6 provides a thorough introduction to object-oriented programming with Python. All of the material on procedural programming that you learned in earlier chapters is still applicable, since object-oriented programming is built on procedural foundations—for example, making use of the same data types, collection data types, and control structures.

Chapter 7 covers writing and reading files. For binary files, the coverage includes compression and random access, and for text files, the coverage includes parsing manually and with regular expressions. This chapter also shows how to write and read XML files, including using element trees, DOM (Document Object Model), and SAX (Simple API for XML).

Chapter 8 revisits material covered in some earlier chapters, exploring many of Python’s more advanced features in the areas of data types and collection data types, control structures, functions, and object-oriented programming. This chapter also introduces many new functions, classes, and advanced techniques, including functional-style programming—the material it covers is both challenging and rewarding.

The remaining chapters cover other advanced topics. Chapter 9 shows techniques for spreading a program’s workload over multiple processes and over multiple threads. Chapter 10 shows how to write client/server applications using Python’s standard networking support. Chapter 11 covers database programming (both simple key-value “DBM” files, and SQL databases). Chapter 12 explains and illustrates Python’s regular expression mini-language and covers the regular expressions module, and Chapter 13 introduces GUI (Graphical User Interface) programming.

Most of the book’s chapters are quite long to keep all the related material together in one place for ease of reference. However, the chapters are broken down into sections, subsections, and sometimes subsubsections, so it is easy to read at a pace that suits you; for example, by reading one section or subsection at a time.

Read More Show Less

Customer Reviews

Be the first to write a review
( 0 )
Rating Distribution

5 Star

(0)

4 Star

(0)

3 Star

(0)

2 Star

(0)

1 Star

(0)

Your Rating:

Your Name: Create a Pen Name or

Barnes & Noble.com Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & Noble.com that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & Noble.com does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at BN.com or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation

Reminder:

  • - By submitting a review, you grant to Barnes & Noble.com and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Noble.com Terms of Use.
  • - Barnes & Noble.com reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & Noble.com also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on BN.com. It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

 
Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously
Sort by: Showing 1 – 8 of 7 Customer Reviews
  • Posted July 11, 2009

    more from this reviewer

    Excellent

    This book does an excellent job of exposing the reader to Python 3. Mark Summerfield shows how to make testing easy with doctest. He also offers suggestions on how to write more maintainable code.

    As stated before, the margins feature cross-references. This is excellent if you want to use the book as a reference, it will point you to pages relevant to the current topic! The book has everything, from binary access files to regex to GUI programming.

    Summerfield does just the right amount of explaining topics for new programmers, and jumps into code discussion and exercises for experienced programmers. He also doesn't clutter the pages with repetitive code, so you are able to follow the topics easily. I definitely recommend this book for anyone moving from Python 2.5+ to Python 3, or anyone who is new to the language.

    Was this review helpful? Yes  No   Report this review
  • Posted May 23, 2009

    great python 3 book

    like most programming books this one covers the language

    what makes this book stand out are the following
    discusses the importance of conventions and idioms
    wants you to write good code, not just code that works
    uses the left margin well for cross references to relevant material
    uses the right margin to note subtle traps in the code
    put try loops in the code from the beginning
    it's ok for documentation to be longer than the code

    so after several decades of trying to be a professional coder
    whats not to like

    Was this review helpful? Yes  No   Report this review
  • Anonymous

    Posted May 2, 2009

    No text was provided for this review.

  • Anonymous

    Posted October 30, 2011

    No text was provided for this review.

  • Anonymous

    Posted February 14, 2009

    No text was provided for this review.

  • Anonymous

    Posted February 15, 2009

    No text was provided for this review.

  • Anonymous

    Posted December 12, 2009

    No text was provided for this review.

  • Anonymous

    Posted February 11, 2009

    No text was provided for this review.

Sort by: Showing 1 – 8 of 7 Customer Reviews

If you find inappropriate content, please report it to Barnes & Noble
Why is this product inappropriate?
Comments (optional)