Preface
Preface
Fundamentals of
Python:
From First Programs
Through Data Structures
Kenneth A. Lambert
Martin Osborne, Contributing Author
Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States
FM C6840 43079 10/27/08 9:51 AM Page ii
ISBN-13: 978-1-4239-0218-8
ISBN-10: 1-4239-0218-1
Course Technology
25 Thomson Place
Boston, Massachusetts 02210
USA
Purchase any of our products at your local college store or at our preferred
online store www.ichapters.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.
Printed in Canada
1 2 3 4 5 6 7 12 11 10 09 08
FM C6840 43079 10/27/08 9:51 AM Page xxi
PREFACE
Welcome to Fundamentals of Python. This text is intended for a complete, first-
year study of programming and problem-solving. It covers the material taught in
typical Computer Science 1 and Computer Science 2 courses (CS1 and CS2) at
the undergraduate level.
This book covers five major aspects of computing:
1 Programming Basics—Data types, control structures, algorithm devel-
opment, and program design with functions are basic ideas that you need
to master in order to solve problems with computers. This book exam-
ines these core topics in detail and gives you practice employing your
understanding of them to solve a wide range of problems.
2 Object-Oriented Programming (OOP)—Object-Oriented
Programming is the dominant programming paradigm used to develop
large software systems. This book introduces you to the fundamental
principles of OOP and enables you to apply them successfully.
3 Data and Information Processing—Most useful programs rely on data
structures to solve problems. These data structures include strings,
arrays, files, lists, stacks, queues, trees, sets, dictionaries, and graphs. This
book gives you experience using, building, and assessing the performance
of data structures. The general concept of an abstract data type is intro-
duced, as is the difference between abstraction and implementation.
You’ll learn to use complexity analysis to evaluate space/time tradeoffs of
different implementations of ADTs.
4 Software Development Life Cycle—Rather than isolate software
development techniques in one or two chapters, this book deals with
them throughout in the context of numerous case studies. Among other
things, you’ll learn that coding a program is often not the most difficult
or challenging aspect of problem solving and software development.
5 Contemporary Applications of Computing—The best way to learn
about programming and problem solving is to create interesting programs
with real-world applications. In this book, you’ll begin by creating applica-
tions that involve numerical problems and text processing. For example,
you’ll learn the basics of encryption techniques such as those that are used
to make your credit card number and other information secure on the
Internet. But unlike many other introductory texts, this one does not
restrict itself to problems involving numbers and text. Most contemporary
applications involve graphical user interfaces, event-driven programming,
graphics, and network communications. These topics are presented in
optional, standalone chapters.
PREFACE [ xxi ]
FM C6840 43079 10/27/08 9:51 AM Page xxii
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
[ xxii ] PREFACE
FM C6840 43079 10/27/08 9:51 AM Page xxiii
PREFACE [ xxiii ]
FM C6840 43079 10/27/08 9:51 AM Page xxiv
[ xxiv ] PREFACE
FM C6840 43079 10/27/08 9:51 AM Page xxv
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,
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
two open-source Python toolkits for the easy graphics and image processing
discussed in Chapter 7. These are can be obtained from the student down-
loads page on www.course.com, or at http://home.wlu.edu/~lambertk/python/
! Appendices—Three appendices include information on obtaining Python
PREFACE [ xxv ]
FM C6840 43079 10/27/08 9:51 AM Page xxvi
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.
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.cengage.com/computerscience, and also is avail-
able on the Instructor Resources CD-ROM. If an input file is needed to run a
program, it is included with the source code.
[ xxvi ] PREFACE
FM C6840 43079 10/27/08 9:51 AM Page xxvii
Solution files
The solution files for all programming exercises are available at www.cengage.com/
computerscience 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.
I would like to thank my colleague, Sara Sprenkle, and our students at
Washington and Lee University for classroom testing this book over several semesters.
I would like to thank the following reviewers for the time and effort they
contributed as they completed their reviews of each chapter: 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 con-
tributed 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.
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, Shaffer Technical Editing, LLC; Marisa
Taylor, Senior Project Manager, GEX Inc.; Amy Jollymore, Acquisitions Editor,
Course Technology; Alyssa Pratt, Senior Product Manager, Course Technology;
and Matt Hutchinson, Content Project Manager, Course Technology.
PREFACE [ xxvii ]
FM C6840 43079 10/27/08 9:51 AM Page xxviii
Dedication
To my pal, Ken Van Ness
Kenneth A. Lambert
Lexington, VA
[ xxviii ] PREFACE