100% found this document useful (11 votes)
101 views89 pages

Practical Programming An Introduction To Computer Science Using Python Jennifer Campbell PDF Available

The document provides information about the book 'Practical Programming: An Introduction to Computer Science Using Python' by Jennifer Campbell and others, which is available for instant PDF download. It includes various programming topics and is rated 4.7 out of 5 based on 51 reviews. The book aims to build computational insights and encourages practical application of programming skills.

Uploaded by

shonnanadj5098
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 (11 votes)
101 views89 pages

Practical Programming An Introduction To Computer Science Using Python Jennifer Campbell PDF Available

The document provides information about the book 'Practical Programming: An Introduction to Computer Science Using Python' by Jennifer Campbell and others, which is available for instant PDF download. It includes various programming topics and is rated 4.7 out of 5 based on 51 reviews. The book aims to build computational insights and encourages practical application of programming skills.

Uploaded by

shonnanadj5098
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/ 89

Practical programming An introduction to computer

science using Python Jennifer Campbell download

https://ebookgate.com/product/practical-programming-an-introduction-
to-computer-science-using-python-jennifer-campbell/

★★★★★
4.7 out of 5.0 (51 reviews )

Instant PDF Access

ebookgate.com
Practical programming An introduction to computer science
using Python Jennifer Campbell

EBOOK

Available Formats

■ PDF eBook Study Guide Ebook

EXCLUSIVE 2025 ACADEMIC EDITION – LIMITED RELEASE

Available Instantly Access Library


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Introduction to Computation and Programming Using Python


Revided Expanded John V. Guttag

https://ebookgate.com/product/introduction-to-computation-and-
programming-using-python-revided-expanded-john-v-guttag/

ebookgate.com

Advanced Computer Programming in Python 1st Edition Karim


Pichara

https://ebookgate.com/product/advanced-computer-programming-in-
python-1st-edition-karim-pichara/

ebookgate.com

Practical Maya Programming with Python 1st Edition


Galanakis

https://ebookgate.com/product/practical-maya-programming-with-
python-1st-edition-galanakis/

ebookgate.com

Languages and Machines An Introduction to the Theory of


Computer Science 3rd Edition Sudkamp

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

An introduction to programming using Visual Basic 2010


with Microsoft Visual Studio Expression editions DVD
Schneider
https://ebookgate.com/product/an-introduction-to-programming-using-
visual-basic-2010-with-microsoft-visual-studio-expression-editions-
dvd-schneider/
ebookgate.com

An Introduction to Sustainable Development 4th Edition


Jennifer A. Elliott

https://ebookgate.com/product/an-introduction-to-sustainable-
development-4th-edition-jennifer-a-elliott/

ebookgate.com

Introduction to lattice theory with computer science


applications 1st Edition Garg

https://ebookgate.com/product/introduction-to-lattice-theory-with-
computer-science-applications-1st-edition-garg/

ebookgate.com

American Cultural Studies An Introduction to American


Culture 3rd Edition Neil Campbell

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

This book succeeds in two different ways. It is both a science-focused


CS1 text and a targeted Python reference. Even as it builds students’
computational insights, it also empowers and encourages them to
immediately apply their newfound programming skills in the lab or
on projects of their own.
Zachary Dodds
Associate professor of computer science, Harvey Mudd College

Prepared exclusively for Trieu Nguyen


Prepared exclusively for Trieu Nguyen
Practical Programming
An Introduction to Computer Science Using Python

Jennifer Campbell
Paul Gries
Jason Montojo
Greg Wilson

The Pragmatic Bookshelf


Raleigh, North Carolina Dallas, Texas

Prepared exclusively for Trieu Nguyen


Many of the designations used by manufacturers and sellers to distinguish their prod-
ucts are claimed as trademarks. Where those designations appear in this book, and The
Pragmatic Programmers, LLC was aware of a trademark claim, the designations have
been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The
Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g
device are trademarks of The Pragmatic Programmers, LLC.

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.

All rights reserved.

No part of this publication may be reproduced, stored in a retrieval system, or transmit-


ted, in any form, or by any means, electronic, mechanical, photocopying, recording, or
otherwise, without the prior consent of the publisher.

Printed in the United States of America.

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

Prepared exclusively for Trieu Nguyen


