Instant Ebooks Textbook Practical Programming 3rd Edition Paul Gries Download All Chapters
Instant Ebooks Textbook Practical Programming 3rd Edition Paul Gries Download All Chapters
com
https://textbookfull.com/product/practical-
programming-3rd-edition-paul-gries/
textbookfull
More products digital (pdf, epub, mobi) instant
download maybe you interests ...
https://textbookfull.com/product/practical-programming-an-
introduction-to-computer-science-using-python-3-6-3rd-edition-
paul-gries/
https://textbookfull.com/product/programming-scala-3rd-edition-
dean-wampler/
https://textbookfull.com/product/business-management-answers-3rd-
edition-paul-hoang/
https://textbookfull.com/product/essentials-of-programming-in-
mathematica-1st-edition-paul-wellin/
Practical Electronics for Inventors, Fourth Edition
Paul Scherz
https://textbookfull.com/product/practical-electronics-for-
inventors-fourth-edition-paul-scherz/
https://textbookfull.com/product/business-management-workbook-
answers-3rd-edition-paul-hoang/
https://textbookfull.com/product/efficient-r-programming-a-
practical-guide-to-smarter-programming-1st-edition-colin-
gillespie/
https://textbookfull.com/product/practical-numerical-c-
programming-1st-edition-philip-joyce/
https://textbookfull.com/product/javascript-cookbook-programming-
the-web-3rd-edition-scott/
Acknowledgments
This book would be confusing and riddled with errors if it
weren’t for a bunch of awesome people who patiently and
carefully read our drafts.
It has a clean syntax. Yes, every language makes this claim, but
during the several years that we have been using it at the University
of Toronto, we have found that students make noticeably fewer
“punctuation” mistakes with Python than with C-like languages.
Footnotes
[1]
See http://www.ccs.neu.edu/home/matthias/HtDP2e/index.html.
[2]
See http://learnyousomeerlang.com.
[3]
See http://learnyouahaskell.com.
Take a look at the pictures above. The first one shows forest
cover in the Amazon basin in 1975. The second one shows the
same area twenty-six years later. Anyone can see that much of
the rainforest has been destroyed, but how much is “much”?
Of course, computers are good for a lot more than just science.
We used computers to write this book. Your smartphone is a
pretty powerful computer; you’ve probably used one today to
chat with friends, check your lecture notes, or look for a
restaurant that serves pizza and Chinese food. Every day,
someone figures out how to make a computer do something that
has never been done before. Together, those “somethings” are
changing the world.
This book will teach you how to make computers do what you
want them to do. You may be planning to be a doctor, a linguist,
or a physicist rather than a full-time programmer, but whatever
you do, being able to program is as important as being able to
write a letter or do basic arithmetic.
We begin in this chapter by explaining what programs and
programming are. We then define a few terms and present some
useful bits of information for course instructors.
Programs and Programming
A program is a set of instructions. When you write down
directions to your house for a friend, you are writing a program.
Your friend “executes” that program by following each
instruction in turn.
We hope that by the time you have finished reading this book,
you will see the world in a slightly different way.
What’s a Programming Language?
Directions to the nearest bus station can be given in English,
Portuguese, Mandarin, Hindi, and many other languages. As
long as the people you’re talking to understand the language,
they’ll get to the bus station.
3 + 4
(+ 3 4)
What happened in each case is that the people who wrote the
program told the computer to do something it couldn’t do: open
a file that didn’t exist, perhaps, or keep track of more
information than the computer could handle, or maybe repeat a
task with no way of stopping other than by rebooting the
computer. (Programmers don’t mean to make these kinds of
mistakes, they are just part of the programming process.)
Every piece of software that you can buy has bugs in it. Part of
your job as a programmer is to minimize the number of bugs
and to reduce their severity. In order to find a bug, you need to
track down where you gave the wrong instructions, then you
need to figure out the right instructions, and then you need to
update the program without introducing other bugs.
Every time you get a software update for a program, it is for one
of two reasons: new features were added to a program or bugs
were fixed. It’s always a game of economics for the software
company: are there few enough bugs, and are they minor
enough or infrequent enough in order for people to pay for the
software?
() Parentheses
[] Brackets
To deal with all these pieces, every computer runs some kind of
operating system, such as Microsoft Windows, Linux, or
macOS. An operating system, or OS, is a program; what makes
it special is that it’s the only program on the computer that’s
allowed direct access to the hardware. When any other
application (such as your browser, a spreadsheet program, or a
game) wants to draw on the screen, find out what key was just
pressed on the keyboard, or fetch data from storage, it sends a
request to the OS (see the top image).
There are two ways to use the Python interpreter. One is to tell
it to execute a Python program that is saved in a file with a .py
extension. Another is to interact with it in a program called a
shell, where you type statements one at a time. The interpreter
will execute each statement when you type it, do what the
statement says to do, and show any output as text, all in one
window. We will explore Python in this chapter using a Python
shell.
Programming requires practice: you won’t learn how to program just by reading this book,
much like you wouldn’t learn how to play guitar just by reading a book on how to play guitar.
Python comes with a program called IDLE, which we use to write Python programs. IDLE
has a Python shell that communicates with the Python interpreter and also allows you to
write and run programs that are saved in a file.
We strongly recommend that you open IDLE and follow along with our examples. Typing in
the code in this book is the programming equivalent of repeating phrases back to an
instructor as you’re learning to speak a new language.
Expressions and Values: Arithmetic in
Python
You’re familiar with mathematical expressions like 3 + 4 (“three
plus four”) and 2 - 3 / 5 (“two minus three divided by five”);
each expression is built out of values like 2, 3, and 5 and
operators like + and -, which combine their operands in
different ways. In the expression 4 / 5, the operator is “/” and
the operands are 4 and 5.
>>>4+13
17
>>>15-3
12
>>>4*7
28
>>>5/2
2.5
>>>4/2
2.0
TYPES
Every value in Python has a particular type, and the types of
values determine how they behave when they’re combined.
Values like 4 and 17 have type int (short for integer), and values
like 2.5 and 17.0 have type float. The word float is short for
floating point, which refers to the decimal point that moves
around between digits of the number.
>>>17.0-10.0
7.0
>>>17.0-10
7.0
>>>17-10.0
7.0
If you want, you can omit the zero after the decimal point when
writing a floating-point number:
>>>17-10.
7.0
>>>17.- 10
7.0
>>>53//24
2
We can find out how many hours are left over using the modulo
operator, which gives the remainder of the division:
>>>53%24
5
>>>17//10
1
>>>-17//10
-2
When using modulo, the sign of the result matches the sign of
the divisor (the second operand):
>>>-17%10
3
>>>17%-10
-3
For the mathematically inclined, the relationship between //
and % comes from this equation, for any two non-zero numbers
a and b:
(b * (a // b) + a % b) is equal to a
>>>3.3//1
3.0
>>>3//1.0
3.0
>>>3//1.1
2.0
>>>3.5//1.1
3.0
>>>3.5//1.3
2.0
>>>3**6
729
>>>-5
-5
>>>--5
5
>>>---5
-5
What Is a Type?
We’ve now seen two types of numbers (integers and floating-
point numbers), so we ought to explain what we mean by a type.
In Python, a type consists of two things:
A set of values
For example, in type int, the values are …, -3, -2, -1, 0, 1, 2, 3, …
and we have seen that these operators can be applied to those
values: +, -, *, /, //, %, and **.
The values in type float are a subset of the real numbers, and it
happens that the same set of operations can be applied to float
values. We can see what happens when these are applied to
various values in Table 1, Arithmetic Operators. If an operator
can be applied to more than one type of value, it is called an
overloaded operator.
- Negation -5 -5
- Subtraction 5 - 19 -14
Symbol Operator Example Result
/ Division 11 / 2 5.5
// Integer Division 11 // 2 5
** Exponentiation 2 ** 5 32
FINITE PRECISION
Floating-point numbers are not exactly the fractions you
learned in grade school. For example, look at Python’s version
of the fractions 2/3 and 5/3:
>>>2/3
0.6666666666666666
>>>5/3
1.6666666666666667
The first value ends with a 6, and the second with a 7. This is
fishy: both of them should have an infinite number of 6s after
the decimal point. The problem is that computers have a finite
amount of memory, and (to make calculations fast and memory
efficient) most programming languages limit how much
information can be stored for any single number. The number
0.6666666666666666 turns out to be the closest value to 2/3
that the computer can actually store in that limited amount of
memory, and 1.6666666666666667 is as close as we get to the
real value of 5/3.
Integers (values of type int) in Python can be as large or as small as you like. However, float
values are only approximations to real numbers. For example, 1/4 can be stored exactly, but
as we’ve already seen, 2/3 cannot. Using more memory won’t solve the problem, though it
will make the approximation closer to the real value, just as writing a larger number of 6s
after the 0 in 0.666… doesn’t make it exactly equal to 2/3.
The difference between 2/3 and 0.6666666666666666 may look tiny. But if we use
0.6666666666666666 in a calculation, then the error may get compounded. For example, if
we add 1 to 2/3, the resulting value ends in …6665, so in many programming languages, 1 +
2/3 is not equal to 5/3:
>>>2/3+1
1.6666666666666665
>>>5/3
1.6666666666666667
As we do more calculations, the rounding errors can get larger and larger, particularly if we’re
mixing very large and very small numbers. For example, suppose we add 10000000000 (10
billion) and 0.00000000001 (there are 10 zeros after the decimal point):
>>>10000000000+0.00000000001
10000000000.0
The result ought to have twenty zeros between the first and last significant digit, but that’s
too many for the computer to store, so the result is just 10000000000—it’s as if the addition
never took place. Adding lots of small numbers to a large one can therefore have no effect at
all, which is not what a bank wants when it totals up the values of its customers’ savings
accounts.
It’s important to be aware of the floating-point issue. There is no magic bullet to solve it,
because computers are limited in both memory and speed. Numerical analysis, the study of
algorithms to approximate continuous mathematics, is one of the largest subfields of
computer science and mathematics.
Here’s a tip: If you have to add up floating-point numbers, add them from smallest to largest
in order to minimize the error.
Another random document with
no related content on Scribd:
Scrap.—After the lard is drawn from the cooking kettle, the scrap
should be drawn from the bottom. This scrap is used in the
prime steam lard tank. If, however, there is no other provision for
handling the scrap, it should be put into a hoop press (Fig. 150), and
pressure applied, thereby liberating all the oil left in the residue, the
scrap then being used as “pressed scrap.”
Packing and Cooling.—Kettle rendered lard is usually sold and
used more extensively during the cooler weather. It is very desirable
that there should be a light, fluffy top. This is only possible when the
lard is drawn hot in a cool room; chilling the lard rapidly causes this
appearance at the top, which is always looked upon by the trade as
a mark of excellence. When the lard is drawn off into small packages
they are placed one on top of the other, covering the top with paper.
The cover should not be put on the package until it is chilled,
otherwise the fluffy appearance is lost.
FIG. 150.—HOOP PRESS.
The oil when extracted from the seed is termed “crude oil.” In
refining this oil the loss varies from 7 per cent to 12 per cent, on an
average about 9¹⁄₄ per cent.
Refining Crude Oil.—The crude oil is purchased by refiners and
treated to produce “yellow oil.” In this process it is put into a tank (it
is generally considered profitable to refine cotton seed oil only in
large quantities) supplied with a revolving agitator. Into the tank is
put a solution of eighteen to twenty per cent caustic soda. The
quantity and strength of the solution necessary is determined by
treating a small sample. To a small sample of oil add the soda
solution, stirring continuously, having it heated to a temperature of
160° to 180° F. When sufficient soda lye has been added a floculent
precipitation will be noticed. This indicates a “breaking” of the oil. By
calculation of the relative amount the comparative quantities can be
arrived at.
An excessive amount of lye will saponify its equivalent in good oil,
therefore care must be exercised to see that only the proper amount
is used. In a practical way the soda is introduced in the crude oil
solution and agitation is started to insure the thorough mixing of the
caustic soda and the oil. The floculent substance appearing, the
agitation is stopped and the oil allowed to settle, the sediment and
substance other than oil collecting at the bottom of the tank. A small
quantity (about one-quarter of one per cent by weight) of fullers earth
is added and the oil removed by pumping through a filter press
producing what is known to the trade as “yellow oil.”
The sediment, known as “foots” is collected in kettles and treated
with additional lye, boiled and settled with salt; water added, settled
and drawn. This treatment is duplicated as many times as
necessary, until the soap stock will separate from impurities. When
thoroughly settled draw the soap stock into packages for the soap
trade. The finished “foots” contains about 33 to 40 per cent of
moisture and a small percentage of lye.
Deodorizing Cotton Seed Oil.—The “yellow oil” of trade has a
decided flavor which it is desirable to remove, and this process is
accomplished by deodorizing. (See Fig. 155.)
Treating Tank.—This tank is equipped for heating the oil to a very
high temperature by the introduction of a large coil surface,
preferably rings, one within the other and arranged so that each ring
is accessible. The kettle coils, etc., should be made extra heavy. The
top of the kettle must also be hooded and as the oil boils violently the
steam must be given free opportunity to escape. The escape pipe
should be at least sixteen to twenty inches in diameter in a tank six
feet in diameter, and should be provided with a goose neck, so that
the steam rising which condenses, does not drip back into the tank.
Fig. 155 show the construction of an oil treating tank.
The exhaust pipe extends into the atmosphere a short distance
above the deodorizing tanks. On account of the extreme agitation
caused by the heat, the oil is likely to boil over and waste. Special
attention is called to the crook or neck in the exhaust pipe with two-
inch outlet at the lowest point. This is done so that the condensation
will not run back into the oil, but can be taken out at this point.
Process For Oil.—The tank is filled with oil to about half its
capacity. The oil is then heated to a temperature
of 320° to 360° F. After being held at this temperature for an hour to
an hour and a half, it is ready for the washing process. This is done
by means of a two-inch perforated pipe at the top of the kettle,
through which is sprinkled cold water on the oil. The extreme
temperature evaporates the water very rapidly and at the same time
causes a precipitation. The water should be allowed to run for five
minutes, and then the body of oil should stand for from one to two
hours. The cone, or bottom of the kettle, will be found to have a
sediment which has been precipitated from the oil. This “bottom”
should be drawn off, and the steam again turned on the coils.
FIG. 155.—DEODORIZING TANK FOR COTTON SEED OILS.