100% found this document useful (2 votes)
121 views

Instant Access to (eBook PDF) Building Python Programs 1st Edition ebook Full Chapters

Python

Uploaded by

najjilust
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
121 views

Instant Access to (eBook PDF) Building Python Programs 1st Edition ebook Full Chapters

Python

Uploaded by

najjilust
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Download the full version of the ebook now at ebooksecure.

com

(eBook PDF) Building Python Programs 1st


Edition

https://ebooksecure.com/product/ebook-pdf-
building-python-programs-1st-edition/

Explore and download more ebook at https://ebooksecure.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

(eBook PDF) Fundamentals of Python: First Programs 2nd


Edition

https://ebooksecure.com/product/ebook-pdf-fundamentals-of-python-
first-programs-2nd-edition/

ebooksecure.com

Fundamentals of Python: First Programs, 2nd Edition


Kenneth A. Lambert - eBook PDF

https://ebooksecure.com/download/fundamentals-of-python-first-
programs-2nd-edition-ebook-pdf/

ebooksecure.com

(eBook PDF) Building Java Programs: A Back to Basics


Approach 5th Edition

https://ebooksecure.com/product/ebook-pdf-building-java-programs-a-
back-to-basics-approach-5th-edition/

ebooksecure.com

(eBook PDF) Cross-Cultural Management Essential Concepts


4th Edition

https://ebooksecure.com/product/ebook-pdf-cross-cultural-management-
essential-concepts-4th-edition/

ebooksecure.com
(eBook PDF) Elasticity: Theory, Applications, and Numerics
4th Edition

https://ebooksecure.com/product/ebook-pdf-elasticity-theory-
applications-and-numerics-4th-edition/

ebooksecure.com

(Original PDF) Politics UK 8th Edition

https://ebooksecure.com/product/original-pdf-politics-uk-8th-edition/

ebooksecure.com

(eBook PDF) Fundamentals of Physics, 11th Edition by David


Halliday

https://ebooksecure.com/product/ebook-pdf-fundamentals-of-
physics-11th-edition-by-david-halliday/

ebooksecure.com

Symbolic Logic Syntax, Semantics, and Proof

https://ebooksecure.com/product/symbolic-logic-syntax-semantics-and-
proof/

ebooksecure.com

Gendered Lives: Communication, Gender, & Culture Twelfth


Edition Julia T. Wood - eBook PDF

https://ebooksecure.com/download/gendered-lives-communication-gender-
culture-ebook-pdf/

ebooksecure.com
(eBook PDF) Examples & Explanations for Criminal
Procedure: The Constitution and the Police 9th Edition

https://ebooksecure.com/product/ebook-pdf-examples-explanations-for-
criminal-procedure-the-constitution-and-the-police-9th-edition/

ebooksecure.com
Preface

The Python programming language has become enormously popular in recent years.
Many people are impressed with how quickly you can learn Python’s simple and intui-
tive syntax and that has led many users to create popular libraries. Python was designed
by Guido van Rossum who has been affectionaly dubbed “Benevolent Dictator For Life
(BDFL)” by the Python community. He has said that he chose the name Python because
he was “in a slightly irreverent mood” and that he is “a big fan of Monty Python’s
Flying Circus” (a British comedy show). Who wouldn’t want to learn a programming
language named after a group of comedians?
Our new Building Python Programs text is designed for use in a first course in com-
puter science. We have class-tested it with hundreds of undergraduates at the University
of Arizona, most of whom were not computer science majors. This textbook is based
on our previous text, Building Java Programs, now in its fourth edition. The Java text
has proven effective in our class testing with thousands of students including our own
at the University of Washington since 2007.
Introductory computer science courses have a long history at many universities
of being “killer” courses with high failure rates. But as Douglas Adams says in The
Hitchhiker’s Guide to the Galaxy, “Don’t panic.” Students can master this material if
they can learn it gradually.
Python has many attributes that make it an appealing language for a first computer
science course. It has a simple and concise yet powerful syntax that makes it pleasant
to learn and great for writing many common programs. A student can write their first
Python program with only a single line of code, as opposed to several lines in most other
languages such as Java or C++. Python includes a built-in interpreter and read-­evaluate-
print loop (REPL) for quickly running and testing code, encouraging students to test and
explore the language. Python also offers a rich set of libraries that students can use for
graphics, animation, math, scientific computing, games, and much more. This text has
been built from the start for Python 3, the most modern version of the language as of this
writing, and it embraces the modern features and idioms of that version of the language.
Our teaching materials are based on a “back to basics” approach that focuses on
procedural programming and program decomposition. This is also called the “objects
later” approach, as opposed to the “objects early” approach taught in some schools. We
know from years of experience that a broad range of scientists, engineers, and others
can learn how to program in a procedural manner. Once we have built a solid founda-
tion of procedural techniques, we turn to object-oriented programming. By the end of
the text, students will have learned about both styles of programming.
iii
Brief Contents

Chapter 1 Introduction to Python Programming 1


Chapter 2 Data and Definite Loops 57
Chapter 3 Parameters and Graphics 132
Chapter 4 Conditional Execution 219
Chapter 5 Program Logic and Indefinite Loops 295
Chapter 6 File Processing 364
Chapter 7 Lists 418
Chapter 8 Dictionaries and Sets 517
Chapter 9 Recursion 563
Chapter 10 Searching and Sorting 636
Chapter 11 Classes and Objects 686
Chapter 12 Functional Programming 738
Appendix A Python Summary 785

