Introduction to Computation and Programming Using Python Introduction to Computation and Programming Using Python Revised and Expanded Edition John V. Guttag The MIT Press Cambridge, Massachusetts London, England © 2013 Massachusetts Institute of Technology All rights reserved. No part of this book may be reproduced in any form by any electronic or mechanical means (including photocopying, recording, or information storage and retrieval) without permission in writing from the publisher. MIT Press books may be purchased at special quantity discounts for business or sales promotional use. For information, please email [email protected] or write to Special Sales Department, The MIT Press, 55 Hayward Street, Cambridge, MA 02142. Printed and bound in the United States of America. Library of Congress Cataloging-‐in-‐Publication Data Guttag, John. Introduction to computation and programming using Python / John V. Guttag. — Revised and expanded edition. pages cm Includes index. ISBN 978-‐0-‐262-‐52500-‐8 (pbk. : alk. paper) 1. Python (Computer program language) 2. Computer programming. I. Title. QA76.73.P48G88 2013 005.13'3—dc23 10 9 8 7 6 5 4 3 2 1 To my family: Olga David Andrea Michael Mark Addie CONTENTS PREFACE ....................................................................................................... xiii ACKNOWLEDGMENTS ..................................................................................... xv 1 GETTING STARTED .................................................................................... 1 2 INTRODUCTION TO PYTHON ...................................................................... 7 2.1 The Basic Elements of Python ............................................................... 8 2.1.1 Objects, Expressions, and Numerical Types .................................... 9 2.1.2 Variables and Assignment ............................................................ 11 2.1.3 IDLE ............................................................................................ 13 2.2 Branching Programs ........................................................................... 14 2.3 Strings and Input ............................................................................... 16 2.3.1 Input ............................................................................................ 18 2.4 Iteration .............................................................................................. 18 3 SOME SIMPLE NUMERICAL PROGRAMS .................................................. 21 3.1 Exhaustive Enumeration .................................................................... 21 3.2 For Loops ............................................................................................ 23 3.3 Approximate Solutions and Bisection Search ...................................... 25 3.4 A Few Words About Using Floats ........................................................ 29 3.5 Newton-Raphson ................................................................................ 32 4 FUNCTIONS, SCOPING, and ABSTRACTION ............................................. 34 4.1 Functions and Scoping ....................................................................... 35 4.1.1 Function Definitions ..................................................................... 35 4.1.2 Keyword Arguments and Default Values ....................................... 36 4.1.3 Scoping ........................................................................................ 37 4.2 Specifications ..................................................................................... 41 4.3 Recursion ........................................................................................... 44 4.3.1 Fibonacci Numbers ...................................................................... 45 4.3.2 Palindromes ................................................................................. 48 4.4 Global Variables ................................................................................. 50 4.5 Modules .............................................................................................. 51 4.6 Files ................................................................................................... 53 viii (cid:3) (cid:3) 5 STRUCTURED TYPES, MUTABILITY, AND HIGHER-ORDER FUNCTIONS .. 56 (cid:3) (cid:3) 5.1 Tuples ................................................................................................ 56 (cid:3) (cid:3) 5.1.1 Sequences and Multiple Assignment ............................................. 57 (cid:3) (cid:3) 5.2 Lists and Mutability ............................................................................ 58 (cid:3) (cid:3) 5.2.1 Cloning ........................................................................................ 63 (cid:3) (cid:3) 5.2.2 List Comprehension ..................................................................... 63 (cid:3) (cid:3) 5.3 Functions as Objects .......................................................................... 64 (cid:3) (cid:3) 5.4 Strings, Tuples, and Lists ................................................................... 66 (cid:3) (cid:3) 5.5 Dictionaries ........................................................................................ 67 (cid:3) (cid:3) 6 TESTING AND DEBUGGING. ..................................................................... 70 (cid:3) (cid:3) 6.1 Testing ................................................................................................ 70 (cid:3) (cid:3) 6.1.1 Black-Box Testing ........................................................................ 71 (cid:3) (cid:3) 6.1.2 Glass-Box Testing ........................................................................ 73 (cid:3) (cid:3) 6.1.3 Conducting Tests ......................................................................... 74 (cid:3) (cid:3) 6.2 Debugging .......................................................................................... 76 (cid:3) (cid:3) 6.2.1 Learning to Debug ........................................................................ 78 (cid:3) (cid:3) 6.2.2 Designing the Experiment ............................................................ 79 (cid:3) (cid:3) 6.2.3 When the Going Gets Tough ......................................................... 81 (cid:3) (cid:3) 6.2.4 And When You Have Found “The” Bug .......................................... 82 (cid:3) (cid:3) 7 EXCEPTIONS AND ASSERTIONS .............................................................. 84 (cid:3) (cid:3) 7.1 Handling Exceptions ........................................................................... 84 (cid:3) (cid:3) 7.2 Exceptions as a Control Flow Mechanism ........................................... 87 (cid:3) (cid:3) 7.3 Assertions ........................................................................................... 90 (cid:3) (cid:3) 8 CLASSES AND OBJECT-ORIENTED PROGRAMMING ............................... 91 (cid:3) (cid:3) 8.1 Abstract Data Types and Classes ........................................................ 91 (cid:3) (cid:3) 8.1.1 Designing Programs Using Abstract Data Types ............................ 96 (cid:3) (cid:3) 8.1.2 Using Classes to Keep Track of Students and Faculty ................... 96 (cid:3) (cid:3) 8.2 Inheritance ......................................................................................... 99 (cid:3) (cid:3) 8.2.1 Multiple Levels of Inheritance ..................................................... 101 (cid:3) (cid:3) 8.2.2 The Substitution Principle .......................................................... 102 (cid:3) (cid:3) 8.3 Encapsulation and Information Hiding .............................................. 103 (cid:3) (cid:3) 8.3.1 Generators ................................................................................. 106 (cid:3) (cid:3) 8.4 Mortgages, an Extended Example ..................................................... 108 ix 9 A SIMPLISTIC INTRODUCTION TO ALGORITHMIC COMPLEXITY ............ 113 9.1 Thinking About Computational Complexity ....................................... 113 9.2 Asymptotic Notation .......................................................................... 116 9.3 Some Important Complexity Classes ................................................. 118 9.3.1 Constant Complexity .................................................................. 118 9.3.2 Logarithmic Complexity .............................................................. 118 9.3.3 Linear Complexity ...................................................................... 119 9.3.4 Log-Linear Complexity ................................................................ 120 9.3.5 Polynomial Complexity ............................................................... 120 9.3.6 Exponential Complexity .............................................................. 121 9.3.7 Comparisons of Complexity Classes ............................................ 123 10 SOME SIMPLE ALGORITHMS AND DATA STRUCTURES ......................... 125 10.1 Search Algorithms .......................................................................... 126 10.1.1 Linear Search and Using Indirection to Access Elements .......... 126 10.1.2 Binary Search and Exploiting Assumptions .............................. 128 10.2 Sorting Algorithms .......................................................................... 131 10.2.1 Merge Sort ................................................................................ 132 10.2.2 Exploiting Functions as Parameters .......................................... 135 10.2.3 Sorting in Python ..................................................................... 136 10.3 Hash Tables .................................................................................... 137 11 PLOTTING AND MORE ABOUT CLASSES ................................................ 141 11.1 Plotting Using PyLab ....................................................................... 141 11.2 Plotting Mortgages, an Extended Example ....................................... 146 12 STOCHASTIC PROGRAMS, PROBABILITY, AND STATISTICS ................... 152 12.1 Stochastic Programs ....................................................................... 153 12.2 Inferential Statistics and Simulation ............................................... 155 12.3 Distributions .................................................................................. 166 12.3.1 Normal Distributions and Confidence Levels ............................. 168 12.3.2 Uniform Distributions .............................................................. 170 12.3.3 Exponential and Geometric Distributions ................................. 171 12.3.4 Benford’s Distribution .............................................................. 173 12.4 How Often Does the Better Team Win? ............................................ 174 12.5 Hashing and Collisions ................................................................... 177