Fundamentals of Python First Programs 1st Edition Kenneth A. Lambert pdf download
Fundamentals of Python First Programs 1st Edition Kenneth A. Lambert pdf download
https://ebookname.com/product/fundamentals-of-python-first-
programs-1st-edition-kenneth-a-lambert/
https://ebookname.com/product/python-high-performance-
programming-boost-the-performance-of-your-python-programs-using-
advanced-techniques-1st-edition-gabriele-lanaro/
https://ebookname.com/product/programming-fundamentals-a-modular-
structured-approach-using-c-kenneth-leroy-busbee/
https://ebookname.com/product/reflections-of-a-technocrat-
managing-defense-air-and-space-programs-during-the-1st-edition-
john-l-kenneth-j-alnwick-lawrence/
https://ebookname.com/product/selected-letters-of-c-g-
jung-1909-1961-c-g-jung-editor/
A season in hell the life of Arthur Rimbaud Carré
https://ebookname.com/product/a-season-in-hell-the-life-of-
arthur-rimbaud-carre/
https://ebookname.com/product/weapons-of-mass-instruction-a-
schoolteacher-s-journey-through-the-dark-world-of-compulsory-
schooling-john-taylor-gatto/
https://ebookname.com/product/vowels-and-consonants-3rd-edition-
peter-ladefoged/
https://ebookname.com/product/c-programming-from-problem-
analysis-to-program-design-2nd-edition-barbara-doyle/
https://ebookname.com/product/transitions-and-learning-through-
the-lifecourse-1st-edition-kathryn-ecclestone-editor/
Victory over cancer Updated Edition M.D. Matthias Rath
https://ebookname.com/product/victory-over-cancer-updated-
edition-m-d-matthias-rath/
Fundamentals of
Python:
First Programs
Kenneth A. Lambert
Martin Osborne, Contributing Author
Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
This is an electronic version of the print textbook. Due to electronic rights restrictions,
some third party content may be suppressed. Editorial review has deemed that any suppressed
content does not materially affect the overall learning experience. The publisher reserves the right
to remove content from this title at any time if subsequent rights restrictions require it. For
valuable information on pricing, previous editions, changes to current editions, and alternate
formats, please visit www.cengage.com/highered to search by ISBN#, author, title, or keyword for
materials in your areas of interest.
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Fundamentals of Python: First Programs © 2012 Course Technology, Cengage Learning
Kenneth A. Lambert ALL RIGHTS RESERVED. No part of this work covered by the copyright
herein may be reproduced, transmitted, stored or used in any form or by
Executive Editor: Marie Lee
any means graphic, electronic, or mechanical, including but not limited to
Acquisitions Editor: Brandi Shailer photocopying, recording, scanning, digitizing, taping, Web distribution,
Senior Product Manager: Alyssa Pratt information networks, or information storage and retrieval systems, except
Development Editor: Ann Shaffer as permitted under Section 107 or 108 of the 1976 United States Copyright
Act, without the prior written permission of the publisher.
Associate Product Manager: Stephanie
Lorenz
For product information and technology assistance, contact us at
Associate Marketing Manager: Shanna
Cengage Learning Customer & Sales Support, 1-800-354-9706
Shelton
For permission to use material from this text or product, submit all
Content Project Manager: Jennifer Feltri requests online at www.cengage.com/permissions
Art Director: Faith Brosnan Further permissions questions can be emailed to
permissionrequest@cengage.com
Image credit: © istockphoto/Pei Ling Hoo
Cover Designer: Wing-ip Ngan,
Ink design, Inc. Library of Congress Control Number: 2011920241
Compositor: GEX Publishing Services ISBN-13: 978-1-111-82270-5
ISBN-10: 1-111-82270-0
Course Technology
20 Channel Center
Boston, Massachusetts 02210
USA
Purchase any of our products at your local college store or at our preferred
online store www.cengagebrain.com.
Some of the product names and company names used in this book have
been used for identification purposes only and may be trademarks or regis-
tered trademarks of their respective manufacturers and sellers.
Course Technology, a part of Cengage Learning, reserves the right to revise this
publication and make changes from time to time in its content without notice.
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Table of Contents
[CHAPTER] 1 INTRODUCTION 1
1.1 Two Fundamental Ideas of Computer Science: Algorithms and Information
Processing .................................................................................................................2
1.1.1 Algorithms ................................................................................................2
1.1.2 Information Processing............................................................................4
1.1 Exercises....................................................................................................................5
1.2 The Structure of a Modern Computer System .......................................................6
1.2.1 Computer Hardware ................................................................................6
1.2.2 Computer Software..................................................................................8
1.2 Exercises..................................................................................................................10
1.3 A Not-So-Brief History of Computing Systems...................................................10
1.3.1 Before Electronic Digital Computers ...................................................11
1.3.2 The First Electronic Digital Computers (1940–1950) .........................15
1.3.3 The First Programming Languages (1950–1965).................................16
1.3.4 Integrated Circuits, Interaction, and Timesharing (1965–1975) .........18
1.3.5 Personal Computing and Networks (1975–1990) ................................19
1.3.6 Consultation, Communication, and Ubiquitous Computing
(1990–Present)........................................................................................21
1.4 Getting Started with Python Programming..........................................................23
1.4.1 Running Code in the Interactive Shell .................................................23
1.4.2 Input, Processing, and Output...............................................................25
1.4.3 Editing, Saving, and Running a Script ..................................................28
1.4.4 Behind the Scenes: How Python Works ...............................................29
1.4 Exercises..................................................................................................................30
1.5 Detecting and Correcting Syntax Errors...............................................................31
1.5 Exercises..................................................................................................................32
Suggestions for Further Reading ...........................................................................32
Summary .................................................................................................................33
Review Questions ...................................................................................................35
Projects....................................................................................................................37
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
2.3 Strings, Assignment, and Comments.....................................................................47
2.3.1 Data Types..............................................................................................47
2.3.2 String Literals.........................................................................................48
2.3.3 Escape Sequences ...................................................................................50
2.3.4 String Concatenation .............................................................................50
2.3.5 Variables and the Assignment Statement ..............................................51
2.3.6 Program Comments and Docstrings.....................................................52
2.3 Exercises..................................................................................................................53
2.4 Numeric Data Types and Character Sets ..............................................................54
2.4.1 Integers ...................................................................................................54
2.4.2 Floating-Point Numbers........................................................................55
2.4.3 Character Sets ........................................................................................55
2.4 Exercises..................................................................................................................57
2.5 Expressions .............................................................................................................58
2.5.1 Arithmetic Expressions ..........................................................................58
2.5.2 Mixed-Mode Arithmetic and Type Conversions ..................................60
2.5 Exercises..................................................................................................................63
2.6 Using Functions and Modules ...............................................................................63
2.6.1 Calling Functions: Arguments and Return Values................................64
2.6.2 The math Module .................................................................................65
2.6.3 The Main Module..................................................................................66
2.6.4 Program Format and Structure .............................................................67
2.6.5 Running a Script from a Terminal Command Prompt ........................68
2.6 Exercises..................................................................................................................70
Summary .................................................................................................................70
Review Questions ...................................................................................................72
Projects....................................................................................................................73
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3.4.3 One-Way Selection Statements.............................................................94
3.4.4 Multi-way if Statements ......................................................................95
3.4.5 Logical Operators and Compound Boolean Expressions.....................97
3.4.6 Short-Circuit Evaluation .......................................................................99
3.4.7 Testing Selection Statements ...............................................................100
3.4 Exercises................................................................................................................101
3.5 Conditional Iteration: The while Loop ............................................................102
3.5.1 The Structure and Behavior of a while Loop ..................................102
3.5.2 Count Control with a while Loop....................................................104
3.5.3 The while True Loop and the break Statement ..........................105
3.5.4 Random Numbers................................................................................107
3.5.5 Loop Logic, Errors, and Testing .........................................................109
3.5 Exercises................................................................................................................109
3.6 Case Study: Approximating Square Roots...........................................................110
3.6.1 Request .................................................................................................110
3.6.2 Analysis .................................................................................................110
3.6.3 Design...................................................................................................110
3.6.4 Implementation (Coding) ....................................................................112
3.6.5 Testing ..................................................................................................113
Summary ...............................................................................................................113
Review Questions .................................................................................................116
Projects..................................................................................................................118
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4.5 Exercises................................................................................................................148
4.6 Case Study: Text Analysis.....................................................................................148
4.6.1 Request .................................................................................................149
4.6.2 Analysis .................................................................................................149
4.6.3 Design...................................................................................................150
4.6.4 Implementation (Coding) ....................................................................151
4.6.5 Testing ..................................................................................................152
Summary ...............................................................................................................153
Review Questions .................................................................................................154
Projects..................................................................................................................156
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5.5 Case Study: Nondirective Psychotherapy ...........................................................191
5.5.1 Request .................................................................................................191
5.5.2 Analysis .................................................................................................191
5.5.3 Design...................................................................................................192
5.5.4 Implementation (Coding) ....................................................................193
5.5.5 Testing ..................................................................................................195
Summary ...............................................................................................................195
Review Questions .................................................................................................196
Projects..................................................................................................................198
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
6.6 Exercises................................................................................................................239
Summary ...............................................................................................................240
Review Questions .................................................................................................242
Projects..................................................................................................................244
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
8.1.4 The __init__ Method and Instance Variables................................299
8.1.5 The __str__ Method........................................................................300
8.1.6 Accessors and Mutators .......................................................................300
8.1.7 The Lifetime of Objects ......................................................................301
8.1.8 Rules of Thumb for Defining a Simple Class.....................................302
8.1 Exercises................................................................................................................303
8.2 Case Study: Playing the Game of Craps .............................................................303
8.2.1 Request .................................................................................................303
8.2.2 Analysis .................................................................................................303
8.2.3 Design...................................................................................................304
8.2.4 Implementation (Coding) ....................................................................306
8.3 Data-Modeling Examples.....................................................................................309
8.3.1 Rational Numbers ................................................................................309
8.3.2 Rational Number Arithmetic and Operator Overloading..................311
8.3.3 Comparison Methods...........................................................................312
8.3.4 Equality and the __eq__ Method ......................................................314
8.3.5 Savings Accounts and Class Variables .................................................315
8.3.6 Putting the Accounts into a Bank........................................................317
8.3.7 Using pickle for Permanent Storage of Objects.............................319
8.3.8 Input of Objects and the try-except Statement............................320
8.3.9 Playing Cards .......................................................................................321
8.3 Exercises................................................................................................................325
8.4 Case Study: An ATM............................................................................................325
8.4.1 Request .................................................................................................325
8.4.2 Analysis .................................................................................................325
8.4.3 Design...................................................................................................327
8.4.4 Implementation (Coding) ....................................................................329
8.5 Structuring Classes with Inheritance and Polymorphism...................................331
8.5.1 Inheritance Hierarchies and Modeling ...............................................332
8.5.2 Example: A Restricted Savings Account..............................................333
8.5.3 Example: The Dealer and a Player in the Game of Blackjack ...........335
8.5.4 Polymorphic Methods..........................................................................340
8.5.5 Abstract Classes ...................................................................................340
8.5.6 The Costs and Benefits of Object-Oriented Programming...............341
8.5 Exercises................................................................................................................343
Summary ...............................................................................................................343
Review Questions .................................................................................................345
Projects..................................................................................................................346
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
9.2.5 Entry Fields for the Input and Output of Text ...................................363
9.2.6 Using Pop-up Dialog Boxes ................................................................365
9.2 Exercises................................................................................................................366
9.3 Case Study: A GUI-Based ATM..........................................................................367
9.3.1 Request .................................................................................................367
9.3.2 Analysis .................................................................................................367
9.3.3 Design...................................................................................................368
9.3.4 Implementation (Coding) ....................................................................369
9.4 Other Useful GUI Resources ..............................................................................372
9.4.1 Colors ...................................................................................................373
9.4.2 Text Attributes......................................................................................373
9.4.3 Sizing and Justifying an Entry .............................................................374
9.4.4 Sizing the Main Window.....................................................................375
9.4.5 Grid Attributes .....................................................................................376
9.4.6 Using Nested Frames to Organize Components................................380
9.4.7 Multi-Line Text Widgets .....................................................................381
9.4.8 Scrolling List Boxes .............................................................................384
9.4.9 Mouse Events .......................................................................................387
9.4.10 Keyboard Events ..................................................................................388
9.4 Exercises................................................................................................................389
Summary ...............................................................................................................390
Review Questions .................................................................................................391
Projects..................................................................................................................392
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
[ONLINE CHAPTER] 11 SEARCHING, SORTING, AND COMPLEXITY ANALYSIS
PREFACE [ xiii ]
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Why Python?
Computer technology and applications have become increasingly more sophisti-
cated over the past two decades, and so has the computer science curriculum, espe-
cially at the introductory level. Today’s students learn a bit of programming and
problem–solving, and are then expected to move quickly into topics like software
development, complexity analysis, and data structures that, twenty years ago, were
relegated to advanced courses. In addition, the ascent of object-oriented program-
ming as the dominant paradigm of problem solving has led instructors and text-
book authors to bring powerful, industrial-strength programming languages such as
C++ and Java into the introductory curriculum. As a result, instead of experiencing
the rewards and excitement of solving problems with computers, beginning com-
puter science students often become overwhelmed by the combined tasks of mas-
tering advanced concepts as well as the syntax of a programming language.
This book uses the Python programming language as a way of making the
first year of computer science more manageable and attractive for students and
instructors alike. Python has the following pedagogical benefits:
Python has simple, conventional syntax. Python statements are very close to
Python. Python also includes all of the advanced features of a modern pro-
gramming language, such as support for data structures and object-oriented
software development, for use when they become necessary.
Python is highly interactive. Expressions and statements can be entered at
[ xiv ] PREFACE
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
To summarize these benefits, Python is a comfortable and flexible vehicle for
expressing ideas about computation, both for beginners and for experts as well. If
students learn these ideas well in the first course, they should have no problems
making a quick transition to other languages needed for courses later in the cur-
riculum. Most importantly, beginning students will spend less time staring at a
computer screen and more time thinking about interesting problems to solve.
PREFACE [ xv ]
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Chapter 8 introduces object-oriented design with class and method defini-
tions. Several examples of simple class definitions from different application
domains are presented. Some of these are then integrated into more realistic
applications, to show how object-oriented software components can be used to
build complex systems. Emphasis is on designing appropriate interfaces for
classes that exploit inheritance and polymorphism.
Chapters 9 and 10 cover advanced material related to two important areas of
computing: graphical user interfaces and networks. Although these two chapters
are entirely optional, they give students challenging experiences at the end of the
first course. Chapter 9 contrasts the event-driven model of GUI programs with
the process-driven model of terminal-based programs. The creation and layout of
GUI components are explored, as well as the decomposition of a GUI-based pro-
gram using the model/view/controller pattern. Chapter 10 introduces multi-
threaded programs and the construction of simple network-based client/server
applications.
Chapter 11 covers some topics addressed at the beginning of a traditional
CS2 course, and is available on the publisher’s Web site. This chapter introduces
complexity analysis with big-O notation. Enough material is presented to enable
you to perform simple analyses of the running time and memory usage of algo-
rithms and data structures, using search and sort algorithms as examples.
Special Features
This book explains and develops concepts carefully, using frequent examples and
diagrams. New concepts are then applied in complete programs to show how
they aid in solving problems. The chapters place an early and consistent emphasis
on good writing habits and neat, readable documentation.
The book includes several other important features:
Case studies—These present complete Python programs ranging from the
of learning objectives and ends with a summary of the major concepts cov-
ered in the chapter.
Key terms and a glossary—When a technical term is introduced in the text,
[ xvi ] PREFACE
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Exercises—Most major sections of each chapter end with exercise ques-
tions that reinforce the reading by asking basic questions about the mate-
rial in the section. Each chapter ends with a set of review exercises.
Programming projects—Each chapter ends with a set of programming
projects of varying difficulty.
A software toolkit for image processing—This book comes with an open-
source Python toolkit for the easy image processing discussed in Chapter 7.
The toolkit can be obtained from the student downloads page on
www.course.com, or at http://home.wlu.edu/~lambertk/python/
Appendices—Three appendices include information on obtaining Python
resources, installing the toolkit, and using the toolkit’s interface.
Supplemental Resources
The following supplemental materials are available when this book is used in a
classroom setting. All of the teaching tools available with this book are provided
to the instructor on a single CD-ROM.
Exercises.
ExamView®
This textbook is accompanied by ExamView, a powerful testing software package
that allows instructors to create and administer printed, computer (LAN-based),
and Internet exams. ExamView includes hundreds of questions that correspond to
the topics covered in this text, enabling students to generate detailed study guides
that include page references for further review. These computer-based and
Internet testing components allow students to take exams at their computers, and
save the instructor time because each exam is graded automatically.
PowerPoint Presentations
This book comes with Microsoft PowerPoint slides for each chapter. These are
included as a teaching aid either to make available to students on the network for
chapter review, or to be used during classroom presentations. Instructors can
modify slides or add their own slides to tailor their presentations.
PREFACE [ xvii ]
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Distance Learning
Course Technology is proud to offer online courses in WebCT and Blackboard.
For more information on how to bring distance learning to your course, contact
your local Cengage Learning sales representative.
Source Code
The source code is available at www.cengagebrain.com—and also is available on the
Instructor Resources CD-ROM. If an input file is needed to run a program, it is
included with the source code.
Solution files
The solution files for all programming exercises are available at www.cengagebrain.com
and are available on the Instructor Resources CD-ROM. If an input file is needed to
run a programming exercise, it is included with the solution file.
Acknowledgments
I would like to thank my contributing author, Martin Osborne, for many years of
advice, friendly criticism, and encouragement on several of my book projects. To
my colleague, Joshua Stough, and our students at Washington and Lee University for
classroom testing this book over several semesters.
In addition, I would like to thank the following reviewers for the time and
effort they contributed to Fundamentals of Python: Paul Albee, Central Michigan
University; Andrew Danner, Swarthmore College; Susan Fox, Macalester
College; Robert Franks, Central College; and Jim Slack, Minnesota State
University, Mankato. Also, thank you to the following reviewers who contributed
their thoughts on the original book proposal: Christian Blouin, Dalhousie
University; Margaret Iwobi, Binghamton University; Sam Midkiff, Purdue
University; and Ray Morehead, West Virginia University.
[ xviii ] PREFACE
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Also, thank you to the individuals at Course Technology who helped to assure
that the content of all data and solution files used for this text were correct and
accurate: Chris Scriver, MQA Project Leader and Serge Palladino, MQA Tester.
Finally, thanks to several other people whose work made this book possible:
Ann Shaffer, Developmental Editor; Brandi Shailer, Acquisitions Editor, Course
Technology; Alyssa Pratt, Senior Product Manager, Course Technology; and
Jennifer Feltri, Content Project Manager, Course Technology.
Dedication
To my students in Computer Science 111
Kenneth A. Lambert
Lexington, VA
PREFACE [ xix ]
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
[CHAPTER]
1 Introduction
After completing this chapter, you will be able to
Describe the basic features of an algorithm
Explain how hardware and software collaborate in a com-
puter’s architecture
Give a brief history of computing
Compose and run a simple Python program
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
for thousands of years, and that the world as we know it has been so involved in
and with computer technology for only the past 25 years or so.
In the chapters that follow, you will learn about computer science, which is
the study of computation that has made this new technology and this new world
possible. You will also learn how to use computers effectively and appropriately to
enhance your own life and the lives of others.
1.1.1 Algorithms
People computed long before the invention of modern computing devices, and
many continue to use computing devices that we might consider primitive. For
example, consider how merchants made change for customers in marketplaces
before the existence of credit cards, pocket calculators, or cash registers. Making
change can be a complex activity. It probably took you some time to learn how to
do it, and it takes some mental effort to get it right every time. Let’s consider
what’s involved in this process.
The first step is to compute the difference between the purchase price and
the amount of money that the customer gives the merchant. The result of this
calculation is the total amount that the merchant must return to the purchaser.
For example, if you buy a dozen eggs at the farmers’ market for $2.39 and you
give the farmer a $10 bill, she should return $7.61 to you. To produce this
amount, the merchant selects the appropriate coins and bills that, when added to
$2.39, make $10.00.
Few people can subtract three-digit numbers without resorting to some man-
ual aids, such as pencil and paper. As you learned in grade school, you can carry
out subtraction with pencil and paper by following a sequence of well-defined
steps. You have probably done this many times but never made a list of the
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
specific steps involved. Making such lists to solve problems is something com-
puter scientists do all the time. For example, the following list of steps describes
the process of subtracting two numbers using a pencil and paper:
Step 1 Write down the two numbers, with the larger number above the
smaller number and their digits aligned in columns from the right.
Step 2 Assume that you will start with the rightmost column of digits and
work your way left through the various columns.
Step 3 Write down the difference between the two digits in the current
column of digits, borrowing a 1 from the top number’s next column
to the left if necessary.
Step 4 If there is no next column to the left, stop. Otherwise, move to the
next column to the left, and go to Step 3.
If the computing agent (in this case a human being) follows each of these
simple steps correctly, the entire process results in a correct solution to the given
problem. We assume in Step 3 that the agent already knows how to compute the
difference between the two digits in any given column, borrowing if necessary.
To make change, most people can select the combination of coins and bills
that represent the correct change amount without any manual aids, other than
the coins and bills. But the mental calculations involved can still be described in a
manner similar to the preceding steps, and we can resort to writing them down
on paper if there is a dispute about the correctness of the change.
The sequence of steps that describes each of these computational processes is
called an algorithm. Informally, an algorithm is like a recipe. It provides a set of
instructions that tells us how to do something, such as make change, bake bread,
or put together a piece of furniture. More precisely, an algorithm describes a
process that ends with a solution to a problem. The algorithm is also one of the
fundamental ideas of computer science. An algorithm has the following features:
1 An algorithm consists of a finite number of instructions.
2 Each individual instruction in an algorithm is well defined. This means that
the action described by the instruction can be performed effectively or be
executed by a computing agent. For example, any computing agent capa-
ble of arithmetic can compute the difference between two digits. So an
algorithmic step that says “compute the difference between two digits”
would be well defined. On the other hand, a step that says “divide a number
by 0” is not well defined, because no computing agent could carry it out.
1.1 Two Fundamental Ideas of Computer Science: Algorithms and Information Processing [3]
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3 An algorithm describes a process that eventually halts after arriving at a
solution to a problem. For example, the process of subtraction halts after
the computing agent writes down the difference between the two digits
in the leftmost column of digits.
4 An algorithm solves a general class of problems. For example, an algo-
rithm that describes how to make change should work for any two
amounts of money whose difference is greater than or equal to $0.00.
Creating a list of steps that describe how to make change might not seem like
a major accomplishment to you. But the ability to break a task down into its com-
ponent parts is one of the main jobs of a computer programmer. Once we write
an algorithm to describe a particular type of computation, a machine can be built
to do the computing. Put another way, if we can develop an algorithm to solve a
problem, we can automate the task of solving the problem. You might not feel
compelled to write a computer program to automate the task of making change,
because you can probably already make change yourself fairly easily. But suppose
you needed to do a more complicated task—such as sorting a list of 100 names.
In that case, a computer program would be very handy.
Computers can be designed to run a small set of algorithms for performing
specialized tasks such as operating a microwave oven. But we can also build com-
puters, like the one on your desktop, that are capable of performing a task
described by any algorithm. These computers are truly general-purpose problem-
solving machines. They are unlike any machines we have ever built before, and
they have formed the basis of the completely new world in which we live.
Later in this book, we introduce a notation for expressing algorithms and
some suggestions for designing algorithms. You will see that algorithms and algo-
rithmic thinking are critical underpinnings of any computer system.
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Like mathematical calculations, information processing can be described with
algorithms. In our earlier example of making change, the subtraction steps
involved manipulating symbols used to represent numbers and money. In carry-
ing out the instructions of any algorithm, a computing agent manipulates infor-
mation. The computing agent starts with some given information (known as
input), transforms this information according to well-defined rules, and produces
new information, known as output.
It is important to recognize that the algorithms that describe information
processing can also be represented as information. Computer scientists have been
able to represent algorithms in a form that can be executed effectively and effi-
ciently by machines. They have also designed real machines, called electronic
digital computers, which are capable of executing algorithms.
Computer scientists more recently discovered how to represent many other
things, such as images, music, human speech, and video, as information. Many of
the media and communication devices that we now take for granted would be
impossible without this new kind of information processing. We examine many of
these achievements in more detail in later chapters.
1.1 Exercises
These short end-of-section exercises are intended to stimulate your thinking
about computing.
1 List three common types of computing agents.
2 Write an algorithm that describes the second part of the process of mak-
ing change (counting out the coins and bills).
3 Write an algorithm that describes a common task, such as baking a cake
or operating a DVD player.
4 Describe an instruction that is not well defined and thus could not be
included as a step in an algorithm. Give an example of such an instruction.
5 In what sense is a desktop computer a general-purpose problem-solving
machine?
6 List four devices that use computers and describe the information that
they process. (Hint: Think of the inputs and outputs of the devices.)
1.1 Two Fundamental Ideas of Computer Science: Algorithms and Information Processing [5]
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Other documents randomly have
different content
THE TWO WAYS
Some wait ever for the morrow—let the present hours slip by:
“So little can be done to-day, what’s the use to try?”
Notice, he who grasps the moments, lad, every one that flies,
Is the man in life’s sharp contest who obtains the victor’s prize.
TO-DAY AND TO-
MORROW
TO-DAY
A sunless sky,
Unaccomplished aim,
The flag of Hope at half mast furled,—
A bitter cry,
“I’ve tried—no gain,—
O empty, disappointing world!”
TO-MORROW
A rosy light,
Success attained,
The banner of Victory to the breezes hurled,—
A cry of might,
“The mastery gained,
Hail! glorious, God-given world!”
A BEAUTIFUL RESULT
And thus ’twas so all through the town. Each testified the same;
Not one was half so happy as before the Wish-Man came.
“Ah, ha!” Perhaps by this you’ve guessed who was the stranger man;
If not, by throwing out this hint, I’m very sure you can!
That night, when everything was still, there crept from room to
room,
Some one who gathered up each wish that caused such direful
gloom,
And when old Sol arose next day, and scattered sunbeams down,
They fell upon—the name was changed—upon Contented Town!
A LITTLE
MATHEMATICIAN
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.
ebookname.com