Complete Download (eBook PDF) Introduction to Programming Using Python An 1 PDF All Chapters
Complete Download (eBook PDF) Introduction to Programming Using Python An 1 PDF All Chapters
https://ebookluna.com/product/python-programming-an-introduction-to-
computer-science-3rd-edition-by-john-zelle-ebook-pdf/
ebookluna.com
https://ebookluna.com/product/ebook-pdf-introduction-to-programming-
using-visual-basic-10th-edition/
ebookluna.com
https://ebookluna.com/download/an-introduction-to-parallel-
programming-ebook-pdf/
ebookluna.com
https://ebookluna.com/product/ebook-pdf-java-an-introduction-to-
problem-solving-and-programming-7th-edition/
ebookluna.com
(eBook PDF) Java: An Introduction to Problem Solving and
Programming 8th Edition
https://ebookluna.com/product/ebook-pdf-java-an-introduction-to-
problem-solving-and-programming-8th-edition/
ebookluna.com
https://ebookluna.com/product/ebook-pdf-python-programming-in-
context-3rd-edition/
ebookluna.com
https://ebookluna.com/product/python-programming-in-context-3rd-
edition-ebook-pdf/
ebookluna.com
https://ebookluna.com/product/ebook-pdf-microsoft-visual-c-an-
introduction-to-object-oriented-programming-7th-edition/
ebookluna.com
https://ebookluna.com/download/problem-solving-and-python-programming-
ebook-pdf/
ebookluna.com
Contents
Preface xi
Acknowledgments xv
Programming Projects 74
Appendices
Appendix A ASCII Values 349
Appendix B Reserved Words 351
Appendix C Installing Python and IDLE 353
Answers 355
Index 405
This page intentionally left blank
Preface
S ince its introduction in the 1990s, Python has become one of the most widely used
programming languages in the software industry. Also, students learning their first
programming language find Python the ideal tool to understand the development of
computer programs.
My objectives when writing this text were as follows:
1. To develop focused chapters. Rather than covering many topics superficially,
I concentrate on important subjects and cover them thoroughly.
2. To use examples and exercises with which students can relate, appreciate, and feel
comfortable. I frequently use real data. Examples do not have so many embel-
lishments that students are distracted from the programming techniques
illustrated.
3. To produce compactly written text that students will find both readable and informa-
tive. The main points of each topic are discussed first and then the peripheral
details are presented as comments.
4. To teach good programming practices that are in step with modern programming
methodology. Problem-solving techniques, structured programming, and
object-oriented programming are thoroughly discussed.
5. To provide insights into the major applications of computers.
xi
xii ◆ Preface
or are best appreciated after the student has thought about them. The reader should
seriously attempt the practice problems and study their solutions before moving on
to the exercises.
Comments. Extensions and fine points of new topics are deferred to the “Comments”
portion at the end of each section so that they will not interfere with the flow of the
presentation.
Key Terms and Concepts. In Chapters 2 through 8, the key terms and concepts (along
with examples) are summarized at the end of the chapter.
Guide to Application Topics. This section provides an index of programs that deal
with various topics including Business, Economics, Mathematics, and Sports.
VideoNotes. Twenty-four VideoNotes are available at www.pearsonhighered.com/
schneider. VideoNotes are Pearson’s visual tool designed for teaching key program-
ming concepts and techniques. VideoNote icons are placed in the margin of the text
book to notify the reader when a topic is discussed in a video. Also, a Guide to Video
Notes summarizing the different videos throughout the text is included.
Solution Manuals. The Student Solutions Manual contains the answer to every odd-
numbered exercise (not including programming projects). The Instructor Solutions
Manual contains the answer to every exercise and programming project. Both solu-
tion manuals are in pdf format and can be downloaded from the Publisher’s website.
Source Code and Data Files. The programs for all examples and the data files needed
for the exercises can be downloaded from the Publisher’s website.
Instructor Resources
The following protected instructor resource materials are available on the Publisher’s
website at www.pearsonhighered.com/schneider. For username and password infor-
mation, please contact your local Pearson representative.
Preface ◆ xiii
Student Resources
Access to the Premium website and VideoNotes tutorials is located at www
.pearsonhighered.com/schneider. Students must use the access card located in the
front of the book to register and access the online material. If there is no access
card in the front of this textbook, students can purchase access by going to www
.pearsonhighered.com/schneider and selecting “purchase access to premium con-
tent.” Instructors must register on the site to access the material.
The following content is available through the Premium website:
• VideoNotes
• Student Solutions Manual
• Programs for examples (Data files needed for the exercises are included in the
Programs folder.)
This page intentionally left blank
Acknowledgments
xv
Visit https://ebookluna.com
now to explore a diverse
collection of ebooks available
in formats like PDF, EPUB, and
MOBI, compatible with all
devices. Don’t miss the chance
to enjoy exciting offers and
quickly download high-quality
materials in just a few simple
steps!
This page intentionally left blank
1
An Introduction to
Computing and Problem
Solving
1.1 An Introduction to Computing and Python 2
1
2 ◆ Chapter 1 An Introduction to Computing and Problem Solving
Question: Why did you decide to use Python as the programming language?
Answer: Many people consider Python to be the best language to teach beginners how to
program. We agree. Also, Python is being used by major software companies. Python is
powerful, easy to write and read, easy to download and install, and it runs under Windows,
Mac, and Linux operating systems.
Question: This book uses the editor IDLE to create programs. How did IDLE get its name?
Answer: Idle stands for Integrated DeveLopment Environment. (Some people think the
name was chosen as a tribute to Eric Idle, a founding member of the Monty Python group.)
The IDLE editor has many features (such as color coding and formatting assistance) that
help the programmer.
Question: What are the meanings of the terms “programmer” and “user”?
Answer: A programmer (also called a developer) is a person who solves problems by writing
programs on a computer. After analyzing the problem and developing a plan for solving it,
the programmer writes and tests the program that instructs the computer how to carry out
the plan. The program might be run many times, either by the programmer or by others.
A user is any person who runs the program. While working through this text, you will
function both as a programmer and as a user.
1.1 An Introduction to Computing and Python ◆ 3
Question: Are there certain characteristics that all programs have in common?
Answer: Most programs do three things: take in data, manipulate data, and produce results.
These operations are referred to as input, processing, and output. The input data might be
held in the program, reside on a disk, or be provided by the user in response to requests
made by the computer while the program is running. The processing of the input data
occurs inside the computer and can take from a fraction of a second to many hours. The
output data are displayed on a monitor, printed on a printer, or recorded on a disk. As a
simple example, consider a program that computes sales tax. An item of input data is the
cost of the thing purchased. The processing consists of multiplying the cost by the sales
tax rate. The output data is the resulting product, the amount of sales tax to be paid.
Question: What are the meanings of the terms “hardware” and “software”?
Answer: Hardware refers to the physical components of the computer, including all periph-
erals, the central processing unit (CPU), disk drives, and all mechanical and electrical
devices. Programs are referred to as software.
Question: Many programming languages, including Python, use a zero-based numbering system.
What is a zero-based numbering system?
Answer: In a zero-based numbering system, numbering begins with zero instead of one. For
example, in the word “code”, “c” would be the zeroth letter, “o” would be the first letter,
and so on.
presses the Enter (or return) key, the names of the presidents who have that first name are
displayed.
Question: How can the programs for the examples in this textbook be obtained?
Answer: See the preface for information on how to download the programs from the
Pearson website.
is used to process the distance traveled and the time elapsed in order to determine the
speed. That is,
1.2 Program Development Cycle ◆ 5
We determine what we want as output, get the needed input, and process the input to
produce the desired output.
In the chapters that follow, we discuss how to write programs to carry out the preceding
operations. But first we look at the general process of writing programs.
■■ Program Planning
A baking recipe provides a good example of a plan. The ingredients and the amounts are
determined by what is to be baked. That is, the output determines the input and the process-
ing. The recipe, or plan, reduces the number of mistakes you might make if you tried to
bake with no plan at all. Although it’s difficult to imagine an architect building a bridge or
a factory without a detailed plan, many programmers (particularly students in their first
programming course) try to write programs without first making a careful plan. The more
complicated the problem, the more complex the plan must be. You will spend much less
time working on a program if you devise a carefully thought out step-by-step plan and test
it before actually writing the program.
Many programmers plan their programs using a sequence of steps, referred to as the
Software Development Life Cycle. The following s tep-by-step process will enable you to use
your time efficiently and help you design e rror-free programs that produce the desired output.
typed, Python points out certain kinds of program errors. Other kinds of errors are
detected by Python when the program is executed—however, many errors due to typ-
ing mistakes, flaws in the algorithm, or incorrect use of the Python language rules, can
be uncovered and corrected only by careful detective work. An example of such an
error would be using addition when multiplication was the proper operation.
5. Complete the documentation: Organize all the material that describes the program.
Documentation is intended to allow another person, or the programmer at a later
date, to understand the program. Internal documentation (comments) consists of
statements in the program that are not executed, but point out the purposes of vari-
ous parts of the program. Documentation might also consist of a detailed descrip-
tion of what the program does and how to use it (for instance, what type of input is
expected). For commercial programs, documentation includes an instruction manual
and on-line help. Other types of documentation are the flowchart, pseudocode, and
hierarchy chart that were used to construct the program. Although documentation
is listed as the last step in the program development cycle, it should take place as the
program is being coded.
The preceding algorithm takes the number of sheets (Sheets) as input, processes the
data, and produces the number of stamps needed (Stamps) as output. We can test the algo-
rithm for a letter with 16 sheets of paper.
Of the program design tools available, three popular ones are the following:
Flowcharts: Graphically depict the logical steps to carry out a task and show how the
steps relate to each other.
Pseudocode: Uses English-like phrases with some Python terms to outline the task.
Hierarchy charts: Show how the different parts of a program relate to each other.
■■ Flowcharts
A flowchart consists of special geometric symbols connected by arrows. Within each sym-
bol is a phrase presenting the activity at that step. The shape of the symbol indicates the type
of operation that is to occur. For instance, the parallelogram denotes input or output. The
arrows connecting the symbols, called flowlines, show the progression in which the steps
take place. Flowcharts should “flow” from the top of the page to the bottom. Although the
symbols used in flowcharts are standardized, no standards exist for the amount of detail
required within each symbol.
+PRWV1WVRWV 7UGFHQTKPRWVCPFQWVRWVQRGTCVKQPU
6JGFCVCVQDGKPRWVQTQWVRWVKUFGUETKDGF
KPVJGRCTCNNGNQITCO
2TQEGUUKPI 7UGFHQTCTKVJOGVKECPFFCVCOCPKRWNCVKQP
QRGTCVKQPU6JGKPUVTWEVKQPUCTGNKUVGF
KPUKFGVJGTGEVCPING
&GEKUKQP 7UGFHQTCP[NQIKEQTEQORCTKUQPQRGTCVKQPU
7PNKMGVJGKPRWVQWVRWVCPFRTQEGUUKPI
U[ODQNUYJKEJJCXGQPGGPVT[CPFQPGGZKV
ƃQYNKPGVJGFGEKUKQPU[ODQNJCUQPGGPVT[
CPFVYQGZKVRCVJU6JGRCVJEJQUGPFGRGPFU
QPYJGVJGTVJGCPUYGTVQCSWGUVKQPKUp[GUq
QTpPQq
%QPPGEVQT 7UGFVQLQKPFKHHGTGPVƃQYNKPGU
#PPQVCVKQP 7UGFVQRTQXKFGCFFKVKQPCNKPHQTOCVKQP
CDQWVCPQVJGTƃQYEJCTVU[ODQN
The table of the flowchart symbols has been adopted by the American National Stand-
ards Institute (ANSI). Figure 1.4 shows the flowchart for the postage-stamp problem.
The main advantage of using a flowchart to plan a task is that it provides a graphical
representation of the task, thereby making the logic easier to follow. We can clearly see
every step and how each is connected to the next. The major disadvantage is that when a
program is very large, the flowcharts may continue for many pages, making them difficult
to follow and modify.
■■ Pseudocode
Pseudocode is an abbreviated plain English version of actual computer code (hence, pseu-
docode). The geometric symbols used in flowcharts are replaced by E nglish-like statements
that outline the process. As a result, pseudocode looks more like computer code than does
8 ◆ Chapter 1 An Introduction to Computing and Problem Solving
5VCTV
1DVCKP
KPRWV
5JGGVU
5GV5VCORU=
RTQEGUUKPI
5JGGVU
4QWPF5VCORU
WRVQCYJQNG RTQEGUUKPI
PWODGT
&KURNC[
QWVRWV
5VCORU
'PF
a flowchart. Pseudocode allows the programmer to focus on the steps required to solve a
problem rather than on how to use the computer language. The programmer can describe
the algorithm in P
ython-like form without being restricted by the rules of Python. When
the pseudocode is completed, it can be easily translated into the Python language.
The pseudocode for the postage-stamp problem is shown in Fig. 1.5.
Pseudocode has several advantages. It is compact and probably will not extend for many
pages as flowcharts commonly do. Also, the pseudocode looks like the code to be written
and so is preferred by many programmers.
■■ Hierarchy Chart
The last programming tool we’ll discuss is the hierarchy chart, which shows the overall
program structure. Hierarchy charts are also called structure charts, HIPO (Hierarchy plus
Input-Process-Output) charts, top-down charts, or VTOC (Visual Table of Contents) charts.
All these names refer to planning diagrams that are similar to a company’s organization chart.
Other documents randomly have
different content
brother’s best wishes could give; and when he rejoined his regiment,
he carried with him a hope which brightened his darkest hours, and
made every hardship seem light.
Young Fenton had been at home but a few months before he
became a universal favorite, as we discovered the acquisition we had
made in his society. He was ardent, impulsive, and generous even to
a fault, and possessed the best traits of Southern character, with an
ingenuous frankness peculiarly his own. Often led astray by the
warmth of his feelings, he was quick to perceive and retract his
errors, and eager to make reparation for them. It was impossible to
associate with Stanwood Fenton without being constantly reminded
of the Saviour’s remark to the young man who came to him, and
who awoke so deep an interest in his benevolent heart: “One thing
thou lackest.” Deep religious principle was the one thing wanting in
his character, the balance-wheel without which his movements were
erratic and uncertain, guided rather by the impulse of the moment
than by any sense of accountability to God.
His original plan had been to enter the Union army as soon as
possible after the death of his mother, but his health had suffered
greatly from long confinement in the hospital; and the loss of his
hand, together with the entreaties of his father and sister, induced
him to relinquish the idea, and devote himself to the care of his
father’s business, which had suffered from neglect. The light came
back to Elinor’s eye, and the bloom to her cheek, as she saw her
brother once more in his proper place at home; and though her
beloved mother was still fondly remembered and her loss deplored,
it was with a chastened sorrow, as she felt that for her to die had
been great gain.
Winter, with its storms and sunshine, its triumphs and reverses,
wore away at length, and with the first blossoms of May Lilian came
back to us, more welcome to our hearts than the breath of spring or
the fragrance of forest flowers. She was accompanied by Captain,
now Major May, who was on the staff of Gen. Lester, and who
eagerly availed himself of an opportunity to revisit the spot where
his earthly hopes were centred. During her stay at the head-quarters
of her husband, Lilian had learned to appreciate the worth of the
young aide-de-camp, and ardently hoped he might be successful in
his suit. Why should I narrate the progress of events? It was the
same old story repeated once again, a tale as old as the history of
the race, yet new in the experience of every human heart—the story
of faithful love meeting its reward at last in the affection of the
beloved object. As Elinor heard from her cousin the recital of the
gallant exploits of Major May, of his courage and devoted loyalty, she
loved him not only “for the perils he had passed,” but for the high
principle which had thus far shielded him from the peculiar
temptations of a soldier’s life; and before his departure he won from
her a promise, that when the war was ended, or his term of service
expired, he might claim his reward.
Months have passed, and still the cloud of war overshadows the
land, and still our beloved ones are absent from us, some with the
heroic Sherman in Northern Georgia, scaling the heights of
Kennesaw and Lookout mountains, and driving the eagle from his
eyrie, as they plant the stars and stripes among the clouds; some
are in the sultry swamps and bayous of Louisiana, exposed to a foe
more insidious and deadly than the rebel armies; and others, among
whom are our best and bravest, are swelling the ranks which
threaten the Confederate capital.
“The time has come when brothers must fight
And sisters must pray at home.”
But while we look up to Him who alone can send help and
deliverance, it is our privilege to labor as well as to pray, and while
we wait upon God, to watch for every opportunity of doing whatever
our hands find to do in the good cause, with our whole heart.
The heavens are dark above us, and the earth rocks wildly under our
feet, but God has a divine purpose underlying all these convulsions,
and it is fixed and immutable as his throne. Faith sees in the
overturnings around us the majestic march of his providence,
preparing a way in the tempest, and making the wrath of man to
praise him, while he restrains the remainder thereof.
It is good for us sometimes, when hope deferred makes the heart
sick, to go back to first principles, that we may gather strength from
a review of our past history and of God’s dealings with us as a
nation.
The American republic was unique in its inception and
establishment. The pilgrim band who came to New England in the
Mayflower were not a company of commercial adventurers, led
hither by the hope of gain; still less were they a party of military
freebooters, actuated by the lust of conquest, like the Spaniards,
who carried fire and sword among the unoffending inhabitants of
Southern America. The principle which led those noble men and
women to forsake kindred and home, and to brave the perils of a
howling wilderness, and which sustained them amid all their
privations and sufferings, was not earthly or perishable. It was the
burning, quenchless thirst for religious liberty, the strong
determination to worship God according to the dictates of their own
conscience, though the roof of their temple were the boundless sky
and their altar the rough stones of the forest, that actuated the
founders of this republic in their sublime enterprise.
They came to these wilds of nature that they might found a colony
and build up a church, and advance the interests of the Redeemer’s
kingdom, and serve as stepping-stones to others in the great work
of human progress. And never has the divine declaration, “Them
that honor me I will honor,” been more signally fulfilled than in the
growth of the infant nation thus established. Every step of the way
in which, as a people, we have been led, from the landing on
Plymouth rock to the proud position which we have hitherto
occupied among the nations, has been marked by special
interpositions of Providence, no less real, though less miraculous,
than the pillar of cloud and flame which guided the ancient Israelites
to the land of promise.
But in our prosperity we have forgotten the Rock whence we were
hewn, and have rebelled against our father’s God, and refused to
obey his commands, until in his righteous indignation he has come
out in judgment against us, and left us to our own ways and to eat
the fruit of our own devices. As a nation we have deeply sinned. As
a nation we are suffering a fearful punishment.
But let not the enemies of liberty in the old world or the new, exult
in the belief that the republic is about to be rent in fragments, and
the last hope of the oppressed millions of Europe to be for ever
entombed. We are bearing the indignation of the Lord because we
have sinned against him; but when his purposes concerning us are
accomplished, he will arise to execute judgment for us.
No careful observer of God’s providence can doubt that he has
reserved America for a grand destiny—that our country has a
mission to perform of the sublimest import, and a grand agency to
exert in the regeneration of the world.
If we read aright the divine purpose in reference to this nation, and
the historical causes here concentrating, we must believe that the
vital forces inherent in our government and institutions will, with the
blessing of God, master all the antagonisms now threatening their
overthrow; and that, having passed through this baptism of blood
and fire, we shall emerge cleansed and purified, and stand as a
beacon light to the struggling nations of the old world, until suffering
humanity everywhere, regenerated and redeemed, shall rejoice in
one grand jubilee of liberty, Christianity, and universal brotherhood.
Beautiful Books
FOR CHILDREN AND YOUTH.
AMERICAN TRACT SOCIETY, 150 NASSAU-STREET,
NEW YORK.
Flowers of Spring-time. Combining
amusement and instruction in most
attractive forms. One hundred and fifty
Engravings. Quarto size.
Home Scenes. An elegant small quarto for
the family, with fourteen photographic
pictures, fac-similes of fine Engravings.
Views from Nature. Forty scenes in nature
and art Finely printed in tint.
Songs for the Little Ones at Home.
Attractive as ever. Beautifully illustrated.
Lullabies, Ditties, and Tales. Original
short Poems for the Children, Containing
Tales, Songs, and Dialogues. With eighty-
four Engravings.
Home Pictures. 72 pages. A fine Cut on
each page.
My Picture-book. 64 pages. Sixty-one
Engravings.
Fireside Pictures. 64 pages. With a Cut on
each page.
The Illustrated Tract Primer. The
Children’s favorite. Finely Illustrated.
FOR SALE AT 150 NASSAU-
STREET, NEW YORK; 40
Cornhill, BOSTON; 1210
Chestnut-street,
PHILADELPHIA; 75 State-
street, ROCHESTER; 163
Walnut-street, CINCINNATI;
and in other cities and
principal towns.
TRANSCRIBER’S NOTES:
Obvious typographical errors have been corrected.
Inconsistencies in hyphenation have been
standardized.
Archaic or alternate spelling has been retained from
the original.
The cover image for this eBook was created by the
transcriber and is entered into the public domain.
*** END OF THE PROJECT GUTENBERG EBOOK OUR VILLAGE IN
WAR-TIME ***
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside
the United States, check the laws of your country in addition to
the terms of this agreement before downloading, copying,
displaying, performing, distributing or creating derivative works
based on this work or any other Project Gutenberg™ work. The
Foundation makes no representations concerning the copyright
status of any work in any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must,
at no additional cost, fee or expense to the user, provide a copy,
a means of exporting a copy, or a means of obtaining a copy
upon request, of the work in its original “Plain Vanilla ASCII” or
other form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
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.
ebookluna.com