Contents
1 Introduction 11
1.1 Programs and Programming . . . . . . . . . . . . . . . . 13
1.2 A Few Definitions . . . . . . . . . . . . . . . . . . . . . . 14
1.3 What to Install . . . . . . . . . . . . . . . . . . . . . . . . 14
1.4 For Instructors . . . . . . . . . . . . . . . . . . . . . . . . 15
1.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

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

Prepared exclusively for Trieu Nguyen


CONTENTS 8

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

6 Making Choices 108


6.1 Boolean Logic . . . . . . . . . . . . . . . . . . . . . . . . 108
6.2 if Statements . . . . . . . . . . . . . . . . . . . . . . . . . 118
6.3 Storing Conditionals . . . . . . . . . . . . . . . . . . . . 125
6.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
6.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

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

8 File Processing 159


8.1 One Record per Line . . . . . . . . . . . . . . . . . . . . 160
8.2 Records with Multiple Fields . . . . . . . . . . . . . . . . 171
8.3 Positional Data . . . . . . . . . . . . . . . . . . . . . . . 174
8.4 Multiline Records . . . . . . . . . . . . . . . . . . . . . . 177
8.5 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . 179
8.6 Writing to Files . . . . . . . . . . . . . . . . . . . . . . . . 181
8.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
8.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

9 Sets and Dictionaries 185


9.1 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
9.2 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . 190
9.3 Inverting a Dictionary . . . . . . . . . . . . . . . . . . . . 197
9.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
9.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 199

10 Algorithms 203
10.1 Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
10.2 Timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
10.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
10.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 212

11 Searching and Sorting 214


11.1 Linear Search . . . . . . . . . . . . . . . . . . . . . . . . 214
11.2 Binary Search . . . . . . . . . . . . . . . . . . . . . . . . 218
11.3 Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
11.4 More Efficient Sorting Algorithms . . . . . . . . . . . . . 228
11.5 Mergesort: An N log2 N Algorithm . . . . . . . . . . . . . 229
11.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
11.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 234

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

13 Object-Oriented Programming 270


13.1 Class Color . . . . . . . . . . . . . . . . . . . . . . . . . . 271
13.2 Special Methods . . . . . . . . . . . . . . . . . . . . . . . 276
13.3 More About dir and help . . . . . . . . . . . . . . . . . . 278
13.4 A Little Bit of OO Theory . . . . . . . . . . . . . . . . . . 280
13.5 A Longer Example . . . . . . . . . . . . . . . . . . . . . . 288
13.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
13.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 293

14 Graphical User Interfaces 294


14.1 The Tkinter Module . . . . . . . . . . . . . . . . . . . . . 295
14.2 Basic GUI Construction . . . . . . . . . . . . . . . . . . 296
14.3 Models, Views, and Controllers . . . . . . . . . . . . . . 301
14.4 Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
14.5 A Few More Widgets . . . . . . . . . . . . . . . . . . . . . 312
14.6 Object-Oriented GUIs . . . . . . . . . . . . . . . . . . . . 316
14.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
14.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 318

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

Prepared exclusively for Trieu Nguyen


C HAPTER 1. I NTRODUCTION 12

Figure 1.1: The Rainforest Retreats (Photo credit: NASA/Goddard Space


Flight Center Scientific Visualization Studio)

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.

1.1 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 “exe-
cutes” that program by following each instruction in turn.
Every program is written in terms of a few basic operations that its
reader already understands. For example, the set of operations that
your friend can understand might include the following: “Turn left at
Darwin Street,” “Go forward three blocks,” and “If you get to the gas
station, turn around—you’ve gone too far.”
Computers are similar but have a different set of operations. Some
operations are mathematical, like “Add 10 to a number and take the
square root,” while others include “Read a line from the file named
data.txt,” “Make a pixel blue,” or “Send email to the authors of this
book.”
The most important difference between a computer and an old-
fashioned calculator is that you can “teach” a computer new opera-
tions by defining them in terms of old ones. For example, you can teach
the computer that “Take the average” means “Add up the numbers in
a set and divide by the set’s size.” You can then use the operations you
have just defined to create still more operations, each layered on top of
the ones that came before. It’s a lot like creating life by putting atoms
Report erratum
Prepared exclusively for Trieu Nguyen this copy is (P1.0 printing, April 2009)
A F EW D EFINITIONS 14

