Introduction to
Computation and
Programming Using Python
with Application to Understanding Data
Introduction to
Computation and
Programming Using Python
with Application to Understanding Data
Second Edition
John V. Guttag
The MIT Press
Cambridge, Massachusetts
London, England
© 2016 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 infor-
mation storage and retrieval) without permission in writing from the publisher.
Printed and bound in the United States of America.
Library of Congress Cataloging-in-Publication Data
Names: Guttag, John, author.
Title: Introduction to computation and programming using Python : with
application to understanding data / John V. Guttag.
Description: Second edition. | Cambridge, MA : The MIT Press, [2017] |
Includes index.
Identifiers: LCCN 2016019367 | ISBN 9780262529624 (pbk. : alk. paper)
Subjects: LCSH: Python (Computer program language)--Textbooks. |
Computer programming--Textbooks.
Classification: LCC QA76.73.P98 G88 2017 | DDC 005.13/3--dc23 LC record
available at https://lccn.loc.gov/2016019367
10 9 8 7 6 5 4 3 2 1
To my family:
Olga
David
Andrea
Michael
Mark
Addie
CONTENTS
PREFACE .................................................................................................................... xiii
ACKNOWLEDGMENTS ......................................................................................... xvii
1 GETTING STARTED .............................................................................................. 1
2 INTRODUCTION TO PYTHON .......................................................................... 7
2.1 The Basic Elements of Python ........................................................................ 9
2.1.1 Objects, Expressions, and Numerical Types .......................................... 9
2.1.2 Variables and Assignment ..................................................................... 12
2.1.3 Python IDE’s ........................................................................................... 14
2.2 Branching Programs ...................................................................................... 15
2.3 Strings and Input ........................................................................................... 18
2.3.1
Input ......................................................................................................... 20
2.3.2 A Digression About Character Encoding ............................................ 21
2.4
Iteration .......................................................................................................... 22
3 SOME SIMPLE NUMERICAL PROGRAMS ..................................................... 25
3.1 Exhaustive Enumeration ............................................................................... 25
3.2 For Loops ........................................................................................................ 27
3.3 Approximate Solutions and Bisection Search ............................................ 30
3.4 A Few Words About Using Floats ................................................................ 34
3.5 Newton-Raphson ........................................................................................... 37
4 FUNCTIONS, SCOPING, AND ABSTRACTION ............................................ 39
4.1 Functions and Scoping .................................................................................. 40
4.1.1 Function Definitions .............................................................................. 40
4.1.2 Keyword Arguments and Default Values ............................................. 42
4.1.3 Scoping ..................................................................................................... 43
4.2 Specifications .................................................................................................. 47
4.3 Recursion ........................................................................................................ 50
4.3.1 Fibonacci Numbers ................................................................................ 52
4.3.2 Palindromes ............................................................................................. 54
4.4 Global Variables ............................................................................................. 57
4.5 Modules .......................................................................................................... 59
4.6 Files ................................................................................................................. 61