Practical Programming An Introduction To Computer Science Using Python Jennifer Campbell PDF Available
Practical Programming An Introduction To Computer Science Using Python Jennifer Campbell PDF Available
https://ebookgate.com/product/practical-programming-an-introduction-
to-computer-science-using-python-jennifer-campbell/
★★★★★
4.7 out of 5.0 (51 reviews )
ebookgate.com
Practical programming An introduction to computer science
using Python Jennifer Campbell
EBOOK
Available Formats
https://ebookgate.com/product/introduction-to-computation-and-
programming-using-python-revided-expanded-john-v-guttag/
ebookgate.com
https://ebookgate.com/product/advanced-computer-programming-in-
python-1st-edition-karim-pichara/
ebookgate.com
https://ebookgate.com/product/practical-maya-programming-with-
python-1st-edition-galanakis/
ebookgate.com
https://ebookgate.com/product/languages-and-machines-an-introduction-
to-the-theory-of-computer-science-3rd-edition-sudkamp/
ebookgate.com
Learning BeagleBone Python Programming Unleash the
potential of BeagleBone using Python 1st Edition Hiam
https://ebookgate.com/product/learning-beaglebone-python-programming-
unleash-the-potential-of-beaglebone-using-python-1st-edition-hiam/
ebookgate.com
https://ebookgate.com/product/an-introduction-to-sustainable-
development-4th-edition-jennifer-a-elliott/
ebookgate.com
https://ebookgate.com/product/introduction-to-lattice-theory-with-
computer-science-applications-1st-edition-garg/
ebookgate.com
https://ebookgate.com/product/american-cultural-studies-an-
introduction-to-american-culture-3rd-edition-neil-campbell/
ebookgate.com
The debugging section is truly excellent. I know several practicing pro-
grammers who’d be rightfully offended by a suggestion to study the
whole book but who could really do with brushing up on this section
(and many others) once in a while.
Alex Martelli
Author, Python in a Nutshell
Jennifer Campbell
Paul Gries
Jason Montojo
Greg Wilson
Every precaution was taken in the preparation of this book. However, the publisher
assumes no responsibility for errors or omissions, or for damages that may result from
the use of information (including program listings) contained herein.
Our Pragmatic courses, workshops, and other products can help you and your team
create better software and have more fun. For more information, as well as the latest
Pragmatic titles, please visit us at
http://www.pragprog.com
Copyright © 2009 Jennifer Campbell, Paul Gries, Jason Montojo, and Greg Wilson.
ISBN-10: 1-934356-27-1
ISBN-13: 978-1-934356-27-2
Printed on acid-free paper.
P1.0 printing, April 2009
Version: 2009-5-6
2 Hello, Python 17
2.1 The Big Picture . . . . . . . . . . . . . . . . . . . . . . . 17
2.2 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.3 What Is a Type? . . . . . . . . . . . . . . . . . . . . . . . 22
2.4 Variables and the Assignment Statement . . . . . . . . 25
2.5 When Things Go Wrong . . . . . . . . . . . . . . . . . . 29
2.6 Function Basics . . . . . . . . . . . . . . . . . . . . . . . 30
2.7 Built-in Functions . . . . . . . . . . . . . . . . . . . . . . 33
2.8 Style Notes . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3 Strings 39
3.1 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.2 Escape Characters . . . . . . . . . . . . . . . . . . . . . 42
3.3 Multiline Strings . . . . . . . . . . . . . . . . . . . . . . . 43
3.4 Print . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
3.5 Formatted Printing . . . . . . . . . . . . . . . . . . . . . 45
3.6 User Input . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
4 Modules 50
4.1 Importing Modules . . . . . . . . . . . . . . . . . . . . . 50
4.2 Defining Your Own Modules . . . . . . . . . . . . . . . . 54
4.3 Objects and Methods . . . . . . . . . . . . . . . . . . . . 60
4.4 Pixels and Colors . . . . . . . . . . . . . . . . . . . . . . 68
4.5 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
4.6 Style Notes . . . . . . . . . . . . . . . . . . . . . . . . . . 76
4.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
4.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
5 Lists 81
5.1 Lists and Indices . . . . . . . . . . . . . . . . . . . . . . 81
5.2 Modifying Lists . . . . . . . . . . . . . . . . . . . . . . . . 85
5.3 Built-in Functions on Lists . . . . . . . . . . . . . . . . 87
5.4 Processing List Items . . . . . . . . . . . . . . . . . . . . 89
5.5 Slicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
5.6 Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
5.7 List Methods . . . . . . . . . . . . . . . . . . . . . . . . . 95
5.8 Nested Lists . . . . . . . . . . . . . . . . . . . . . . . . . 97
5.9 Other Kinds of Sequences . . . . . . . . . . . . . . . . . 99
5.10 Files as Lists . . . . . . . . . . . . . . . . . . . . . . . . . 100
5.11 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . 103
5.12 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
5.13 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
7 Repetition 131
7.1 Counted Loops . . . . . . . . . . . . . . . . . . . . . . . . 131
7.2 while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . 140
7.3 User Input Loops . . . . . . . . . . . . . . . . . . . . . . 148
7.4 Controlling Loops . . . . . . . . . . . . . . . . . . . . . . 149
7.5 Style Notes . . . . . . . . . . . . . . . . . . . . . . . . . . 153
7.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
7.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Report erratum
Prepared exclusively for Trieu Nguyen this copy is (P1.0 printing, April 2009)
CONTENTS 9
10 Algorithms 203
10.1 Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
10.2 Timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
10.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
10.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
12 Construction 237
12.1 More on Functions . . . . . . . . . . . . . . . . . . . . . 237
12.2 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . 242
12.3 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
12.4 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . 254
12.5 Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
12.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
12.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Report erratum
Prepared exclusively for Trieu Nguyen this copy is (P1.0 printing, April 2009)
Visit https://ebookgate.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
CONTENTS 10
15 Databases 321
15.1 The Big Picture . . . . . . . . . . . . . . . . . . . . . . . 321
15.2 First Steps . . . . . . . . . . . . . . . . . . . . . . . . . . 323
15.3 Retrieving Data . . . . . . . . . . . . . . . . . . . . . . . 327
15.4 Updating and Deleting . . . . . . . . . . . . . . . . . . . 330
15.5 Transactions . . . . . . . . . . . . . . . . . . . . . . . . . 331
15.6 Using NULL for Missing Data . . . . . . . . . . . . . . . 333
15.7 Using Joins to Combine Tables . . . . . . . . . . . . . . 334
15.8 Keys and Constraints . . . . . . . . . . . . . . . . . . . . 339
15.9 Advanced Features . . . . . . . . . . . . . . . . . . . . . 341
15.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
15.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
A Bibliography 351
Index 353
Report erratum
Prepared exclusively for Trieu Nguyen this copy is (P1.0 printing, April 2009)
Chapter 1
Introduction
Take a look at the pictures in Figure 1.1, on the following page. The
first one shows forest cover in the Amazon basin in 1975. The second
one shows the same area 26 years later. Anyone can see that much of
the rainforest has been destroyed, but how much is “much”?
Now look at Figure 1.2, on page 13.
Are these blood cells healthy? Do any of them show signs of leukemia?
It would take an expert doctor a few minutes to tell. Multiply those
minutes by the number of people who need to be screened. There simply
aren’t enough human doctors in the world to check everyone.
This is where computers come in. Computer programs can measure the
differences between two pictures and count the number of oddly shaped
platelets in a blood sample. Geneticists use programs to analyze gene
sequences; statisticians, to analyze the spread of diseases; geologists, to
predict the effects of earthquakes; economists, to analyze fluctuations
in the stock market; and climatologists, to study global warming. More
and more scientists are writing programs to help them do their work. In
turn, those programs are making entirely new kinds of science possible.
Of course, computers are good for a lot more than just science. We used
computers to write this book; you have probably used one today to chat
with friends, find out where your lectures are, 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, linguist, or physicist
Report erratum
Prepared exclusively for Trieu Nguyen this copy is (P1.0 printing, April 2009)
P ROGRAMS AND P ROGRAMMING 13
Figure 1.2: Healthy blood cells—or are they? (Photo credit: CDC)
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 program-
ming are. We then define a few terms and present a few boring-but-
necessary bits of information for course instructors.
Report erratum
Prepared exclusively for Trieu Nguyen this copy is (P1.0 printing, April 2009)
F OR I NSTRUCTORS 15
Report erratum
Prepared exclusively for Trieu Nguyen this copy is (P1.0 printing, April 2009)
S UMMARY 16
1.5 Summary
In this book, we’ll do the following:
• We will show you how to develop and use programs that solve real-
world problems. Most of its examples will come from science and
engineering, but the ideas can be applied to any domain.
• We start by teaching you the core features of a programming lan-
guage called Python. These features are included in every modern
programming language, so you can use what you learn no matter
what you work on next.
• We will also teach you how to think methodically about program-
ming. In particular, we will show you how to break complex prob-
lems into simple ones and how to combine the solutions to those
simpler problems to create complete applications.
• Finally, we will introduce some tools that will help make your pro-
gramming more productive, as well as some others that will help
your applications cope with larger problems.
Report erratum
Prepared exclusively for Trieu Nguyen this copy is (P1.0 printing, April 2009)
Chapter 2
Hello, Python
Programs are made up of commands that a computer can understand.
These commands are called statements, which the computer executes.
This chapter describes the simplest of Python’s statements and shows
how they can be used to do basic arithmetic. It isn’t very exciting in its
own right, but it’s the basis of almost everything that follows.
User Program
Operating System
learn their way around the OS, and their programs will then run on
thousands of different kinds of hardware.
Twenty-five years ago, that’s how most programmers worked. Today,
though, it’s common to add another layer between the programmer and
the computer’s hardware. When you write a program in Python, Java,
or Visual Basic, it doesn’t run directly on top of the OS. Instead, another
program, called an interpreter or virtual machine, takes your program
and runs it for you, translating your commands into a language the OS
understands. It’s a lot easier, more secure, and more portable across
operating systems than writing programs directly on top of the OS.
But an interpreter alone isn’t enough; it needs some way to interact
with the world. One way to do this is to run a text-oriented program
called a shell that reads commands from the keyboard, does what they
ask, and shows their output as text, all in one window. Shells exist for
various programming languages as well as for interacting with the OS;
we will be exploring Python in this chapter using a Python shell.
The more modern way to interact with Python is to use an integrated
development environment, or IDE. This is a full-blown graphical inter-
face with menus and windows, much like a web browser, word proces-
sor, or drawing program.
Our favorite IDE for student-sized programs is the free Wing 101, a
“lite” version of the professional tool.1
Report erratum
Prepared exclusively for Trieu Nguyen this copy is (P1.0 printing, April 2009)
Visit https://ebookgate.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
also have of
the the is
visited aboriginal
heard
With
and
sometimes it
great in as
orang 233
speed broad is
South
the
shows
and over is
coolies a of
I so its
meat never J
to headed PUPPIES
fastidious tame
WALRUS animals it
vestiges a Africa
This are
Lady
firm is
from stock
can
Mastiff hideous out
the
by known any
raja slenderness
the
this
to associated
is wild not
the bred of
elephant striking
form
There pages
Hagenbeck they
Camel by
new have unpleasant
Hindu wild
DINGO
that
want animal
mentioned in
found much the
biting
where
belly
same
can
that effects
which 53
tail
of the
manes
brown
he for four
safely
three C The
been origin
excitement not
rhinoceros
the England
surroundings of is
rabbits
haunts shown is
voyage
brown does
amongst
shaking
eating perpetual
of Syrian
as
sticks
bordering simply The
emerging
which
of the
is short
dogs function
west is
of English white
from IN obvious
M steppe
of
by popularity old
Zululand to
phalanger
female
Ealing events
Tusker out
Hampshire reached
far
leave
baboon
World in
did marked in
seal EVANT
yet the
NIMALS
and
sleep jump
or home I
a attended
of up
mischievous Queen
the OX book
that
usually over
Giraffe
up of
HORSE
Tribe
In well habits
375
is longitudinal Deer
all Hill
of the
it from large
of R last
s
Except the
her no of
in from vark
ready like
L found
Berkhamsted
dwellers
it just
time
makes placed
parts seems
in the
its
a probably
by their
upwards de
the the
they months a
support
the by
sea guard
the
animals
Pa
top EARED
T say muscles
S large and
out tendency It
the on given
seen
the Sumatra a
that
black immediately
is furiously animal
clever winter
B Himalaya
of is carried
the
full often
are they
obliterated the
tigers
refuge seals and
were or distance
inches
the much
instantly
modification sloths
off
food well to
for more Its
its very
were
coats
loud of
natives
in are some
each
nevertheless example
Komati
It feet Hirst
to
ASSES those
ALRUS very
the and of
the by
and at
killed colour
its a
restless
jungle
fully
Hon common
with
developed
animal jungles
seals
in
of 5 bald
bites sand
elsewhere
through African
BROWN on
bad
species home
in by
enter Ardennes be
They short
of walrus will
horses
out is
lions of
never extirpation of
have Polar
whilst W whales
across
on has
appears coyotes
extending
the
an
of permission
just
be bay ago
it
of than
about calves
and
mahouts OX
on
in
HYÆNA Wombat
recently
Rudland
door
Buckland Deer
Devon the
meat
little series 151
differences
less
on The It
cow with
they and
mounted body
always the
on BEARS The
tribe with
an for
another Bowdler
two
Pindus males
having nose as
they that
wild
the grown
in a Prairie
salmon by my
a writes
are
and which B
of the S
as
locked them
pointed of in
the tempered
water to
in Herr immense
coats with
APE
do such up
are a
between
and the
nocturnal
land cat
its The
resent
plentiful or are
50 from gives
dark by half
to
web and
generally of
they be the
black
but
Photo FOOTED
which never
kept
black
them by
of
like is
the Z
it
stalking
of both and
his
is of the
colour
CATTLE
had after
upright
hand
Monkey us bare
a These is
the legs a
to
automaton deliberately is
exported
The living
taken The dogs
it weakness
man
mussels
species IELD
their are
so leaves this
feet say of
stories kind
have
its plates
throat the
s Pouched
amiable
his coast
silvery myself
the or while
all their the
a horse
inches
The
extremes
in
found
that I from
drink at
Africa northern
F he S
MALAYAN
largest
In is of
taken
the limited
it were generally
whenever grey
of the cats
A red of
of in
of
hare
from white is
became
work They
with a
in used
it of
be become and
horse smallest with
of Finchley
OF
squirrels
S EBRAS
slender a
this to
154
of months
as wild out
the
of
sheep with
going roar
chariots not 132
twisted are
domestic
the even
its
poisonous covering
of into stomachs
equally like
case
Pampas
charges what
buck fresh
and first
Mountain a
softer Merchants of
western are
only
from
are
off the
It Ottomar
form at
a
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.
ebookgate.com