xi
Contents

Chapter 1 Introduction to Python Programming 1


1.1 Basic Computing Concepts 2
Why Programming? 2
Hardware and Software 3
The Digital Realm 4
The Process of Programming 6
Why Python? 7
The Python Programming Environment 8
1.2 And Now: Python 10
Printing Output 14
String Literals (Strings) 15
Escape Sequences 16
Printing a Complex Figure 18
Comments, Whitespace, and Readability 19
1.3 Program Errors 22
Syntax Errors 23
Logic Errors (Bugs) 25
1.4 Procedural Decomposition 26
Functions 27
Flow of Control 31
Identifiers and Keywords 34
Functions That Call Other Functions 36
An Example Runtime Error 38
1.5 Case Study: Drawing Figures 40
Structured Version 41
Final Version without Redundancy 42
Analysis of Flow of Execution 44

Chapter 2 Data and Definite Loops 57


2.1 Basic Data Concepts 58
Types 58
Expressions 59
Literals 62
xiii
xiv Contents 

Arithmetic Operators 62
Precedence 66
Mixing and Converting Types 69
2.2 Variables 70
A Program with Variables 74
Increment/Decrement Operators 79
Printing Multiple Values 80
2.3 The for Loop 83
Using a Loop Variable 87
Details about Ranges 90
String Multiplication and Printing Partial Lines 94
Nested for Loops 98
2.4 Managing Complexity 101
Scope 101
Pseudocode 103
Constants 108
2.5 Case Study: Hourglass Figure 111
Problem Decomposition and Pseudocode 112
Initial Structured Version 114
Adding a Constant 115

Chapter 3 Parameters and Graphics 132


3.1 Parameters 133
The Mechanics of Parameters 139
Limitations of Parameters 141
Multiple Parameters 145
Parameters versus Constants 148
Optional Parameters 149
3.2 Returning Values 151
The math Module 153
The random Module 156
Defining Functions That Return Values 160
Returning Multiple Values 165
3.3 Interactive Programs 167
Sample Interactive Program 170
3.4 Graphics 172
Introduction to DrawingPanel 173
Drawing Lines and Shapes 176
Colors 179
Drawing with Loops 183
Text and Fonts 186
Contents  xv

Images 188
Procedural Decomposition with Graphics 189
3.5 Case Study: Projectile Trajectory 191
Unstructured Solution 195
Structured Solution 196
Graphical Version 199

Chapter 4 Conditional Execution 219


4.1 if/else Statements 220
Relational Operators 222
Nested if/else Statements 225
Factoring if/else Statements 231
Testing Multiple Conditions 232
4.2 Cumulative Algorithms 233
Cumulative Sum 233
Min/Max Loops 236
Cumulative Sum with if 239
Roundoff Errors 242
4.3 Functions with Conditional Execution 245
Preconditions and Postconditions 245
Raising Exceptions 246
Revisiting Return Values 250
Reasoning about Paths 253
4.4 Strings 255
String Methods 257
Accessing Characters by Index 260
Converting between Letters and Numbers 264
Cumulative Text Algorithms 267
4.5 Case Study: Basal Metabolic Rate 269
One-Person Unstructured Solution 270
Two-Person Unstructured Solution 273
Two-Person Structured Solution 275
Procedural Design Heuristics 280

Chapter 5 Program Logic and Indefinite Loops 295


5.1 The while Loop 296
A Loop to Find the Smallest Divisor 298
Loop Priming 300
xvi Contents 

5.2 Fencepost Algorithms 303


Fencepost with if 306
Sentinel Loops 308
Sentinel with Min/Max 310
5.3 Boolean Logic 312
Logical Operators 315
Boolean Variables and Flags 318
Predicate Functions 320
Boolean Zen 322
Short-Circuited Evaluation 325
5.4 Robust Programs 329
The try/except Statement 330
Handling User Errors 333
5.5 Assertions and Program Logic 335
Reasoning about Assertions 337
A Detailed Assertions Example 339
5.6 Case Study: Number Guessing Game 343
Initial Version without Hinting 344
Randomized Version with Hinting 346
Final Robust Version 348

Chapter 6 File Processing 364


6.1 File-Reading Basics 365
Data and Files 365
Reading a File in Python 369
Line-Based File Processing 372
Structure of Files and Consuming Input 373
Prompting for a File 378
6.2 Token-Based Processing 381
Numeric Input 383
Handling Invalid Input 385
Mixing Lines and Tokens 386
Handling Varying Numbers of Tokens 388
Complex Input Files 392
6.3 Advanced File Processing 394
Multi-Line Input Records 395
File Output 397
Reading Data from the Web 400
6.4 Case Study: ZIP Code Lookup 403
Contents  xvii

Chapter 7 Lists 418


7.1 List Basics 419
Creating Lists 420
Accessing List Elements 423
Traversing a List 429
A Complete List Program 430
Random Access 434
List Methods 435
7.2 List-Traversal Algorithms 443
Lists as Parameters 443
Searching a List 445
Replacing and Removing Values 449
Reversing a List 450
Shifting Values in a List 456
Nested Loop Algorithms 462
List Comprehensions 463
7.3 Reference Semantics 464
Values and References 465
Modifying a List Parameter 468
The Value None 470
Mutability 472
Tuples 476
7.4 Multidimensional Lists 482
Rectangular Lists 483
Jagged Lists 485
Lists of Pixels 491
7.5 Case Study: Benford’s Law 495
Tallying Values 497
Completing the Program 501

