About This eBook
ePUB is an open, industry-standard format for eBooks. However, support of ePUB and its
many features varies across reading devices and applications. Use your device or app settings to
customize the presentation to your liking. Settings that you can customize often include font, font
size, single or double column, landscape or portrait mode, and figures that you can click or tap to
enlarge. For additional information about the settings and features on your reading device or app,
visit the device manufacturer’s Web site.
Many titles include programming code or configuration examples. To optimize the presentation of
these elements, view the eBook in single-column, landscape mode and adjust the font size to the
smallest setting. In addition to presenting code and configurations in the reflowable text format, we
have included images of the code that mimic the presentation found in the print book; therefore,
where the reflowable format may compromise the presentation of the code listing, you will see a
“Click here to view code image” link. Click the link to view the print-fidelity code image. To return
to the previous page viewed, click the Back button on your device or app.
2
Introduction
to
Programming in Python
An Interdisciplinary Approach
Robert Sedgewick
Kevin Wayne
Robert Dondero
Princeton University
New York • Boston • Indianapolis • San Francisco
Toronto • Montreal • London • Munich • Paris • Madrid
Capetown • Sydney • Tokyo • Singapore • Mexico City
3
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 authors 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.
For information about buying this title in bulk quantities, or for special sales opportunities (which
may include electronic versions; custom cover designs; and content particular to your business,
training goals, marketing focus, or branding interests), please contact our corporate sales
department at corpsales@pearsoned.com or (800) 382-3419.
For government sales inquiries, please contact governmentsales@pearsoned.com.
For questions about sales outside the United States, please contact
international@pearsoned.com.
Visit us on the Web: informit.com/aw
Library of Cataloging-in-Publication Data
Sedgewick, Robert, 1946-
Introduction to programming in Python : an interdisciplinary approach / Robert Sedgewick, Kevin
Wayne, Robert Dondero.
pages cm
Includes indexes.
ISBN 978-0-13-407643-0 (hardcover : alk. paper)—ISBN 0-13-407643-5
1. Python (Computer program language) 2. Computer programming. I. Wayne, Kevin Daniel,
1971-
II. Dondero, Robert. III. Title.
QA76.73.P98S43 2015
005.13’3—dc23
2015011936
Copyright © 2015 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. To obtain permission to use material from this work, please
submit a written request to Pearson Education, Inc., Permissions Department, 200 Old Tappan
Road, Old Tappan, New Jersey 07675, or you may fax your request to 236-3290.
ISBN-13: 978-0-13-407643-0
ISBN-10: 0-13-407643-5
Text printed in the United States on recycled paper at Edwards Brothers Malloy in Ann Arbor,
Michigan.
4
First printing, June 2015
5
To Adam, Andrew, Brett, Robbie,
Henry, Iona, Rose, Peter,
and especially Linda
To Jackie and Alex
To my family,
especially Ellen and Meghan
6
Contents
Preface
1—Elements of Programming
1.1 Your First Program
1.2 Built-in Types of Data
1.3 Conditionals and Loops
1.4 Arrays
1.5 Input and Output
1.6 Case Study: Random Web Surfer
2—Functions and Modules
2.1 Defining Functions
2.2 Modules and Clients
2.3 Recursion
2.4 Case Study: Percolation
3—Object-Oriented Programming
3.1 Using Data Types
3.2 Creating Data Types
3.3 Designing Data Types
3.4 Case Study: N-Body Simulation
4—Algorithms and Data Structures
4.1 Performance
4.2 Sorting and Searching
4.3 Stacks and Queues
4.4 Symbol Tables
4.5 Case Study: Small-World Phenomenon
Context
Glossary
Index
7
Programs
Elements of Programming
Your First Program
1.1.1 Hello, World
1.1.2 Using a command-line argument
Built-in Types of Data
1.2.1 String concatenation example
1.2.2 Integer operators
1.2.3 Float operators
1.2.4 Quadratic formula
1.2.5 Leap year
Conditionals and Loops
1.3.1 Flipping a fair coin
1.3.2 Your first loop
1.3.3 Computing powers of 2
1.3.4 Your first nested loops
1.3.5 Harmonic numbers
1.3.6 Newton’s method
1.3.7 Converting to binary
1.3.8 Gambler’s ruin simulation
1.3.9 Factoring integers
Arrays
1.4.1 Sampling without replacement
1.4.2 Coupon collector simulation
1.4.3 Sieve of Eratosthenes
1.4.4 Self-avoiding random walks
Input and Output
1.5.1 Generating a random sequence
1.5.2 Interactive user input
1.5.3 Averaging a stream of numbers
1.5.4 A simple filter
1.5.5 Standard input to drawing filter
1.5.6 Function graph
1.5.7 Bouncing ball
8