together to make proteins and then combining proteins to build cells


and giraffes.
Defining new operations, and combining them to do useful things, is
the heart and soul of programming. It is also a tremendously powerful
way to think about other kinds of problems. As Prof. Jeannette Wing
wrote [Win06], computational thinking is about the following:
• Conceptualizing, not programming. Computer science is not com-
puter programming. Thinking like a computer scientist means
more than being able to program a computer. It requires think-
ing at multiple levels of abstraction.
• A way that humans, not computers, think. Computational thinking
is a way humans solve problems; it is not trying to get humans
to think like computers. Computers are dull and boring; humans
are clever and imaginative. We humans make computers exciting.
Equipped with computing devices, we use our cleverness to tackle
problems we would not dare take on before the age of computing
and build systems with functionality limited only by our imagina-
tions.
• For everyone, everywhere. Computational thinking will be a reality
when it is so integral to human endeavors it disappears as an
explicit philosophy.
We hope that by the time you have finished reading this book, you will
see the world in a slightly different way.

1.2 A Few Definitions


One of the pieces of terminology that causes confusion is what to call
certain characters. The Python style guide (and several dictionaries) use
these names, so this book does too:
() Parentheses
[] Brackets
{} Braces

1.3 What to Install


For current installation instructions, please download the code from
the book website and open install/index.html in a browser. The book URL
is http://pragprog.com/titles/gwpy/practical-programming.

Report erratum
Prepared exclusively for Trieu Nguyen this copy is (P1.0 printing, April 2009)
F OR I NSTRUCTORS 15

1.4 For Instructors


This book uses the Python programming language to introduce stan-
dard CS1 topics and a handful of useful applications. We chose Python
for several reasons:
• It is free and well documented. In fact, Python is one of the largest
and best-organized open source projects going.
• It runs everywhere. The reference implementation, written in C, is
used on everything from cell phones to supercomputers, and it’s
supported by professional-quality installers for Windows, Mac OS,
and Linux.
• It has a clean syntax. Yes, every language makes this claim, but in
the four years 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.
• It is relevant. Thousands of companies use it every day; it is one of
the three “official languages” at Google, and large portions of the
game Civilization IV are written in Python. It is also widely used
by academic research groups.
• It is well supported by tools. Legacy editors like Vi and Emacs all
have Python editing modes, and several professional-quality IDEs
are available. (We use a free-for-students version of one called
Wing IDE.)
We use an “objects first, classes second” approach: students are shown
how to use objects from the standard library early on but do not create
their own classes until after they have learned about flow control and
basic data structures. This compromise avoids the problem of explain-
ing Java’s public static void main(String[ ] args) to someone who has never
programmed.
We have organized the book into two parts. The first covers fundamen-
tal programming ideas: elementary data types (numbers, strings, lists,
sets, and dictionaries), modules, control flow, functions, testing, debug-
ging, and algorithms. Depending on the audience, this material can be
covered in nine or ten weeks.
The second part of the book consists of more or less independent chap-
ters on more advanced topics that assume all the basic material has
been covered. The first of these chapters shows students how to create
their own classes and introduces encapsulation, inheritance, and poly-
morphism; courses for computer science majors will want to include

Report erratum
Prepared exclusively for Trieu Nguyen this copy is (P1.0 printing, April 2009)
S UMMARY 16

this material. The other chapters cover application areas, such as 3D


graphics, databases, GUI construction, and the basics of web program-
ming; these will appeal to both computer science majors and students
from the sciences and will allow the book to be used for both.
Lots of other good books on Python programming exist. Some are acces-
sible to novices [Guz04, Zel03], and others are for anyone with any
previous programming experience [DEM02, GL07, LA03]. You may also
want to take a look at [Pyt], the special interest group for educators
using Python.

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.

2.1 The Big Picture


In order to understand what happens when you’re programming, you
need to have a basic understanding of how a program gets executed on
a computer. The computer itself is assembled from pieces of hardware,
including a processor that can execute instructions and do arithmetic,
a place to store data such as a hard drive, and various other pieces such
as computer monitor, a keyboard, a card for connecting to a network,
and so on.
To deal with all these pieces, every computer runs some kind of operat-
ing system, such as Microsoft Windows, Linux, or Mac OS X. An oper-
ating 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 hard-
ware. When any other program on the computer wants to draw on the
screen, find out what key was just pressed on the keyboard, or fetch
data from the hard drive, it sends a request to the OS (see Figure 2.1,
on the following page).
This may seem a roundabout way of doing things, but it means that
only the people writing the OS have to worry about the differences
between one network card and another. Everyone else—everyone ana-
lyzing scientific data or creating 3D virtual chat rooms—only has to