Chapter 8 Dictionaries and Sets 517


8.1 Dictionary Basics 518
Creating a Dictionary 521
Dictionary Operations 524
Looping Over a Dictionary 527
Dictionary Ordering 528
8.2 Advanced Dictionary Usage 531
Dictionary for Tallying 531
xviii Contents 

Nested Collections 536


Dictionary Comprehensions 541
8.3 Sets 543
Set Basics 543
Set Operations 547
Set Efficiency 549
Set Example: Lottery 552

Chapter 9 Recursion 563


9.1 Thinking Recursively 564
A Nonprogramming Example 564
Iteration to Recursion 567
Structure of Recursive Solutions 570
Reversing a File 573
The Recursive Call Stack 575
9.2 Recursive Functions and Data 581
Integer Exponentiation 581
Greatest Common Divisor 584
Directory Crawler 590
9.3 Recursive Graphics 594
Cantor Set 594
Sierpinski Triangle 597
9.4 Recursive Backtracking 601
Traveling North/East 601
Eight Queens Puzzle 607
Stopping after One Solution 615
9.5 Case Study: Prefix Evaluator 618
Infix, Prefix, and Postfix Notation 618
Evaluating Prefix Expressions 619
Complete Program 623

Chapter 10 Searching and Sorting 636


10.1 Searching and Sorting Libraries 637
Binary Search 638
Sorting 644
Shuffling 645
10.2 Program Complexity 646
Empirical Analysis 649
Complexity Classes 656
Contents  xix

10.3 Implementing Searching and Sorting Algorithms 657


Sequential Search 658
Binary Search 659
Recursive Binary Search 662
Selection Sort 664
10.4 Case Study: Implementing Merge Sort 667
Splitting and Merging lists 668
Recursive Merge Sort 671
Runtime Performance 674
Hybrid Approach 677

Chapter 11 Classes and Objects 686


11.1 Object-Oriented Programming 687
Classes and Objects 688
Date Objects 690
11.2 Object State and Behavior 690
Data Attributes 691
Initializers 694
Methods 698
Accessors and Mutators 702
Making Objects Printable 705
Object Equality and Ordering 707
11.3 Encapsulation 710
Motivation for Encapsulation 711
Private Attributes and Properties 711
Class Invariants 717
11.4 Case Study: Designing a Stock Class 721
Object-Oriented Design Heuristics 723
Stock Attributes and Method Headers 725
Stock Method and Property Implementation 727

Chapter 12 Functional Programming 738


12.1 Functional Programming Concepts 739
Side Effects 740
First-Class Functions 742
Higher-Order Functions 744
Lambda Expressions 746
12.2 Functional Operations on Collections 749
Using Map 751
Using Filter 752
Visit https://testbankfan.com
now to explore a rich
collection of testbank or
solution manual and enjoy
exciting offers!
xx Contents 

Using Reduce 754


List Comprehensions 758
12.3 Function Closures 760
Generator Functions 763
Lazy Evaluation 767
Iterable Objects 769
Generator Expressions 770
12.4 Case Study: Perfect Numbers 772
Computing Sums 772
The Fifth Perfect Number 777
Leveraging Concurrency 778

Appendix A Python Summary 785


Index 798
Chapter 1
Introduction to
Python Programming

Introduction
1.1 Basic Computing Concepts
■■ Why Programming?
This chapter begins with a review of some basic terminology about comput- ■■ Hardware and Software
ers and computer programming. Many of these concepts will come up in later ■■ The Digital Realm
chapters, so it will be useful to review them before we start delving into the
■■ The Process of Programming
■■ Why Python?
details of how to program in Python. ■■ The Python Programming
Environment
We will begin our exploration of Python by looking at simple programs
1.2 And Now: Python
that produce text output to a console window. This discussion will allow us
■■ Printing Output
to explore many elements that are common to all Python programs, while ■■ String Literals (Strings)
working with programs that are fairly simple in structure. ■■ Escape Sequences
■■ Printing a Complex Figure
After we have reviewed the basic elements of Python programs, we will ■■ Comments, Whitespace, and
explore the technique of procedural decomposition by learning how to break Readability

up a Python program into several functions. Using this technique, we can 1.3 Program Errors
break up complex tasks into smaller subtasks that are easier to manage and ■■ Syntax Errors
■■ Logic Errors (Bugs)
we can avoid redundancy in our program solutions.
1.4 Procedural Decomposition
■■ Functions
■■ Flow of Control
■■ Identifiers and Keywords
■■ Functions That Call Other
Functions
■■ An Example Runtime Error

1.5 Case Study: Drawing Figures


■■ Structured Version
■■ Final Version without
Redundancy
■■ Analysis of Flow of Execution

Chapter Summary

Self-Check Problems

Exercises

Programming Projects

1
2 Chapter 1 Introduction to Python Programming

1.1 Basic Computing Concepts


Computers are pervasive in our daily lives, and, thanks to the Internet, they give us
access to nearly limitless information. Some of this information is essential news, like
the headlines at cnn.com. Computers let us share photos with our families and map
directions to the nearest pizza place for dinner.
Lots of real-world problems are being solved by computers, some of which don’t
much resemble the one on your desk or lap. Computers allow us to sequence the human
genome and search for DNA patterns within it. Computers in recently manufactured
cars monitor each vehicle’s status and motion. Digital devices such as Apple’s iPhone
actually have computers inside their small casings. Even the Roomba vacuum-cleaning
robot houses a computer with complex instructions about how to dodge furniture while
cleaning your floors.
But what makes a computer a computer? Is a calculator a computer? Is a human
being with a paper and pencil a computer? The next several sections attempt to address
this question while introducing some basic terminology that will help prepare you to
study programming.

