Instant download Introduction to Computation and Programming Using Python 3rd Edition John V. Guttag pdf all chapter
Instant download Introduction to Computation and Programming Using Python 3rd Edition John V. Guttag pdf all chapter
com
https://textbookfull.com/product/introduction-to-
computation-and-programming-using-python-3rd-edition-john-v-
guttag/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/introduction-to-computation-and-
programming-using-python-3rd-edition-john-v-guttag-2/
textboxfull.com
https://textbookfull.com/product/introduction-to-computation-and-
programming-using-python-with-application-to-understanding-data-
guttag/
textboxfull.com
https://textbookfull.com/product/python-programming-an-introduction-
to-computer-science-3rd-edition-john-m-zelle/
textboxfull.com
Practical Programming An Introduction to Computer Science
Using Python 3 6 3rd Edition Paul Gries
https://textbookfull.com/product/practical-programming-an-
introduction-to-computer-science-using-python-3-6-3rd-edition-paul-
gries/
textboxfull.com
https://textbookfull.com/product/python-programming-an-introduction-
to-computer-science-john-m-zelle/
textboxfull.com
https://textbookfull.com/product/introduction-to-scientific-
programming-with-python-joakim-sundnes/
textboxfull.com
https://textbookfull.com/product/bite-size-python-an-introduction-to-
python-programming-1st-edition-april-speight/
textboxfull.com
https://textbookfull.com/product/introduction-to-radar-using-python-
and-matlab-lee-andrew-andy-harrison/
textboxfull.com
Introduction to Computation
and Programming Using
Python
with Application to Computational Modeling
and Understanding Data
Introduction to Computation
and Programming Using
Python
with Application to Computational Modeling
and Understanding Data
third edition
John V. Guttag
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.
This book was set in Minion Pro by New Best-set Typesetters Ltd.
10 9 8 7 6 5 4 3 2 1
d_r0
To my family:
Olga
David
Andrea
Michael
Mark
Addie
Pierce
CONTENTS
PREFACE
ACKNOWLEDGMENTS
1: GETTING STARTED
2: INTRODUCTION TO PYTHON
3: SOME SIMPLE NUMERICAL PROGRAMS
4: FUNCTIONS, SCOPING, AND ABSTRACTION
5: STRUCTURED TYPES AND MUTABILITY
6: RECURSION AND GLOBAL VARIABLES
7: MODULES AND FILES
8: TESTING AND DEBUGGING
9: EXCEPTIONS AND ASSERTIONS
10: CLASSES AND OBJECT-ORIENTED PROGRAMMING
11: A SIMPLISTIC INTRODUCTION TO ALGORITHMIC
COMPLEXITY
12: SOME SIMPLE ALGORITHMS AND DATA STRUCTURES
13: PLOTTING AND MORE ABOUT CLASSES
14: KNAPSACK AND GRAPH OPTIMIZATION PROBLEMS
15: DYNAMIC PROGRAMMING
16: RANDOM WALKS AND MORE ABOUT DATA
VISUALIZATION
17: STOCHASTIC PROGRAMS, PROBABILITY, AND
DISTRIBUTIONS
18: MONTE CARLO SIMULATION
19: SAMPLING AND CONFIDENCE
20: UNDERSTANDING EXPERIMENTAL DATA
21: RANDOMIZED TRIALS AND HYPOTHESIS CHECKING
22: LIES, DAMNED LIES, AND STATISTICS
23: EXPLORING DATA WITH PANDAS
24: A QUICK LOOK AT MACHINE LEARNING
25: CLUSTERING
26: CLASSIFICATION METHODS
PYTHON 3.8 QUICK REFERENCE
INDEX
List of figures
Chapter 1
Figure 1-1 Flowchart of getting dinner
Chapter 2
Figure 2-1 Anaconda startup window
Figure 2-2 Spyder window
Figure 2-3 Operators on types int and float
Figure 2-4 Binding of variables to objects
Figure 2-5 Flowchart for conditional statement
Figure 2-6 Flowchart for iteration
Figure 2-7 Squaring an integer, the hard way
Figure 2-8 Hand simulation of a small program
Figure 2-9 Using a for statement
Chapter 3
Figure 3-1 Using exhaustive enumeration to find the cube root
Figure 3-2 Using exhaustive enumeration to test primality
Figure 3-3 A more efficient primality test
Figure 3-4 Approximating the square root using exhaustive
enumeration
Figure 3-5 Using bisection search to approximate square root
Figure 3-6 Using bisection search to estimate log base 2
Figure 3-7 Implementation of Newton–Raphson method
Chapter 4
Figure 4-1 Using bisection search to approximate square root
of x
Figure 4-2 Summing a square root and a cube root
Figure 4-3 A function for finding roots
Figure 4-4 Code to test find_root
Figure 4-5 Nested scopes
Figure 4-6 Stack frames
Figure 4-7 A function definition with a specification
Figure 4-8 Splitting find_root into multiple functions
Figure 4-9 Generalizing bisection_solve
Figure 4-10 Using bisection_solve to approximate logs
Chapter 5
Figure 5-1 Two lists
Figure 5-2 Two lists that appear to have the same value, but
don't
Figure 5-3 Demonstration of mutability
Figure 5-4 Common methods associated with lists
Figure 5-5 Applying a function to elements of a list
Figure 5-6 Common operations on sequence types
Figure 5-7 Comparison of sequence types
Figure 5-8 Some methods on strings
Figure 5-9 Translating text (badly)
Figure 5-10 Some common operations on dicts
Chapter 6
Figure 6-1 Iterative and recursive implementations of factorial
Figure 6-2 Growth in population of female rabbits
Figure 6-3 Recursive implementation of Fibonacci sequence
Figure 6-4 Palindrome testing
Figure 6-5 Code to visualize palindrome testing
Figure 6-6 Using a global variable
Chapter 7
Figure 7-1 Some code related to circles and spheres
Figure 7-2 Common functions for accessing files
Chapter 8
Figure 8-1 Testing boundary conditions
Figure 8-2 Not the first bug
Figure 8-3 Program with bugs
Chapter 9
Figure 9-1 Using exceptions for control flow
Figure 9-2 Control flow without a try-except
Figure 9-3 Get grades
Chapter 10
Figure 10-1 Class Int_set
Figure 10-2 Using magic methods
Figure 10-3 Class Person
Figure 10-4 Class MIT_person
Figure 10-5 Two kinds of students
Figure 10-6 Class Grades
Figure 10-7 Generating a grade report
Figure 10-8 Information hiding in classes
Figure 10-9 New version of get_students
Figure 10-10 Mortgage base class
Figure 10-11 Mortgage subclasses
Chapter 11
Figure 11-1 Using exhaustive enumeration to approximate
square root
Figure 11-2 Using bisection search to approximate square root
Figure 11-3 Asymptotic complexity
Figure 11-4 Implementation of subset test
Figure 11-5 Implementation of list intersection
Figure 11-6 Generating the power set
Figure 11-7 Constant, logarithmic, and linear growth
Figure 11-8 Linear, log-linear, and quadratic growth
Figure 11-9 Quadratic and exponential growth
Chapter 12
Figure 12-1 Implementing lists
Figure 12-2 Linear search of a sorted list
Figure 12-3 Recursive binary search
Figure 12-4 Selection sort
Figure 12-5 Merge sort
Figure 12-6 Sorting a list of names
Figure 12-7 Implementing dictionaries using hashing
Chapter 13
Figure 13-1 A simple plot
Figure 13-2 Contents of Figure-Jane.png (left) and Figure-
Addie.png (right)
Figure 13-3 Produce plots showing compound growth
Figure 13-4 Plots showing compound growth
Figure 13-5 Another plot of compound growth
Figure 13-6 Strange-looking plot
Figure 13-7 Class Mortgage with plotting methods
Figure 13-8 Subclasses of Mortgage
Figure 13-9 Compare mortgages
Figure 13-10 Generate mortgage plots
Figure 13-11 Monthly payments of different kinds of mortgages
Figure 13-12 Cost over time of different kinds of mortgages
Figure 13-13 Balance remaining and net cost for different kinds
of mortgages
Figure 13-14 Simulation of spread of an infectious disease
Figure 13-15 Function to plot history of infection
Figure 13-16 Produce plot with a single set of parameters
Figure 13-17 Static plot of number of infections
Figure 13-18 Interactive plot with initial slider values
Figure 13-19 Interactive plot with changed slider values
Chapter 14
Figure 14-1 Table of items
Figure 14-2 Class Item
Figure 14-3 Implementation of a greedy algorithm
Figure 14-4 Using a greedy algorithm to choose items
Figure 14-5 Brute-force optimal solution to the 0/1 knapsack
problem
Figure 14-6 The bridges of Königsberg (left) and Euler's
simplified map (right)
Figure 14-7 Nodes and edges
Figure 14-8 Classes Graph and Digraph
Figure 14-9 Depth-first-search shortest-path algorithm
Figure 14-10 Test depth-first-search code
Figure 14-11 Breadth-first-search shortest path algorithm
Chapter 15
Figure 15-1 Tree of calls for recursive Fibonacci
Figure 15-2 Implementing Fibonacci using a memo
Figure 15-3 Table of items with values and weights
Figure 15-4 Decision tree for knapsack problem
Figure 15-5 Using a decision tree to solve a knapsack problem
Figure 15-6 Testing the decision tree-based implementation
Figure 15-7 Dynamic programming solution to knapsack
problem
Figure 15-8 Performance of dynamic programming solution
Chapter 16
Figure 16-1 An unusual farmer
Figure 16-2 Location and Field classes
Figure 16-3 Classes defining Drunks
Figure 16-4 The drunkard's walk (with a bug)
Figure 16-5 Distance from starting point versus steps taken
Figure 16-6 Subclasses of Drunk base class
Figure 16-7 Iterating over styles
Figure 16-8 Plotting the walks of different drunks
Figure 16-9 Mean distance for different kinds of drunks
Figure 16-10 Plotting final locations
Figure 16-11 Where the drunk stops
Figure 16-12 Tracing walks
Figure 16-13 Trajectory of walks
Figure 16-14 Fields with strange properties
Figure 16-15 A strange walk
Chapter 17
Figure 17-1 Roll die
Figure 17-2 Flipping a coin
Figure 17-3 Regression to the mean
Figure 17-4 Illustration of regression to mean
Figure 17-5 Plotting the results of coin flips
Figure 17-6 The law of large numbers at work
Figure 17-7 The law of large numbers at work
Figure 17-8 Variance and standard deviation
Figure 17-9 Helper function for coin-flipping simulation
Figure 17-10 Coin-flipping simulation
Figure 17-11 Convergence of heads/tails ratios
Figure 17-12 Absolute differences
Figure 17-13 Mean and standard deviation of heads - tails
Figure 17-14 Coefficient of variation
Figure 17-15 Final version of flip_plot
Figure 17-16 Coefficient of variation of heads/tails and
abs(heads – tails)
Figure 17-17 A large number of trials
Figure 17-18 Income distribution in Australia
Figure 17-19 Code and the histogram it generates
Figure 17-20 Plot histograms of coin flips
Figure 17-21 Histograms of coin flips
Figure 17-22 PDF for random.random
Figure 17-23 PDF for Gaussian distribution
Figure 17-24 A normal distribution
Figure 17-25 Plot of absolute value of x
Figure 17-26 Checking the empirical rule
Figure 17-27 Produce plot with error bars
Figure 17-28 Estimates with error bars
Figure 17-29 Exponential clearance of molecules
Figure 17-30 Exponential decay
Figure 17-31 Plotting exponential decay with a logarithmic axis
Figure 17-33 A geometric distribution
Figure 17-32 Producing a geometric distribution
Figure 17-34 Simulating a hash table
Figure 17-35 World Series simulation
Figure 17-36 Probability of winning a 7-game series
Chapter 18
Figure 18-1 Checking Pascal's analysis
Figure 18-2 Craps_game class
Figure 18-3 Simulating a craps game
Figure 18-4 Using table lookup to improve performance
Figure 18-5 Unit circle inscribed in a square
Figure 18-6 Estimating π
Chapter 19
Figure 19-1 The first few lines in bm_results2012.csv
Figure 19-2 Read data and produce plot of Boston Marathon
Figure 19-3 Boston Marathon finishing times
Figure 19-4 Sampling finishing times
Figure 19-5 Analyzing a small sample
Figure 19-6 Effect of variance on estimate of mean
Figure 19-7 Compute and plot sample means
Figure 19-8 Sample means
Figure 19-9 Estimating the mean of a continuous die
Figure 19-10 An illustration of the CLT
Figure 19-11 Produce plot with error bars
Figure 19-12 Estimates of finishing times with error bars
Figure 19-13 Standard error of the mean
Figure 19-14 Sample standard deviation vs. population
standard deviation
Figure 19-15 Sample standard deviations
Figure 19-16 Estimating the population mean 10,000 times
Chapter 20
Figure 20-1 A classic experiment
Figure 20-2 Extracting the data from a file
Figure 20-3 Plotting the data
Figure 20-4 Displacement of spring
Figure 20-5 Fitting a curve to data
Figure 20-6 Measured points and linear model
Figure 20-7 Linear and cubic fits
Figure 20-8 Using the model to make a prediction
Figure 20-9 A model up to the elastic limit
Figure 20-10 Data from projectile experiment
Figure 20-11 Plotting the trajectory of a projectile
Figure 20-12 Plot of trajectory
Figure 20-13 Computing R2
Figure 20-14 Computing the horizontal speed of a projectile
Figure 20-15 Fitting a polynomial curve to an exponential
distribution
Figure 20-16 Fitting an exponential
Figure 20-17 An exponential on a semilog plot
Figure 20-18 Using polyfit to fit an exponential
Figure 20-19 A fit for an exponential function
Chapter 21
Figure 21-1 Finishing times for cyclists
Figure 21-2 January 2020 temperature difference from the
1981-2010 average145
Figure 21-3 Plotting a t-distribution
Figure 21-4 Visualizing the t-statistic
Figure 21-5 Compute and print t-statistic and p-value
Figure 21-6 Code for generating racing examples
Figure 21-7 Probability of p-values
Figure 21-8 Lyndsay's simulation of games
Figure 21-9 Correct simulation of games
Figure 21-10 Impact of sample size on p-value
Figure 21-11 Comparing mean finishing times for selected
countries
Figure 21-12 Checking multiple hypotheses
Figure 21-13 Has the sun exploded?
Chapter 22
Figure 22-1 Housing prices in the U.S. Midwest
Figure 22-2 Plotting housing prices
Figure 22-3 A different view of housing prices
Figure 22-4 Housing prices relative to $200,000
Figure 22-5 Comparing number of Instagram followers
Figure 22-6 Do Mexican lemons save lives?
Figure 22-7 Statistics for Anscombe's quartet
Figure 22-8 Data for Anscombe's quartet
Figure 22-9 Welfare vs. full-time jobs
Figure 22-10 Sea ice in the Arctic
Figure 22-11 Growth of Internet usage in U.S.
Figure 22-12 Professor puzzles over students' chalk-throwing
accuracy
Figure 22-13 Probability of 48 anorexics being born in June
Figure 22-14 Probability of 48 anorexics being born in some
month
Chapter 23
Figure 23-1 A sample Pandas DataFrame bound to the variable
wwc
Figure 23-2 An example CSV file
Figure 23-3 Building a dictionary mapping years to
temperature data
Figure 23-4 Building a DataFrame organized around years
Figure 23-5 Produce plots relating year to temperature
measurements
Figure 23-6 Mean and minimum annual temperatures
Figure 23-7 Rolling average minimum temperatures
Figure 23-8 Average temperatures for select cities
Figure 23-9 Variation in temperature extremes
Figure 23-10 Global consumption of fossil fuels
Chapter 24
Figure 24-1 Two sets of names
Figure 24-2 Associating a feature vector with each name
Figure 24-3 Feature vector/label pairs for presidents
Figure 24-4 Name, features, and labels for assorted animals
Figure 24-5 Visualizing distance metrics
Figure 24-6 Minkowski distance
Figure 24-7 Class Animal
Figure 24-8 Build table of distances between pairs of animals
Figure 24-9 Distances between three animals
Figure 24-10 Distances between four animals
Figure 24-11 Distances using a different feature representation
Chapter 25
Figure 25-1 Height, weight, and shirt color
Figure 25-2 Class Example
Figure 25-3 Class Cluster
Figure 25-4 K-means clustering
Figure 25-5 Finding the best k-means clustering
Figure 25-6 A test of k-means
Figure 25-7 Examples from two distributions
Figure 25-8 Lines printed by a call to contrived_test(1, 2, True)
Figure 25-9 Generating points from three distributions
Figure 25-10 Points from three overlapping Gaussians
Figure 25-11 Mammal dentition in dentalFormulas.csv
Figure 25-12 Read and process CSV file
Figure 25-13 Scaling attributes
Figure 25-14 Start of CSV file classifying mammals by diet
Figure 25-15 Relating clustering to labels
Chapter 26
Figure 26-1 Plots of voter preferences
Figure 26-2 Confusion matrices
Figure 26-3 A more complex model
Figure 26-4 Functions for evaluating classifiers
Figure 26-5 First few lines of bm_results2012.csv
Figure 26-6 Build examples and divide data into training and
test sets
Figure 26-7 Finding the k-nearest neighbors
Figure 26-8 Prevalence-based classifier
Figure 26-9 Searching for a good k
Figure 26-10 Choosing a value for k
Figure 26-11 Linear regression models for men and women
Figure 26-12 Produce and plot linear regression models
Figure 26-13 Using linear regression to build a classifier
Figure 26-14 Using sklearn to do multi-class logistic regression
Figure 26-15 Example of two-class logistic regression
Figure 26-16 Use logistic regression to predict gender
Figure 26-17 Construct ROC curve and find AUROC
Figure 26-18 ROC curve and AUROC
Figure 26-19 Class Passenger
Figure 26-20 Read Titanic data and build list of examples207
Figure 26-21 Test models for Titanic survival
Figure 26-22 Print statistics about classifiers
PREFACE
This book is based on courses that have been offered at MIT since
2006, and as “Massive Online Open Courses” (MOOCs) through edX
and MITx since 2012. The first edition of the book was based on a
single one-semester course. However, over time I couldn't resist
adding more material than could be fit into a semester. The current
edition is suitable for either a two-semester or a three-quarter
introductory computer science sequence.
The book is aimed at 1) readers with little or no prior
programming experience who have a desire to understand
computational approaches to problem solving, and 2) more
experienced programmers who want to learn how to use
computation to model things or explore data.
We emphasize breadth rather than depth. The goal is to provide
readers with a brief introduction to many topics, so that they will
have an idea of what's possible when the time comes to think about
how to use computation to accomplish a goal. That said, this is not a
“computation appreciation” book. It is challenging and rigorous.
Readers who wish to really learn the material will have to spend a lot
of time and effort learning to bend the computer to their will.
The main goal of this book is to help readers become skillful at
making productive use of computational techniques. They should
learn to use computational modes of thoughts to frame problems, to
build computational models, and to guide the process of extracting
information from data. The primary knowledge they will take away
from this book is the art of computational problem solving.
We chose not to include problems at the end of chapters. Instead
we inserted “finger exercises” at opportune points within the
chapters. Some are quite short, and are intended to allow readers to
confirm that they understood the material they just read. Some are
more challenging, and are suitable for exam questions. And others
are challenging enough to be useful as homework assignments.
Chapters 1-13 contain the kind of material typically included in
an introductory computer science course, but the presentation is not
conventional. We braid together four strands of material:
and
https://ocw.mit.edu/courses/electrical-engineering-and-
computer-science/6-0002-introduction-to-computational-
thinking-and-data-science-fall-2016/.
The first edition of this book grew out of a set of lecture notes that I
prepared while teaching an undergraduate course at MIT. The
course, and therefore this book, benefited from suggestions from
faculty colleagues (especially Ana Bell, Eric Grimson, Srinivas
Devadas, Fredo Durand, Ron Rivest, and Chris Terman), teaching
assistants, and the students who took the course. David Guttag
overcame his aversion to computer science, and proofread multiple
chapters of the first edition.
Like all successful professors, I owe a great deal to my graduate
students. In addition to doing great research (and letting me take
some of the credit for it), Guha Balakrishnan, Davis Blalock, Joel
Brooks, Ganeshapillai Gartheeban, Jen Gong, Katie Lewis, Yun Liu,
Jose Javier Gonzalez Ortiz, Anima Singh, Divya Shanmugam, Jenna
Wiens, and Amy Zhao all provided useful comments on various
versions of this manuscript.
I owe a special debt of gratitude to Julie Sussman, P.P.A., who
edited the first two editions of this book, and Lisa Ruffolo who edited
the current edition. Both Julie and Lisa were collaborators who read
the book with the eyes of a student, and told me what needed to be
done, what should be done, and what could be done if I had the time
and energy to do it. They buried me in “suggestions” that were too
good to ignore.
Finally, thanks to my wife, Olga, for pushing me to finish and for
excusing me from various household duties so that I could work on
the book.
1
GETTING STARTED
4. Using this new guess, which we again call g, repeat the process
until g*g is close enough to x.
The recipe includes some tests for deciding when the process is
complete, as well as instructions about the order in which to execute
instructions, sometimes jumping to a specific instruction based on a
test.
So how does one capture the idea of a recipe in a mechanical
process? One way is to design a machine specifically intended to
compute square roots. Odd as this may sound, the earliest computing
machines were, in fact, fixed-program computers, meaning they
were designed to solve a specific mathematical problem, e.g., to
compute the trajectory of an artillery shell. One of the first
computers (built in 1941 by Atanasoff and Berry) solved systems of
linear equations, but could do nothing else. Alan Turing's bombe
machine, developed during World War II, was designed to break
German Enigma codes. Some simple computers still use this
approach. For example, a four-function calculator6 is a fixed-
program computer. It can do basic arithmetic, but it cannot be used
as a word processor or to run video games. To change the program of
such a machine, one has to replace the circuitry.
The first truly modern computer was the Manchester Mark 1.7 It
was distinguished from its predecessors by being a stored-
program computer. Such a computer stores (and manipulates) a
sequence of instructions, and has components that execute any
instruction in that sequence. The heart of such a computer is an
interpreter that can execute any legal set of instructions, and thus
can be used to compute anything that can be described using those
instructions. The result of the computation can even be a new
sequence of instructions, which can then be executed by the
computer that generated them. In other words, it is possible for a
computer to program itself.8
Both the program and the data it manipulates reside in memory.
Typically, a program counter points to a particular location in
memory, and computation starts by executing the instruction at that
point. Most often, the interpreter simply goes to the next instruction
in the sequence, but not always. In some cases, it performs a test,
and on the basis of that test, execution may jump to another point in
the sequence of instructions. This is called flow of control, and is
essential to allowing us to write programs that perform complex
tasks.
People sometimes use flowcharts to depict flow of control. By
convention, we use rectangular boxes to depict a processing step, a
diamond to depict a test, and arrows to indicate the order in which
things are done. Figure 1-1 contains a flowchart depicting an
approach to getting dinner.
21. Cfr. Polyb., 3, 24; Liv., 7, 27; Diod., 16, 69, 1. Diodoro menziona per
quest’anno (348) un trattato romano-cartaginese, che, per errore, dice
primo.
22. Sulla Campania in questo tempo e sui rapporti tra Osci, Etruschi, Greci
e Sanniti, cfr. A. Sogliano, Sanniti ed Osci, in Rendiconti della R.
Accademia dei Lincei, 1912, pp. 208 sgg.
23. Questa prima guerra sannitica, la cui realtà storica è stata revocata in
dubbio da taluni moderni, ci è attestata da Dionys., 15, 3, 2; da App.,
Samn., 1; da Liv., 7, 29 sgg.
25. Su questo trattato, cfr. gli accenni sparsi che si ricavano da Strab., 5, 4,
7; Polyb., 6, 14, 8.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com