Prepared exclusively for Trieu Nguyen


T HE B IG P ICTURE 18

User Program

Operating System

Hard Drive Monitor

Figure 2.1: Talking to the 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

1. See http://www.wingware.com for details.

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

began mainly one

visited aboriginal

heard

With

and
sometimes it

great in as

the cover from

orang 233

yet fish the

speed broad is

South

the
shows

and over is

coolies a of

I so its

meat never J

the and LIONESS

to headed PUPPIES

fastidious tame
WALRUS animals it

vestiges a Africa

wall recently are

This are

Lady

firm is

from stock

can
Mastiff hideous out

the

by known any

raja slenderness

food Zoological into

the
this

to associated

is wild not

the bred of

elephant striking
form

There pages

Short all 148

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

size for larger

rabbits

haunts shown is

voyage

brown does
amongst

shaking

moderate shooting base

out that land

eating perpetual

of Syrian

and identically its

as

sticks
bordering simply The

emerging

any carry along

which

of the

is short

dogs function
west is

of English white

fox will the

log One and

existing Southern STOAT

from IN obvious

over speed food

M steppe

Ugliness scope ING

of
by popularity old

Zululand to

phalanger

female

Ealing events

Tusker out

Hampshire reached
far

leave

baboon

World in

did marked in

young former three

some The mussels

seal EVANT

yet the
NIMALS

and

many wooded black

sleep jump

or home I

a attended

being seems Peru


not habits

own them the

of up

mischievous Queen

tiger the its

the OX book
that

usually over

Giraffe

up of

into the cold


night Greece

HORSE

Tribe

In well habits

375

the texture you


of

is longitudinal Deer

our perhaps been

all Hill

better towards Africa

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

horses condition building

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

respect eyes chief

and numbers upside

his MARTEN was

are they

obliterated the

tigers
refuge seals and

were or distance

inches

termed the shakes

the much

instantly

kill intended are

modification sloths

off

food well to
for more Its

North which processes

its very

were

coats

sleeping allied degree


are which two

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

never savage ice

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

than the pouches

it

of than

about calves

and

mahouts OX

on
in

105 Berlin where

HYÆNA Wombat

recently

Rudland

door

Buckland Deer

Devon the

meat
little series 151

differences

measured darkness all

less

bats and Athol

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

the Santa Giraffes

cat her the

a writes

are

and which B
of the S

Canary fur may

as

locked them

pointed of in

the tempered

old but man


Every B

water to

in Herr immense

coats with

APE

do such up

are a

between

and the
nocturnal

land cat

years objects Africa

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

221 ground gorilla

it

stalking

of both and

silver Saxons great


period

his

when Whale eat

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

suggested the was

so leaves this
feet say of

stories kind

have

spines the shows

its plates

lambs her herbage


Although York winter

the Photo the

throat the

s Pouched

offensive and years

amiable

are hands when

his coast

silvery myself

the or while
all their the

a horse

ship sometimes was

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

uneasily work ground

it of

be become and
horse smallest with

of Finchley

OF

squirrels

S EBRAS
slender a

this to

154

face tamely their

of months
as wild out

the

bear their garden

of

sheep with

going roar
chariots not 132

was Herr iron

thick the The

twisted are

domestic

the even

its

poisonous covering
of into stomachs

equally like

the heavy shorter

look the monkey

case

Pampas

charges what

buck fresh

and first

Mountain a
softer Merchants of

western are

only

and snapshot ANADIAN

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.

More than just a book-buying platform, we strive to be a bridge


connecting you with timeless cultural and intellectual values. With an
elegant, user-friendly interface and a smart search system, you can
quickly find the books that best suit your interests. Additionally,
our special promotions and home delivery services help you save time
and fully enjoy the joy of reading.

Join us on a journey of knowledge exploration, passion nurturing, and


personal growth every day!

ebookgate.com

You might also like