Why Programming?
At most universities, the first course in computer science is a programming course.
Many computer scientists are bothered by this because it leaves people with the impres-
sion that computer science is programming. While it is true that many trained computer
scientists spend time programming, there is a lot more to the discipline. So why do we
study programming first?
A Stanford computer scientist named Don Knuth answers this question by saying
that the common thread for most computer scientists is that we all in some way work
with algorithms.

Algorithm
A step-by-step description of how to accomplish a task.

Knuth is an expert in algorithms, so he is naturally biased toward thinking of them as


the center of computer science. Still, he claims that what is most important is not the
algorithms themselves, but rather the thought process that computer scientists employ
to develop them. According to Knuth:

It has often been said that a person does not really understand something until
after teaching it to someone else. Actually a person does not really understand
something until after teaching it to a computer, i.e., expressing it as an
algorithm.1

1
Knuth, Don. Selected Papers on Computer Science. Stanford. CA: Center for the Study of Language and
Information, 1996.
1.1 Basic Computing Concepts 3

Knuth is describing a thought process that is common to most of computer science,


which he refers to as algorithmic thinking. We study programming not because it is the
most important aspect of computer science, but because it is the best way to explain the
approach that computer scientists take to solving problems.
The concept of algorithms is helpful in understanding what a computer is and what
computer science is all about. The Merriam-Webster dictionary defines the word “com-
puter” as “one that computes.” Using that definition, all sorts of devices qualify as
computers, from calculators to GPS navigation systems to children’s toys. Prior to the
invention of electronic computers, it was common to refer to humans as computers. The
nineteenth-century mathematician Charles Peirce, for example, was originally hired
to work for the U.S. government as an “Assistant Computer” because his job involved
performing mathematical computations.
In a broad sense, then, the word “computer” can be applied to many devices. But
when computer scientists refer to a computer, they are usually thinking of a universal
computation device that can be programmed to execute any algorithm. Computer sci-
ence, then, is the study of computational devices and the study of computation itself,
including algorithms.
Algorithms are expressed as computer programs, and that is what this book is all
about. But before we look at how to program, it will be useful to review some basic
concepts about computers.

Hardware and Software


A computer is a machine that manipulates data and executes lists of instructions known
as programs.

Program
A list of instructions to be carried out by a computer.

One key feature that differentiates a computer from a simpler machine like a calculator
is its versatility. The same computer can perform many different tasks (playing games,
computing income taxes, connecting to other computers around the world), depending
on what program it is running at a given moment. A computer can run not only the pro-
grams that exist on it currently, but also new programs that haven’t even been written yet.
The physical components that make up a computer are collectively called hardware.
One of the most important pieces of hardware is the central processing unit, or CPU.
The CPU is the “brain” of the computer: It is what executes the instructions. Also
important is the computer’s memory (often called random access memory, or RAM,
because the computer can access any part of that memory at any time). The computer
uses its memory to store programs that are being executed, along with their data. RAM
is limited in size and does not retain its contents when the computer is turned off.
Therefore, computers generally also use a hard disk as a larger permanent storage area.
Computer programs are collectively called software. The primary piece of soft-
ware running on a computer is its operating system. An operating system provides an
environment in which many programs may be run at the same time; it also provides
4 Chapter 1 Introduction to Python Programming

a bridge among those programs, the hardware, and the user (the person using the
­computer). The programs that run inside the operating system are often called
­applications or apps.
When the user selects a program for the operating system to run (e.g., by double-
clicking the program’s icon on the desktop), several things happen: The instructions for
that program are loaded into the computer’s memory from the hard disk, the operating
system allocates memory for that program to use, and the instructions to run the pro-
gram are fed from memory to the CPU and executed sequentially.

The Digital Realm


In the previous section, we saw that a computer is a general-purpose device that can be
programmed. You will often hear people refer to modern computers as digital comput-
ers because of the way they operate.

Digital
Based on numbers that increase in discrete increments, such as the integers
0, 1, 2, 3, etc.

Because computers are digital, everything that is stored on a computer is stored as a


sequence of integers. This includes every program and every piece of data. An MP3
file, for example, is simply a long sequence of integers that stores audio information.
Today we’re used to digital music, digital pictures, and digital movies, but in the 1940s,
when the first computers were built, the idea of storing complex data in integer form
was fairly unusual.
Not only are computers digital, storing all information as integers, but they are also
binary, which means they store integers as binary numbers.

Binary Number
A number composed of just 0s and 1s, also known as a base-2 number.

