Programming in Python 3

Programming in Python 3

Programming in Python 3 A Complete Introduction to the Python Language Second Edition Mark Summerfield Upper Saddle River, NJ · Boston · Indianapolis · San Francisco New York · Toronto · Montreal · London · Munich · Paris · Madrid Capetown · Sydney · Tokyo · Singapore · Mexico City Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 [email protected] For sales outside the United States, please contact: International Sales [email protected] Visit us on the Web: informit.com/aw Library of Congress Cataloging-in-Publication Data Summerfield, Mark. Programming in Python 3 : a complete introduction to the Python language / Mark Summerfield.—2nd ed. p. cm. Includes bibliographical references and index. ISBN 978-0-321-68056-3 (pbk. : alk. paper) 1. Python (Computer program language) 2. Object-oriented programming (Computer science) I. Title. QA76.73.P98S86 2010 005.13’3—dc22 2009035430 Copyright © 2010 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, write to: Pearson Education, Inc. Rights and Contracts Department 501 Boylston Street, Suite 900 Boston, MA 02116 Fax: (617) 671-3447 ISBN-13: 978-0-321-68056-3 ISBN-10: 0-321-68056-1 Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana. First printing, November 2009 Contents at a Glance List of Tables ...................................................... xv Introduction ...................................................... 1 Chapter 1. Rapid Introduction to Procedural Programming ... 9 Chapter 2. Data Types ............................................ 51 Chapter 3. Collection Data Types ................................ 107 Chapter 4. Control Structures and Functions ................... 159 Chapter 5. Modules ...............................................195 Chapter 6. Object-Oriented Programming ...................... 233 Chapter 7. File Handling ......................................... 287 Chapter 8. Advanced Programming Techniques ................ 339 Chapter 9. Debugging, Testing, and Profiling ................... 413 Chapter 10. Processes and Threading ........................... 439 Chapter 11. Networking ..........................................457 Chapter 12. Database Programming ............................. 475 Chapter 13. Regular Expressions ................................ 489 Chapter 14. Introduction to Parsing ............................. 513 Chapter 15. Introduction to GUI Programming ................. 569 Epilogue ...........................................................595 Selected Bibliography ............................................ 597 Index .............................................................. 599 www.qtrac.eu/py3book.html 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 Piece #1: Data Types ....................................... 14 Piece #2: Object References ................................. 16 Piece #3: Collection Data Types ............................. 18 Piece #4: Logical Operations ................................ 21 Piece #5: Control Flow Statements .......................... 26 Piece #6: Arithmetic Operators .............................. 30 Piece #7: Input/Output ..................................... 33 Piece #8: Creating and Calling Functions .................... 36 Examples ...................................................... 39 bigdigits.py ................................................ 39 generate_grid.py ........................................... 42 Summary ...................................................... 44 Exercises ...................................................... 47 Chapter 2. Data Types ........................................... 51 Identifiers and Keywords ....................................... 51 Integral Types .................................................. 54 Integers ................................................... 54 Booleans ................................................... 58 Floating-Point Types ........................................... 58 Floating-Point Numbers .................................... 59 Complex Numbers ......................................... 62 Decimal Numbers .......................................... 63 Strings ......................................................... 65 Comparing Strings ......................................... 68 Slicing and Striding Strings ................................ 69 String Operators and Methods .............................. 71 ix String Formatting with the str.format() Method .............. 78 Character Encodings ....................................... 91 Examples ...................................................... 94 quadratic.py ............................................... 94 csv2html.py ................................................ 97 Summary ...................................................... 102 Exercises ...................................................... 104 Chapter 3. Collection Data Types ................................ 107 Sequence Types ................................................ 107 Tuples ..................................................... 108 Named Tuples ............................................. 111 Lists ....................................................... 113 Set Types ...................................................... 120 Sets ....................................................... 121 Frozen Sets ................................................ 125 Mapping Types ................................................. 126 Dictionaries ................................................ 126 Default Dictionaries ........................................ 135 Ordered Dictionaries ....................................... 136 Iterating and Copying Collections ............................... 138 Iterators and Iterable Operations and Functions ............. 138 Copying Collections ........................................ 146 Examples ...................................................... 148 generate_usernames.py .................................... 149 statistics.py ................................................ 152 Summary ...................................................... 156 Exercises ...................................................... 158 Chapter 4. Control Structures and Functions ................... 159 Control Structures .............................................. 159 Conditional Branching ..................................... 159 Looping .................................................... 161 Exception Handling ............................................ 163 Catching and Raising Exceptions ........................... 163 Custom Exceptions ......................................... 168 Custom Functions .............................................. 171 Names and Docstrings ...................................... 176 Argument and Parameter Unpacking ....................... 177 x Accessing Variables in the Global Scope ..................... 180 Lambda Functions ......................................... 182 Assertions ................................................. 183 Example: make_html_skeleton.py ............................... 185 Summary ...................................................... 191 Exercise ....................................................... 192 Chapter 5. Modules .............................................. 195 Modules and Packages .......................................... 195 Packages .................................................. 199 Custom Modules ........................................... 202 Overview of Python’s Standard Library .......................... 212 String Handling ........................................... 213 Command-Line Programming .............................. 214 Mathematics and Numbers ................................. 216 Times and Dates ........................................... 216 Algorithms and Collection Data Types ....................... 217 File Formats, Encodings, and Data Persistence ..............

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    636 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us