Programming in Python 3 - SMU

Programming in Python 3

A Complete Introduction to the Python Language

Second Edition

Mark Summer?eld

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

Summer?eld, Mark.

Programming in Python 3 : a complete introduction to the Python language / Mark

Summer?eld.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.133dc22

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 Pro?ling . . . . . . . . . . . . . . . . . . . 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 . . .

Creating and Running Python Programs . . . . . . . . . . . . . . . . . . . . . . . .

Pythons Beautiful Heart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Piece #1: Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Piece #2: Object References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Piece #3: Collection Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Piece #4: Logical Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Piece #5: Control Flow Statements . . . . . . . . . . . . . . . . . . . . . . . . . .

Piece #6: Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Piece #7: Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Piece #8: Creating and Calling Functions . . . . . . . . . . . . . . . . . . . .

Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

bigdigits.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

generate_grid.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9

9

14

14

16

18

21

26

30

33

36

39

39

42

44

47

Chapter 2. Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Identi?ers and Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Integral Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Floating-Point Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Floating-Point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Decimal Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Comparing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Slicing and Striding Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

String Operators and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

51

51

54

54

58

58

59

62

63

65

68

69

71

ix

String Formatting with the str.format() Method . . . . . . . . . . . . . .

Character Encodings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

quadratic.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

csv2html.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

78

91

94

94

97

102

104

Chapter 3. Collection Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Sequence Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Named Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Set Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Frozen Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Mapping Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Default Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Ordered Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Iterating and Copying Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Iterators and Iterable Operations and Functions . . . . . . . . . . . . .

Copying Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

generate_usernames.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

statistics.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

107

107

108

111

113

120

121

125

126

126

135

136

138

138

146

148

149

152

156

158

Chapter 4. Control Structures and Functions . . . . . . . . . . . . . . . . . . .

Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Conditional Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Catching and Raising Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . .

Custom Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Custom Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Names and Docstrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Argument and Parameter Unpacking . . . . . . . . . . . . . . . . . . . . . . .

159

159

159

161

163

163

168

171

176

177

x

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download