Humans generally work with decimal or base-10 numbers, which match our physiol-
ogy (10 fingers and 10 toes). However, when we were designing the first computers,
we wanted systems that would be easy to create and very reliable. It turned out to be
simpler to build these systems on top of binary phenomena (e.g., a circuit being open or
closed) rather than having 10 different states that would have to be distinguished from
one another (e.g., 10 different voltage levels).
From a mathematical point of view, you can store things just as easily using binary
numbers as you can using base-10 numbers. But since it is easier to construct a physical
device that uses binary numbers, that’s what computers use.
This does mean, however, that people who aren’t used to computers find their
conventions unfamiliar. As a result, it is worth spending a little time reviewing how
Exploring the Variety of Random
Documents with Different Content
little time to spend in wishing, though every night he went out for a
good look at the mountains. But he was beginning to think about the
kind of man that he would like to be; and every day he was a little
more sure that he wanted to be like the young superintendent.
He was so short himself that he was afraid that he would never be
as tall as Mr. Prescott. So he began to stand as tall as he could,
especially when he was in the office. Then he tried to remember to
breathe deep, the way that the teacher at school had told the boys to
do. But he wondered, sometimes, when he looked at Mr. Prescott’s
broad shoulders, whether he had ever been as small as most boys.
The day that Billy had his first little brown envelope with three
dollars and fifty cents in it, he stood very tall indeed. That night, at
supper, he handed it to Aunt Mary, saying:
“That’s for you to put in the bank.”
“For Billy,” said Uncle John, looking up quickly and speaking
almost sternly. “I’m the one to give Aunt Mary money.”
Then he said gently: “It’s a good plan, Billy, to put your first money
in the bank. You’ll never have any more just like that.”
The thing that first excited Billy’s curiosity, as he went about on
errands, was the big pile of old iron in the mill yard. There were
pieces of old stoves, and seats from schoolhouses that had been
burned, and engines that had been smashed in wrecks, and old
ploughs, and nobody knew what else—all piled up in a great heap.
One day, when he carried an order to the man that tended the
furnace in the cupola where they melted the iron, he saw them
putting pieces of old iron on the scales; and he heard the man say to
his helper: “We shall have to put in fifty pounds extra to-day.”
It seemed to Billy that it wasn’t quite fair to put in old iron, when
they were making new machinery. So, one noon, he asked Uncle
John about it.
“Using your eyes, are you, Billy? That’s quite likely to set your
mind to working.
“I suppose you’ve heard them talking around here about testing
machinery. That isn’t the first testing. They test iron all the way
along, from the ore in the mine to the sticks of pig iron piled up in our
yard.”
“Some of it is in cakes,” said Billy.
“Is that so?” asked Uncle John, as he took another sandwich out
of his pail. “Now I think of it, they did tell me that cakes are the new
style in pig iron.
“Well,” continued Uncle John, “there are men testing and
experimenting all the time; and some of them found out that old iron
and pig iron together make better new iron than they can make from
pig iron alone. Since they found that out, scrap iron has kept on
going up in price.
“Did you happen, Billy, to see any other heaps lying around?”
“I saw a pile of coke, over in the corner,” answered Billy.
“Somewhere,” said Uncle John, “there must have been a heap of
limestone. They use that for what they call a flux. That unites with
the waste things, the ashes of the coke and any sand that may have
stuck to the pig iron. Those things together make slag. The slag is so
much lighter than the iron that it floats on top, and there are tap
holes in the cupola where they draw it off. Limestone helps the iron
to melt, that’s another reason why they use it.”
“I saw some scales,” said Billy.
“Those,” said Uncle John, “are to weigh the things that they put
into the cupola. There are rules for making cast iron. It all depends
on what kind of machinery we want to make.
“First, in the bottom of the cupola, they make a fire of shavings
and wood, with a little coal; then they put in coke, pig iron, scrap iron,
and limestone, according to the rule for the kind of iron that they
want to make.
“Those heaps all pieced together, Billy?”
“Sure,” answered Billy; and, then, the whistle blew.
Deep down in his heart, Billy didn’t like that whistle. He didn’t tell
Uncle John, because William Wallace scorned anybody who felt like
that. William Wallace said that being on time—on time to the minute
—was only just business. Nevertheless, Billy missed being free. Aunt
Mary’s errands hadn’t been timed by the clock.
There was another reason why he didn’t tell Uncle John how he
felt.
“Stand by your job, every minute that you belong on it,” was one of
the things that Uncle John had said so many times that it almost
worried Billy.
But, before the summer was over, Billy was glad that he had kept
that on his mind.
CHAPTER III
A MOUNTAIN OF IRON
WHETHER, if it hadn’t been for Billy’s new jack-knife, he and
Thomas Murphy would have become friends, no one can say. It
seems very probable that something would have made them like
each other.
Sitting on a high stool to check time or in a chair to watch the great
door had grown so monotonous that Tom really needed to have
somebody to talk to.
Then there wasn’t any boy in the mill for Billy to get acquainted
with; and Billy saw Tom oftener than he saw any of the other men.
So it seems very natural that Billy and Tom should have become
friends.
If they hadn’t, things wouldn’t have turned out just as they did; and
whatever else might have happened, it was really the jack-knife that
brought them together.
Billy had been in the mill about two weeks when, one morning, just
as Tom was finishing making a mark after Uncle John’s name, snap
went the point of his pencil.
Billy heard it break, and saw Tom put his hand into his pocket. Billy
knew, from Tom’s face, before he drew his hand out, that there
wasn’t any knife in his pocket.
So Billy put his dinner pail down, and pulling his knife out by the
chain, said quickly:
“I’ll sharpen your pencil, Mr. Murphy.”
Billy had been practicing on sharpening pencils. He worked so fast
that the men behind had hardly begun to grumble before the pencil
was in working order, and the line began to move on again.
Though he did not know it, Billy had done something more than
merely to sharpen Tom’s pencil. When he said, “Mr. Murphy,” he
waked up something in Tom that Tom himself had almost forgotten
about.
He had been called “Tom Murphy” so long, sometimes only “lame
Tom,” that Billy’s saying “Mr. Murphy” had made him sit up very
straight, while he was waiting for Billy to sharpen the pencil.
Mr. Prescott thought that he really appreciated Tom. He always
said, “Tom Murphy is as faithful as the day is long”; but even Mr.
Prescott didn’t know so much about Tom as he thought he did. If Billy
and Tom hadn’t become friends, Mr. Prescott would probably never
have learned anything about the “Mr. Murphy” side of Tom.
After that morning, Billy and Tom kept on getting acquainted, until
one day when Uncle John had to go out one noon to see about
some new window screens for Aunt Mary, Billy went to the door to
see Tom.
Tom, having just sat down in his chair, was trying to get his lame
leg into a position where it would be more comfortable.
“Does your leg hurt, Mr. Murphy?” asked Billy.
“Pretty bad to-day, William,” answered Thomas Murphy with a
groan. “If it wasn’t so dry, I should think, from the way my leg aches,
that it was going to rain, but there’s no hope of that.”
“It’s rheumatism, isn’t it?” asked Billy, sympathetically.
“Part of it is,” answered Tom, “but before that it was crush. I hope
you don’t think I’ve never done anything but mark time at Prescott
mill.
“I suppose that you think you’ve seen considerable iron in this yard
and in this mill; but you don’t know half so much about iron as I did
when my legs were as good as yours.
“Out West, where I was born, there are acres and acres and acres
of iron almost on top of the ground; and, besides that, a whole
mountain of iron.”
Tom paused a moment to move his leg again.
“Was there an iron mine in the ground, too?” asked Billy sitting
down on the threshold of the door.
“Yes, there was,” answered Tom. “If I had stayed on top of the
ground, perhaps I shouldn’t have been hurt. Might have been blown
up in a gopher hole, though, the way my brother was.”
“O-oh!” said Billy.
“Never heard of a gopher hole, I suppose,” continued Tom, settling
back in his chair, as though he intended to improve his opportunity to
talk.
“That’s one way that they get iron out of a mountain. They make
holes straight into a bank. Then they put in sacks of powder, and fire
it with a fuse. That loosens the ore so that they can use a steam
shovel. Sometimes the men go in too soon.”
“I wish,” said Billy with a little shiver, “that you would tell me about
the mine.”
“That’ll be quite a contract,” said Thomas Murphy, clasping his
hands across his chest, “but I was in one long enough to know.
“You’ll think there was a mine down in the ground when I tell you
that I’ve been down a thousand feet in one myself.
“I went down that one in a cage; but in the mine where I worked I
used to go down on ladders at the side of the shaft.”
“Was it something like a coal mine?” asked Billy.
“I’ve heard miners say,” answered Tom, “that some iron is so hard
that it has to be worked with a pick and a shovel; but the iron in our
mine was so soft that we caved it down.
“If I had been working with a pick, perhaps I shouldn’t have been
hurt.
“When you cave iron, you go down to the bottom of the shaft and
work under the iron. You cut out a place, and put in some big timbers
to hold up the roof. Then you cut some more, and keep on till you
think the roof may fall.
“Then you board that place in, and knock out some pillars, or blow
them out, and down comes the iron. Then you put it in a car and
push it to a chute, and that loads it on an elevator to be brought up.
Sometimes they use electric trams; we used to have to push the
cars.”
“It must be very hard work,” said Billy.
“Work, William, usually is hard,” said Thomas Murphy. “Work,
underground or above ground, is work, William.”
“But you haven’t told me, Mr. Murphy,” said Billy, “how you hurt
your knee.”
“The quickest way to tell you that, William, is to tell you that the
cave, that time, caved too soon. I got caught on the edge of it.
“After I got out of the hospital, I tried to work above ground; but the
noise of the steam shovels and the blasting wore me out. So, one
day, I took an ore train, and went to the boat and came up the river.
“Finally, I drifted to Prescott mill, some seasons before you were
born, William.”
“Have you ever wanted to go back?” asked Billy.
“No, William, I haven’t. There’s nobody left out there that belongs
to me, anyway. My lame knee wasn’t the only reason why I left,
William. I heard something about the country that I didn’t like at all; I
didn’t like it at all.”
“Weren’t the people good?” asked Billy.
“Very good people,” answered Thomas Murphy firmly. “’Twas
something about the mountain that I heard.
“There were always men around examining the mines. I never
paid much attention to ’em till one day I heard a man—they said he
came from some college—a-talking about volcanoes. He said that
iron mountain was thrown up by a volcano, said he was sure of it.
“I never told anybody, William, but I cleared out the very next day.
You’ve never heard anything about volcanoes round here, have you,
William?”
“No, Mr. Murphy,” answered Billy.
“If you ever should, William——” said Thomas Murphy, leaning
anxiously forward.
“If I ever do hear, Mr. Murphy,” said Billy, feeling that he was
making a promise, “I’ll tell you right away.”
“Thank you, William,” said Tom. “You won’t mention it, will you?”
“No, Mr. Murphy,” answered Billy.
That was really the day when Billy and Thomas Murphy sealed
their compact as friends.
CHAPTER IV
THE FOUNDRY
“MY friend, Mr. Murphy,” said Billy, one night after supper, when he
and Uncle John were sitting side by side on the steps.
“Did I understand?” interrupted Uncle John, “Mr. Murphy?”
“Yes,” answered Billy, “Mr. Thomas Murphy the timekeeper.”
“Exactly,” said Uncle John.
“Mr. Murphy,” Billy went on, “says that iron moves the world.”
“I should say,” said Uncle John, deliberately, “that power generally
has to be put into an iron harness before anything can move; but Mr.
Murphy evidently knows what he is talking about.”
“He says,” continued Billy, “that iron mills are very important
places; and that, for his part, he’s glad that he works in an iron mill.”
“That’s the way every man ought to feel about his work,” said
Uncle John; “all the work in the world has to be done by somebody.”
That remark sounded to Billy as if another motto might be coming;
and, being tired, he wanted just to be social. So he said:
“Uncle John, did you ever see Miss King, the stenographer?”
“Only coming and going,” he answered.
“She’s a friend of mine, too,” said Billy. “She told me, to-day, that
she wants me always to feel that she is my friend.”
“Everything going all right in the office, Billy?” asked Uncle John,
quickly.
“Oh, yes,” answered Billy, with a little note of happiness in his
voice. “She told me that so as to make me feel comfortable. She’s
the loveliest woman I ever saw. Don’t you think, Uncle John, that
yellow-brown is the prettiest color for hair?”
“I do,” said Uncle John, emphatically. Then, rising to go into the
house, he added, “That’s exactly what I used to call Aunt Mary’s hair,
yellow-brown.”
“Oh!” said Billy wonderingly. Then it was time for him to go to bed;
but he lingered a moment to look at Aunt Mary’s hair that was dark
brown, now, where it wasn’t gray. There was something in his “Good-
night, Aunt Mary,” that made her look up from her paper as she said:
“Good-night, William Wallace.”
Anybody can see that William Wallace is a hard name for a boy to
go to bed on. It was so hard for Billy that it almost hurt; but Billy had
lived with Aunt Mary long enough to be sure that she meant to be a
true friend.
Whether or not Mr. Prescott was his friend, Billy did not know. Mr.
Murphy had told him one day when he was out by the door, waiting
for the postman, that Mr. Prescott was a friend to every man in the
mill. Billy supposed that every man was a friend back again. At any
rate he knew that he was; and he hoped that, some day, he would be
able to do something, just to show Mr. Prescott how much he liked
him.
The more he thought about it, the more it didn’t seem possible that
such a hope as that could ever come true.
But anybody who liked anybody else as much as he liked Mr.
Prescott couldn’t help seeing that something bothered him. So Billy
had a little secret with himself to try to look specially pleasant when
Mr. Prescott came in from a trip around the mill. He had begun to
think that Mr. Prescott had given up springing questions on him
when, one very warm afternoon, Mr. Prescott looked up from his
desk and said:
“William, if you were to have an afternoon off, what would you do?”
“I’d rather than anything else in the world,” answered Billy
promptly, “go out into the country.”
“That being hardly feasible,” said Mr. Prescott, “what else would
you rather do?”
“Next to that,” answered Billy, “I’d rather go into the foundry to see
Uncle John work.”
“Well!” exclaimed Mr. Prescott, whirling around in his chair. “That’s
about the last thing that I should have thought of, especially on such
a hot day. May I inquire whether you are interested in iron?”
Billy, with a quick flash of spirit, answered promptly, “I am, sir.”
As promptly Mr. Prescott said, “I’m glad to hear it, William. You
may spend the rest of the afternoon in the foundry.”
“Thank you, sir,” said Billy, very much surprised. Then he looked at
Miss King, and she nodded and smiled.
Billy ran down the corridor, passing Mr. Murphy with a flying salute,
and hurried across the yard to the foundry door.
Just then he remembered that he hadn’t a permit; but the foreman
appeared in the door saying, “The super has telephoned over that
you’re to visit us this afternoon.”
Pointing across the room, he added, “Your uncle is over there.”
Billy wanted to surprise his Uncle John, so he went carefully along
the outer side of the long, low room, past pile after pile of gray black
sand, until he came to the place where Uncle John was bending over
what seemed to be a long bar of sand.
“Uncle John,” he said softly.
“Why, Billy, my lad!” exclaimed he, looking up with so much
surprise in his face that Billy said quickly:
“It’s all right, Uncle John. Mr. Prescott sent me to watch you work.”
“Things,” said Uncle John, with a smile that made wrinkles around
his eyes, “generally come round right if you wait for them.”
“What is that?” asked Billy, pointing at the bar.
“That is a mold for a lathe,” answered Uncle John. “I’m nearly
through with it, then I’m going to help out on corn cutters. We have a
rush order on corn canning machines. You’d better sit on that box till
I’m through.”
Billy looked at the tiny trowel in Uncle John’s hand, and saw him
take off a little sand in one place, and put some on in another, until
the mold was smooth and even. Then he tested his corners with
what he called a “corner slick.”
“I never supposed that you worked that way,” said Billy, “but Miss
King told me that molders are artists in sand.”
“Did she, though?” said Uncle John, straightening up to take a final
look at his work. “I’ll remember that.
“Now we’ll go over where they are working on the corn cutters. It’s
a little cooler on that side.”
“Where does black sand come from?” asked Billy.
“It’s yellow,” answered his uncle, “when we begin to use it, but the
action of the hot iron, as we use it, over and over, turns it black.”
Then came the work that Billy had waited so long to see.
Uncle John took a wooden frame—he called it a drag—which was
about two feet square and not quite so deep. He put it on a bench
high enough for him to work easily. Then he laid six cutters for a corn
canning machine, side by side, in the bottom of the box.
“Those,” he said, “are patterns.”
Taking a sieve—a riddle—he filled it with moist sand which he
sifted over the cutters. Next, with his fingers, he packed the sand
carefully around the patterns. Then, with a shovel, he filled the drag
with sand, and rammed it down with a wooden rammer until the drag
was full.
“Now,” said he, taking up a wire, “I am going to make some vent
holes, so the steam can escape.”
When that was done, he clamped a top on the box, turned it over,
and took out the bottom.
Billy could see the cutters, bedded firm in the sand.
Blowing off the loose sand with bellows, and smoothing the sand
around the pattern, Uncle John took some dry sand, which he sifted
through his fingers, blowing it off where it touched the cutters.
“This sand,” he said, “will keep the two parts of the mold from
sticking together.”
HE FILLED IT WITH MOIST SAND

