John M. Zelle
Wartburg College
Franklin, Beedle & Associates Inc.+ 2154 NE Broadway, Suite 100 +Portland, Oregon 97232 + 503/284-6348 + www.fbeedle.com
Publisher
Editor
Production Associate Cover Photography Tom Sumner (tsumner@fbeedle.com) Brenda Jones
Jaron Ayres
Jim Leisy 2012
Printed in the U. S. A.
Names of all products herein are used for identification purposes only and are trademarks and/or registered trademarks of their respective owners. Franklin, Beedle & Associates Inc. makes no claim of ownership or corporate association with the products or companies that own them.
2017 Franklin, Beedle & Associates Incorporated. No part of this book may be reproduced, stored in a retrieval system, transmitted, or transcribed, in any form or by any means-electronic, mechanical, telepathic, photocopying, recording, or otherwisewithout prior written permission of the publish er. Requests for permission should be addressed as follows:
Rights and Permissions
Franklin, Beedle & Associates Incorporated 2154 NE Broadway, Suite 100
Portland, Oregon 97232
Library of Congress Cataloging-in-Publication data
Names: Zelle, John M., author.
Title: Python programming : an introduction to computer science I John M.
Zelle, Wartburg College.
Description: Third edition. I Portland, Oregon : Franklin, Beedle &
Associates Inc., [2016] I Includes bibliographical references and index. Identifiers: LCCN 2016024338 I ISBN 9781590282755
Subjects: LCSH: Python (Computer program language)
Classification: LCC QA76.73.P98 Z98 2016 I DDC 005.13/3--dc23 LC record available at https:/ /lccn.loc.gov/2016024338
Contents
Foreword, by Guido van Rossum ........................................................................................ ix
Preface ............................................................................................................................. x
Chapter 1 Computers and Programs 1
1.1 The Universal Machine ........................................................................................... 1
1.2 Program Power ......................................................................................................
3
1. 3 What Is Computer Science? ................................................................................... 3
1.4 Hardware Basics ..................................................................................................... 5
1.5 Programming Languages ........................................................................................ 6
1.6 The Magic of Python ............................................................................................. 9
1. 7 Inside a Python Program ...................................................................................... 15
1.8 Chaos and Computers .......................................................................................... 18
1. 9 Chapter S u m mary ................................................................................................ 20 1.10 Exercises .............................................................................................................. 21
Chapter 2 Writing Simple Programs 27
2.1 The Software Development Process ...................................................................... 27
2. 2 Exam pie Program: T em perature Converter ........................................................... 28
2.3 Elements of Programs ......................................................................................... 31
2.3.1 Names ..................................................................................................... 31
2.3.2 Expressions .............................................................................................. 32
2.4 0 utput Statements .............................................................................................. 34
2. 5 Assignment Statements ........................................................................................ 36
2. 5 .1 S i m pIe Assign men t .................................................................................. 3 7
2.5.2 Assigning Input ........................................................................................ 39
2.5.3 Simultaneous Assignment ......................................................................... 41
2. 6 Definite Loops ..................................................................................................... 43
. Contents IV
2.7 Example Program: Future Value ........................................................................... 47 2.8 Chapter Summary ................................................................................................ 50
2.9 Exercises ..............................................................................................................
Chapter 3 Computing with Numbers 57
3.1 Numeric Data Types ............................................................................................ 57 3. 2 Type Conversions and Rounding ........................................................................... 62
3.3 Using the Math Library ........................................................................................
65
3.4 Accumulating Results: Factorials .......................................................................... 68
3.5 Limitations of Computer Arithmetic ..................................................................... 71
3.6 Chapter Summary ................................................................................................ 75
3. 7 Exercises .............................................................................................................. 76
Chapter 4 Objects and Graphics 83
4.1 Overview .............................................................................................................. 83
4. 2 T h e 0 b j ect of 0 b j ects.......................................................................................... 84
4.3 Simple Graphics Programming .............................................................................. 85
4.4 Using Graphical Objects ....................................................................................... 91
4.5 Graphing Future Value ......................................................................................... 96
4.6 Choosing Coordinates ......................................................................................... 103
4. 7 Interactive Graphics ........................................................................................... 107
4.7.1 Getting Mouse Clicks ............................................................................. 107
4. 7.2 Handling Textual Input .......................................................................... 109
4.8 Graphics Module Reference ................................................................................ 112
4.8.1 Graph Win Objects ................................................................................. 113
4. 8. 2 G ra ph i cs 0 b j ects.................................................................................... 115