Cay Horstmann
Rance Necaise
P
y
t
hon
for
Everyone
3/e
Cay Horstmann
San Jose State University
Rance Necaise
Randolph-Macon College
P
y
t
hon
for
Everyone
3/e
VICE PRESIDENT AND EXECUTIVE PUBLISHER
Laurie
Rosatone
EXECUTIVE EDITOR
Joanna Dingle
PROJECT MANAGER/DEVELOPMENT EDITOR
Cindy
Johnson
EDITORIAL
ASSISTANT
Crystal Franks
LEAD PRODUCT DESIGNER
Tom
Kulesa
MARKETING MANAGER
Michael
MacDougald
PRODUCTION
MANAGER
Nichole
Urban
PRODUCTION
MANAGER
Nicole
Repasky
PRODUCTION MANAGEMENT SERVICES
Cindy Johnson, Publishing Services
PHOTO EDITOR
Anindita Adiyal
COVER DESIGNER
Joanna
Vieira
COVER PHOTOS (castle) © Tom Watson/EyeEm/Getty Images;
(chalice) © Paul Fleet/Getty Images, Inc.;
(balloon) © Mikhail Mishchenko/123RFLimited;
(trumpets) © modella/123RF.com.
This book was set in 10.5/12 Stempel Garamond LT Std by Publishing Services, and printed and bound by Quad
Graphics/Versailles. The cover was printed by Quad Graphics/Versailles.
Founded in 1807, John Wiley & Sons, Inc. has been a valued source of knowledge and understanding for more than
200 years, helping people around the world meet their needs and fulfill their aspirations. Our company is built on
a foundation of principles that include responsibility to the communities we serve and where we live and work.
In 2008, we launched a Corporate Citizenship Initiative, a global effort to address the environmental, social, eco-
nomic, and ethical challenges we face in our business. Among the issues we are addressing are carbon impact, paper
specifications and procurement, ethical conduct within our business and among our vendors, and community and
charitable support. For more information, please visit our website: www.wiley.com/go/citizenship.
This book is printed on acid-free paper. oo
Copyright © 2019, 2016 John Wiley & Sons, Inc. All rights reserved. No part of this publication may be repro-
duced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photo-
copying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United
States Copyright Act, without either the prior written permission of the Publisher, or authorization through
payment of the appropriate per-copy fee to the Copyright Clearance Center, Inc., 222 Rosewood Drive, Dan-
vers, MA 01923, (978) 750-8400, fax (978) 646-8600, or on the Web at www.copyright.com. Requests to the
Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111
River Street, Hoboken, NJ 07030-5774, (201) 748-6011, fax (201) 748-6008.
Evaluation copies are provided to qualified academics and professionals for review purposes only, for use in
their courses during the next academic year. These copies are licensed and may not be sold or transferred to a
third party. Upon completion of the review period, please return the evaluation copy to Wiley. Return instruc-
tions and a free of charge return shipping label are available at: www.wiley.com/go/returnlabel. If you have
chosen to adopt this textbook for use in your course, please accept
th
is book as your complimentary desk copy.
Outside of the United States, please contact your local representative.
ePUB ISBN 978-1-119-49853-7
Printed in the United States of America.
The inside back cover will contain printing identification and country of origin if omitted from this page. In
addition, if the ISBN on the back cover differs from the ISBN on this page, the one on the back cover is correct.
10 9 8 7 6 5 4 3 2 1
This book is an introduction to computer programming using Python that focuses on
the essentials—and on effective learning. Designed to serve a wide range of student
interests and abilities, it is suitable for a first course in programming for computer
scientists, engineers, and students in other disciplines. No prior programming expe-
rience is required, and only a modest amount of high school algebra is needed. For
pedagogical reasons, the book uses Python 3, which is more regular than Python 2.
Here are the book’s key features:
Present fundamentals first.
The book takes a traditional route, first stressing control structures, functions,
procedural decomposition, and the built-in data structures. Objects are used when
appropriate in the early chapters. Students start designing and implementing their
own classes in Chapter 9.
Guidance and worked examples help students succeed.
Beginning programmers often ask “How do I start? Now what do I do?” Of course,
an activity as complex as programming cannot be reduced to cookbook-style
instructions. However, step-by-step guidance is immensely helpful for building con-
fidence and providing an outline for the task at hand. “Problem Solving” sections
stress the importance of design and planning. “How To” guides help students with
common programming tasks. Numerous “Worked Examples” demonstrate how to
apply chapter concepts to interesting problems.
Problem solving strategies are made explicit.
Practical, step-by-step illustrations of techniques help students devise and evaluate
solutions to programming problems. Introduced where they are most relevant, these
strategies address barriers to success for many students. Strategies included are:
•
Algorithm Design (with pseudocode)
•
First Do It By Hand (doing sample
calculations by hand)
•
Flowcharts
•
Test Cases
•
Hand-Tracing
•
Storyboards
•
Solve a Simpler Problem First
•
Reusable Functions
Practice makes perfect.
•
Stepwise Refinement
•
Adapting Algorithms
•
Discovering Algorithms by
Manipulating Physical Objects
•
Tracing Objects
•
Patterns for Object Data
•
Thinking Recursively
•
Estimating the Running Time of
an Algorithm
Of course, programming students need to be able to implement nontrivial programs,
but they first need to have the confidence that they can succeed. Each section con-
tains numerous exercises that as
k students to carry out progressively more complex
tasks: trace code and understand its effects, produce program snippets from prepared
parts, and complete simple programs. Additional review and programming problems
are provided at the end of each chapter.
iii
PREFACE