logo资料库

Introduction to Programming in Python An Interdisciplinary Approach.pdf

第1页 / 共1239页
第2页 / 共1239页
第3页 / 共1239页
第4页 / 共1239页
第5页 / 共1239页
第6页 / 共1239页
第7页 / 共1239页
第8页 / 共1239页
资料共1239页,剩余部分请下载后查看
About This eBook
Title Page
Copyright Page
Dedication Page
Contents
Programs
Preface
Coverage
Use in the Curriculum
Prerequisites
Goals
Booksite
Acknowledgments
Chapter One. Elements of Programming
1.1 Your First Program
Programming in Python
Input and output
Q&A
Exercises
1.2 Built-in Types of Data
Definitions
Strings
Integers
Floating-point numbers
Booleans
Comparisons
Functions and APIs
Type conversion
Summary
Q&A (strings)
Q&A (integers)
Q&A (floating-point numbers)
Q&A
Exercises
Creative Exercises
1.3 Conditionals and Loops
If statements
Else clauses
While statements
For statements
Nesting
Applications
Loop and a half
Infinite loops
Summary
Q&A
Exercises
Creative Exercises
1.4 Arrays
Arrays in Python
Array aliases and copies
System support for arrays
Sample applications of arrays
Coupon collector
Sieve of Eratosthenes
Two-dimensional arrays
Example: self-avoiding random walks
Summary
Q&A
Exercises
Creative Exercises
1.5 Input and Output
Bird’s-eye view
Standard output
Standard input
Redirection and piping
Standard drawing
Animation
Standard audio
Summary
Q&A
Exercises
Creative Exercises
1.6 Case Study: Random Web Surfer
Input format
Transition matrix
Simulation
Mixing a Markov chain
Lessons
Exercises
Creative Exercises
Chapter Two. Functions and Modules
2.1 Defining Functions
Using and defining functions
Implementing mathematical functions
Using functions to organize code
Passing arguments and returning values
Example: superposition of sound waves
Q&A
Exercises
Creative Exercises
2.2 Modules and Clients
Using functions in other programs
Modular programming abstractions
Random numbers
Array-processing API
Iterated function systems
Standard statistics
Modular programming
Q&A
Exercises
Creative Exercises
2.3 Recursion
Your first recursive program
Mathematical induction
Euclid’s algorithm
Towers of Hanoi
Function-call trees
Exponential time
Gray codes
Recursive graphics
Brownian bridge
Pitfalls of recursion
Perspective
Q&A
Exercises
Creative Exercises
2.4 Case Study: Percolation
Percolation
Basic scaffolding
Vertical percolation
Testing
Estimating probabilities
Recursive solution for percolation
Adaptive plot
Lessons
Q&A
Exercises
Creative Exercises
Chapter Three. Object-Oriented Programming
3.1 Using Data Types
Methods
String processing
String-processing application: genomics
A user-defined data type
Color
Digital image processing
Input and output revisited
Memory management
Q&A
Exercises
Creative Exercises
3.2 Creating Data Types
Basic elements of a data type
Stopwatch
Histogram
Turtle graphics
Complex numbers
Mandelbrot set
Commercial data processing
Q&A
Exercises
Creative Exercises
3.3 Designing Data Types
Designing APIs
Encapsulation
Immutability
Example: spatial vectors
Tuples
Polymorphism
Overloading
Functions are objects
Inheritance
Application: data mining
Design-by-contract
Q&A
Exercises
Data-Type Design Exercises
Creative Exercises
3.4 Case Study: N-Body Simulation
N-body simulation
Q&A
Exercises
Creative Exercises
Chapter Four. Algorithms and Data Structures
4.1 Performance
Scientific method
Observations
Hypotheses
Order of growth classifications
Predictions
Caveats
Performance guarantees
Python lists and arrays
Strings
Memory
Perspective
Q&A
Exercises
Creative Exercises
4.2 Sorting and Searching
Binary search
Insertion sort
Mergesort
Python system sort
Application: frequency counts
Lessons
Q&A
Exercises
Creative Exercises
4.3 Stacks and Queues
Pushdown stacks
Python list (resizing array) implementation of a stack
Linked-list implementation of a stack
Stack applications
FIFO queues
Queue applications
Resource allocation
Q&A
Exercises
Linked-List Exercises
Creative Exercises
4.4 Symbol Tables
API
Symbol table clients
Elementary symbol-table implementations
Hash tables
Binary search trees
Performance characteristics of BSTs
Traversing a BST
Iterables
Ordered symbol table operations
Dictionary data type
Set data type
Perspective
Q&A
Exercises
Binary Tree Exercises
Creative Exercises
4.5 Case Study: Small-World Phenomenon
Graphs
Graph data type
Graph client example
Shortest paths in graphs
Small-world graphs
Lessons
Q&A
Exercises
Creative Exercises
Context
Standard Python modules
Programming environments
Scientific computing
Computer systems
Theoretical computer science
Glossary
Index
APIs
Code Snippets
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
分享到:
收藏