Then he took another frame, a cope, which was like the first,
except that it had pins on the sides, where the other had sockets.
Slipping the pins into the sockets, he fastened them together.
Taking two round, tapering plugs of wood, he set them firmly in the
sand, at each end of the patterns.
“One of those,” said he, “will make a place for the hot iron to go in,
and the other for it to rise up on the other side.”
Then he filled the second box as he had the first, and made more
vent holes.
“Billy,” he said, suddenly, “where are those corn cutters?”
“In the middle of the box,” answered Billy promptly, just as if he
had always known about molding in sand.
“Now,” said Uncle John, “comes the artist part.”
Lifting the second part off the first, he turned it over carefully and
set it on the bench.
“There they are,” exclaimed Billy.
“There they are,” said Uncle John, with a smile, “but there they are
not going to remain.”
Dipping a sponge in water, he wet the sand around the edges of
the pattern. Then he screwed a draw spike into the middle of the
pattern and rapped it gently with a mallet to loosen it from the sand.
“Pretty nearly perfect, aren’t they?” he said, when he had them all
safely out. “Now for some real artist work.”
With a lifter he took out the sand that had fallen into the mold,
patched a tiny break here and there, and tested the corners.
Last of all he made grooves, which he called “gates,” between the
patterns, and also at the ends where the iron was to be poured in.
Then he clamped the two boxes together. “Now the holes are in
the middle,” said he, “and I hope that they will stay there till the iron
is poured in.”
Billy, sitting on a box, watched Uncle John till he had finished
another set of molds.
“That all clear so far?” asked Uncle John.
“Sure,” answered Billy.
“Think you could do it yourself?” broke in a heavy voice.
Billy, looking up, saw the foreman, who had been watching Billy
while he watched his uncle.
“I think I know how,” answered Billy.
“If you won’t talk to the men,” said the foreman, “you may walk
around the foundry until we are ready to pour.”
So Billy walked slowly around the long foundry. He saw that each
man had his own pile of sand, but the piles were growing very small,
because the day’s work was nearly over. The molds were being put
in rows for the pouring.
He had walked nearly back to his Uncle John when he happened
to step in a hollow place in the earth floor and, losing his balance, fell
against a man who was carrying a mold.
With a strange, half-muttered expression the man pushed his
elbow against Billy and almost threw him down.
Billy, looking up into a pair of fierce black eyes that glared at him
from under a mass of coal black hair, turned so pale that William
Wallace then and there called him a coward.
As fast as his feet would carry him Billy went back to Uncle John,
who, still busy with his molds, said:
“Go out behind the foundry and look in at the window to see us
pour.”
Billy, for the first time in his life thoroughly frightened, was glad to
go out into the open air.
Then he went to the window opposite the great cupola to wait for
the pouring.
There at the left of the furnace door stood the foundry foreman, tall
and strong, holding a long iron rod in his hand. He, too, was waiting.
Then, because Billy had thought and thought over what Uncle
John had told him about pouring, his mind began to make a picture;
and when sparks of fire from the spout shot across the foundry, the
cupola became a fiery dragon and the foreman a noble knight,
bearing a long iron spear.
Only once breathed the dragon; for the knight, heedless of danger,
closed the iron mouth with a single thrust of his spear.
Another wait. This time the knight forced the dragon to open his
mouth, and the yielding dragon sent out his pointed, golden tongue.
But only for a moment; for again the knight thrust in his iron spear.
At last the knight gave way to the dragon.
Then, wonder of wonders, from the dragon’s mouth there came a
golden, molten stream.
When the great iron ladle below was almost filled, the knight
closed once more the dragon’s mouth.
Two by two came men bearing between them long-handled iron
ladles. The great ladle swung forward, for a moment, on its tilting
gear, and the men bore away their ladles filled with iron that the
great dragon had changed from its own dull gray to the brilliant
yellow of gold.
The molds, as they were filled, smoked from all their venting
places, till, to his picture, Billy added a place for a battle-field.
By the time that the last molds were filled, some of the men began
to take off the wooden frames, and there the iron was, gray again,
but, this time, shaped for the use of man.
“See,” said Uncle John, coming to the window, “there are our corn
cutters. Came out pretty well, didn’t they?”
“Wasn’t it great!” exclaimed Billy.
“Just about as wonderful every time,” said Uncle John.
“What do they do next?” asked Billy.
“Make new heaps of sand—every man his own heap—and in the
morning, after the castings have been carried into the mill, they
begin all over again.”
“I’m so glad I saw it,” said Billy, drawing a deep breath of
satisfaction.
That night he told Aunt Mary about what he had seen. And he
thought about it almost until he fell asleep. Almost, but not quite; for,
just as he was dozing off, William Wallace said:
“You were frightened—frightened. You showed a white feather!”
Half asleep as he was, Billy, tired of William Wallace’s superior
airs, roused himself long enough to say: “We’ll see who has white
feathers.”

You might also like