Programming in Python 3 - SMU
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 corpsales@
For sales outside the United States, please contact:
International Sales international@
Visit us on the Web: 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
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
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- strings ncert
- the software development process python programming an
- strings lists sets dictionaries and files 4 1 strings
- introduction to the burrows wheeler transform and fm index
- lecture 8 motifs and motifs finding
- python 3 beginner s reference cheat sheet http www
- programming in python 3 smu
- ppyytthhoonn ssttrriinnggss tutorialspoint
- 6 5 2019 william mary
- strings and pattern matching purdue university
Related searches
- python 3 built in functions
- python 3 find element in list
- python 3 7 3 shell download
- install pip in python 3 8
- install python 3 in centos
- python 3 find string in string
- objects in python 3 example
- upgrade python 3 6 to 3 8
- python programming in visual studio
- python 3 built in methods
- python 3 built in function
- upgrade python 3 7 to 3 8 windows