Python for Informatics Exploring Information 1st Edition Dr. Charles R Severance 2024 scribd download
Python for Informatics Exploring Information 1st Edition Dr. Charles R Severance 2024 scribd download
com
https://ebookname.com/product/python-for-informatics-
exploring-information-1st-edition-dr-charles-r-severance/
OR CLICK BUTTON
DOWNLOAD EBOOK
https://ebookname.com/product/modernist-informatics-literature-
information-and-the-state-1st-edition-purdon/
https://ebookname.com/product/exploring-with-lewis-and-clark-
charles-floyd/
https://ebookname.com/product/programming-like-a-pro-for-
teens-1st-edition-charles-r-hardnett/
https://ebookname.com/product/an-introduction-to-derivatives-and-
risk-management-eighth-edition-brooks/
Java Database Best Practices 1st Edition George Reese
https://ebookname.com/product/java-database-best-practices-1st-
edition-george-reese/
https://ebookname.com/product/ownership-and-appropriation-1st-
edition-veronica-strang-editor/
https://ebookname.com/product/radical-self-love-a-guide-to-
loving-yourself-living-your-dream-1st-edition-gala-darling/
https://ebookname.com/product/beyond-slavery-overcoming-its-
religious-and-sexual-legacies-1st-edition-bernadette-j-brooten-
eds/
https://ebookname.com/product/attached-to-dispossession-
sacrificial-narratives-in-post-imperial-europe-sacrificial-
narratives-in-post-imperial-europe-1st-edition-vladimir-biti/
The Non Geometric Lenin Essays on the Development of
the Bolshevik Party 1910 1914 First Edition Carter
Elwood
https://ebookname.com/product/the-non-geometric-lenin-essays-on-
the-development-of-the-bolshevik-party-1910-1914-first-edition-
carter-elwood/
Python for Informatics
Exploring Information
Version 0.0.4
Charles Severance
Copyright © 2009, 2010 Charles Severance.
Printing history:
December 2009: Begin to produce Python for Informatics: Exploring Information by re-mixing
Think Python: How to Think Like a Computer Scientist
June 2008: Major revision, changed title to Think Python: How to Think Like a Computer Scientist.
August 2007: Major revision, changed title to How to Think Like a (Python) Programmer.
April 2002: First edition of How to Think Like a Computer Scientist.
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. This
license is available at creativecommons.org/licenses/by-sa/3.0/.
The original form of this book is LATEX source code. Compiling this LATEX source has the effect
of generating a device-independent representation of a textbook, which can be converted to other
formats and printed.
The LATEX source for the Think Python: How to Think Like a Computer Scientist version of this book
is available from http://www.thinkpython.com.
The LATEX source for the Python for Informatics: Exploring Information version of the book is avail-
able from http://source.sakaiproject.org/contrib/csev/trunk/pyinf/.
The cover image shows social connectivity of NSF grant investigators at the University of Michigan
from September 1999 through October 2010 and was provided by Eric Hofer and visualized using
the GUESS software developed by Eytan Adar, both of the University of Michigan. The cover design
is by Terri Geitgey of the University of Michigan Library.
Preface
In chapters 11-14, nearly all of the material is brand new, focusing on real-world uses and
simple examples of Python for data analysis including automating tasks on your computer,
retrieving data across the network, scraping web pages for data, using web services, parsing
XML data, and creating and using databases using Structured Query Language.
The ultimate goal of all of these changes is a shift from a Computer Science to an Infor-
matics focus is to only include topics into a first technology class that can be applied even
if one chooses not to become a professional programmer.
Students who find this book interesting and want to further explore should look at Allen
B. Downey’s Think Python book. Because there is a lot of overlap between the two books,
students will quickly pick up skills in the additional areas of computing in general and
computational thinking that are covered in Think Python. And given that the books have a
similar writing style and at times have identical text and examples, you should be able to
move quickly through Think Python with a minimum of effort.
As the copyright holder of Think Python, Allen has given me permission to change the
book’s license from the GNU Free Documentation License to the more recent Creative
Commons Attribution — Share Alike license. This follows a general shift in open doc-
umentation licenses moving from the GFDL to the CC-BY-SA (i.e. Wikipedia). Using
the CC-BY-SA license maintains the book’s strong copyleft tradition while making it even
more straightforward for new authors to reuse this material as they see fit.
I feel that this book serves an example of why open materials are so important to the future
of education, and want to thank Allen B. Downey and Cambridge University Press for their
forward looking decision to make the book available under an open Copyright. I hope they
are pleased with the results of my efforts and I hope that you the reader are pleased with
our collective efforts.
Charles Severance
www.dr-chuck.com
Ann Arbor, MI, USA
July 25, 2011
In January 1999 I was preparing to teach an introductory programming class in Java. I had
taught it three times and I was getting frustrated. The failure rate in the class was too high
and, even for students who succeeded, the overall level of achievement was too low.
vii
One of the problems I saw was the books. They were too big, with too much unnecessary
detail about Java, and not enough high-level guidance about how to program. And they all
suffered from the trap door effect: they would start out easy, proceed gradually, and then
somewhere around Chapter 5 the bottom would fall out. The students would get too much
new material, too fast, and I would spend the rest of the semester picking up the pieces.
Two weeks before the first day of classes, I decided to write my own book. My goals were:
• Keep it short. It is better for students to read 10 pages than not read 50 pages.
• Be careful with vocabulary. I tried to minimize the jargon and define each term at
first use.
• Build gradually. To avoid trap doors, I took the most difficult topics and split them
into a series of small steps.
Preface v
2.6 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.7 Order of operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.8 Modulus operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.9 String operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.10 Asking the user for input . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.11 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.12 Choosing mnemonic variable names . . . . . . . . . . . . . . . . . . . . 23
2.13 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.14 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.15 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3 Conditional execution 29
3.1 Boolean expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.2 Logical operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.3 Conditional execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.4 Alternative execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.5 Chained conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.6 Nested conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.7 Catching exceptions using try and except . . . . . . . . . . . . . . . . . . 34
3.8 Short circuit evaluation of logical expressions . . . . . . . . . . . . . . . 35
3.9 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.10 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
3.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
4 Functions 41
4.1 Function calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.2 Built-in functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.3 Type conversion functions . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.4 Random numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Contents xi
5 Iteration 55
5.1 Updating variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
5.2 The while statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
5.3 Infinite loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
5.4 “Infinite loops” and break . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.5 Finishing iterations with continue . . . . . . . . . . . . . . . . . . . . . 59
5.6 Definite loops using for . . . . . . . . . . . . . . . . . . . . . . . . . . 59
5.7 Loop patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
5.8 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
5.9 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
5.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
6 Strings 65
6.1 A string is a sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
6.2 Getting the length of a string using len . . . . . . . . . . . . . . . . . . . 66
6.3 Traversal through a string with a for loop . . . . . . . . . . . . . . . . . 66
6.4 String slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.5 Strings are immutable . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
xii Contents
6.6 Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
6.7 Looping and counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
6.8 The in operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
6.9 String comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
6.10 string methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
6.11 Parsing strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
6.12 Format operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
6.13 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
6.14 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
6.15 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
7 Files 81
7.1 Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
7.2 Opening files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
7.3 Text files and lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
7.4 Reading files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
7.5 Searching through a file . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
7.6 Letting the user choose the file name . . . . . . . . . . . . . . . . . . . . 87
7.7 Using try, catch, and open . . . . . . . . . . . . . . . . . . . . . . . 88
7.8 Writing files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
7.9 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
7.10 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
7.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
8 Lists 93
8.1 A list is a sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
8.2 Lists are mutable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
8.3 Traversing a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
8.4 List operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Contents xiii
9 Dictionaries 109
9.1 Dictionary as a set of counters . . . . . . . . . . . . . . . . . . . . . . . 111
9.2 Dictionaries and files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
9.3 Looping and dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . 113
9.4 Advanced text parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
9.5 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
9.6 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
9.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
10 Tuples 119
10.1 Tuples are immutable . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
10.2 Comparing tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
10.3 Tuple assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
10.4 Dictionaries and tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
10.5 Multiple assignment with dictionaries . . . . . . . . . . . . . . . . . . . 123
10.6 The most common words . . . . . . . . . . . . . . . . . . . . . . . . . . 124
10.7 Using tuples as keys in dictionaries . . . . . . . . . . . . . . . . . . . . . 126
xiv Contents
A Debugging 187
A.1 Syntax errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
A.2 Runtime errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
A.3 Semantic errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Writing programs (or programming) is a very creative and rewarding activity. You can
write programs for many reasons ranging from making your living to solving a difficult
data analysis problem to having fun to helping someone else solve a problem. This book
assumes that everyone needs to know how to program and that once you know how to
program, you will figure out what you want to do with your newfound skills.
We are surrounded in our daily lives with computers ranging from laptops to cell phones.
We can think of these computers as our “personal assistants” who can take care of many
things on our behalf. The hardware in our current-day computers is essentially built to
continuously ask us the question, “What would you like me to do next?”.
Programmers add an operating system and a set of applications to the hardware and we
end up with a Personal Digital Assistant that is quite helpful and capable of helping many
different things.
Our computers are fast and have vast amounts of memory and could be very helpful to us
if we only knew the language to speak to explain to the computer what we would like it to
“do next”. If we knew this language we could tell the computer to do tasks on our behalf
that were repetitive. Interestingly, the kinds of things computers can do best are often the
kinds of things that we humans find boring and mind-numbing.
2 Chapter 1. Why should you learn to write programs?
For example, look at the first three paragraphs of this chapter and tell me the most com-
monly used word and how many times the word is used. While you were able to read and
understand the words in a few seconds, counting them is almost painful because it is not
the kind of problem that human minds are designed to solve. For a computer the opposite
is true, reading and understanding text from a piece of paper is hard for a computer to do
but counting the words and telling you how many times the most used word was used is
very easy for the computer:
python words.py
Enter file:words.txt
to 16
Our “personal information analysis assistant” quickly told us that the word “to” was used
sixteen times in the first three paragraphs of this chapter.
This very fact that computers are good at things that humans are not is why you need to
become skilled at talking “computer language”. Once you learn this new language, you can
delegate mundane tasks to your partner (the computer), leaving more time for you to do the
things that you are uniquely suited for. You bring creativity, intuition, and inventiveness to
this partnership.
For now, our primary motivation is not to make money or please end-users, but instead
for us to be more productive in handling the data and information that we will encounter
in our lives. When you first start, you will be both the programmer and end-user of your
1.2. Computer hardware architecture 3
programs. As you gain skill as a programmer and programming feels more creative to you,
your thoughts may turn toward developing programs for others.
What
Software Next?
Central
Input Processing
Output Network
Unit
Devices
Main
Secondary
Memory
Memory
• The Central Processing Unit (or CPU) is that part of the computer that is built to be
obsessed with “what is next?”. If your computer is rated at 3.0 Gigahertz, it means
that the CPU will ask “What next?” three billion times per second. You are going to
have to learn how to talk fast to keep up with the CPU.
• The Main Memory is used to store information that the CPU needs in a hurry. The
main memory is nearly as fast as the CPU. But the information stored in the main
memory vanishes when the computer is turned off.
• The Secondary Memory is also used to store information, but it is much slower
than the main memory. The advantage of the secondary memory is that it can store
information even when there is no power to the computer. Examples of secondary
memory are disk drives or flash memory (typically found in USB sticks and portable
music players).
• The Input and Output Devices are simply our screen, keyboard, mouse, micro-
phone, speaker, touchpad, etc. They are all of the ways we interact with the com-
puter.
4 Chapter 1. Why should you learn to write programs?
• These days, most computers also have a Network Connection to retrieve informa-
tion over a network. We can think of the network as a very slow place to store and
retrieve data that might not always be “up”. So in a sense, the network is a slower
and at times unreliable form of Secondary Memory
While most of the detail of how these components work is best left to computer builders, it
helps to have a some terminology so we can talk about these different parts as we write our
programs.
As a programmer, your job is to use and orchestrate each of these resources to solve the
problem that you need solving and analyze the data you need. As a programmer you will
mostly be “talking” to the CPU and telling it what to do next. Sometimes you will tell the
CPU to use the main memory, secondary memory, network, or the input/output devices.
What
Software Next?
Central
Input Processing
Output Network
Unit
Devices
Main
Secondary
Memory
Memory
You
You need to be the person who answers the CPU’s “What next?” question. But it would be
very uncomfortable to shrink you down to 5mm tall and insert you into the computer just
so you could issue a command three billion times per second. So instead, you must write
down your instructions in advance. We call these stored instructions a program and the act
of writing these instructions down and getting the instructions to be correct programming.
• First you need to know the programming language (Python) - you need to know the
vocabulary and the grammar. You need to be able spell the words in this new lan-
guage properly and how to construct well-formed “sentences” in this new languages.
• Second you need to “tell a story”. In writing a story, you combine words and sen-
tences to convey an idea to the reader. There is a skill and art in constructing the story
and skill in story writing is improved by doing some writing and getting some feed-
back. In programming, our program is the “story” and the problem you are trying to
solve is the “idea”.
Once you learn one programming language such as Python, you will find it much easier to
learn a second programming language such as JavaScript or C++. The new programming
language has very different vocabulary and grammar but once you learn problem solving
skills, they will be the same across all programming languages.
You will learn the “vocabulary” and “sentences” of Python pretty quickly. It will take
longer for you to be able to write a coherent program to solve a brand new problem. We
teach programming much like we teach writing. We start reading and explaining programs
and then we write simple programs and then write increasingly complex programs over
time. At some point you “get your muse” and see the patterns on your own and can see
more naturally how to take a problem and write a program that solves that problem. And
once you get to that point, programming becomes a very pleasant and creative process.
We start with the vocabulary and structure of Python programs. Be patient as the simple
examples remind you of when you started reading for the first time.
Two kinds of programs process high-level languages into low-level languages: inter-
preters and compilers. An interpreter reads a high-level program and executes it, meaning
that it does what the program says. It processes the program a little at a time, alternately
reading lines and performing computations.
A compiler reads the program and translates it completely before the program starts run-
ning. In this context, the high-level program is called the source code, and the translated
program is called the object code, machine code or the executable. Once a program is
compiled, you can execute it repeatedly without further translation.
>>> 1 + 1
2
>>>
The chevron, >>>, is the prompt the interpreter uses to indicate that it is ready. If you type
1 + 1, the interpreter replies 2. The chevron is the Python interpreter’s way of asking you,
“What do you want me to do next?”. You will notice that as soon as Python finishes one
statement it immediately is ready for you to type another statement.
Typing commands into the Python interpreter is a great way to experiment with Python’s
features, but it is a bad way to type in many commands to solve a more complex problem.
When we want to write a program, we use a text editor to write the Python instructions into
a file, which is called a script. By convention, Python scripts have names that end with
.py.
To execute the script, you have to tell the interpreter the name of the file. In a UNIX or
Windows command window, you would type python dinsdale.py. In other development
environments, the details of executing scripts are different. You can find instructions for
your environment at the Python Website python.org.
1.5. What is a program? 7
Working in interactive mode is convenient for testing small pieces of code because you can
type and execute them immediately. But for anything more than a few lines, you should
save your code as a script so you can modify and execute it in the future.
The details look different in different languages, but a few basic instructions appear in just
about every language:
input: Get data from the keyboard, a file, or some other device, pausing if necessary.
output: Display data on the screen or send data to a file or other device.
sequential execution: Perform statements one after another in the order they are encoun-
tered in the script.
conditional execution: Check for certain conditions and execute or skip a sequence of
statements.
repeated execution: Perform some set of statements repeatedly, usually with some varia-
tion.
reuse: Write a set of instructions once and give them a name and then reuse those instruc-
tions as needed throughout your program.
Believe it or not, that’s pretty much all there is to it. Every program you’ve ever used, no
matter how complicated, is made up of instructions that look pretty much like these. So you
can think of programming as the process of breaking a large, complex task into smaller and
smaller subtasks until the subtasks are simple enough to be performed with one of these
basic instructions.
Three kinds of errors can occur in a program: syntax errors, runtime errors, and semantic
errors. It is useful to distinguish between them in order to track them down more quickly.
8 Chapter 1. Why should you learn to write programs?
can predict the result of the modification, and you take a step closer to a working program.
If your hypothesis was wrong, you have to come up with a new one. As Sherlock Holmes
pointed out, “When you have eliminated the impossible, whatever remains, however im-
probable, must be the truth.” (A. Conan Doyle, The Sign of Four)
For some people, programming and debugging are the same thing. That is, programming
is the process of gradually debugging a program until it does what you want. The idea is
that you should start with a program that does something and make small modifications,
debugging them as you go, so that you always have a working program.
For example, Linux is an operating system that contains thousands of lines of code, but it
started out as a simple program Linus Torvalds used to explore the Intel 80386 chip. Ac-
cording to Larry Greenfield, “One of Linus’s earlier projects was a program that would
switch between printing AAAA and BBBB. This later evolved to Linux.” (The Linux
Users’ Guide Beta Version 1).
Later chapters will make more suggestions about debugging and other programming prac-
tices.
Formal languages tend to have strict rules about syntax. For example, 3 + 3 = 6 is a syntac-
tically correct mathematical statement, but 3 + +3@6 is not. H2 O is a syntactically correct
chemical formula, but 2 Zz is not.
Syntax rules come in two flavors, pertaining to tokens and structure. Tokens are the basic
elements of the language, such as words, numbers, and chemical elements. One of the
problems with 3 + +3@6 is that @ is not a legal token in mathematics (at least as far as I
know). Similarly, 2 Zz is not legal because there is no element with the abbreviation Zz.
The second type of syntax error pertains to the structure of a statement; that is, the way the
tokens are arranged. The statement 3 + +3@6 is illegal because even though + is a legal
10 Chapter 1. Why should you learn to write programs?
token and you can have more than one plus in an exression, you cannot have one right after
another. Similarly, in a chemical formula the subscript comes after the element name, not
before.
Exercise 1.1 Write a well-structured English sentence with invalid tokens in it. Then write
another sentence with all valid tokens but with invalid structure.
When you read a sentence in English or a statement in a formal language, you have to
figure out what the structure of the sentence is (although in a natural language you do this
subconsciously). This process is called parsing.
For example, when you hear the sentence, “The penny dropped,” you understand that “the
penny” is the subject and “dropped” is the predicate. Once you have parsed a sentence, you
can figure out what it means, or the semantics of the sentence. Assuming that you know
what a penny is and what it means to drop, you will understand the general implication of
this sentence.
Although formal and natural languages have many features in common—tokens, structure,
syntax, and semantics—there are some differences:
ambiguity: Natural languages are full of ambiguity, which people deal with by using con-
textual clues and other information. Formal languages are designed to be nearly or
completely unambiguous, which means that any statement has exactly one meaning,
regardless of context.
redundancy: In order to make up for ambiguity and reduce misunderstandings, natural
languages employ lots of redundancy. As a result, they are often verbose. Formal
languages are less redundant and more concise.
literalness: Natural languages are full of idiom and metaphor. If I say, “The penny
dropped,” there is probably no penny and nothing dropping1 . Formal languages mean
exactly what they say.
People who grow up speaking a natural language—everyone—often have a hard time ad-
justing to formal languages. In some ways, the difference between formal and natural
language is like the difference between poetry and prose, but more so:
Poetry: Words are used for their sounds as well as for their meaning, and the whole poem
together creates an effect or emotional response. Ambiguity is not only common but
often deliberate.
Prose: The literal meaning of words is more important, and the structure contributes more
meaning. Prose is more amenable to analysis than poetry but still often ambiguous.
Programs: The meaning of a computer program is unambiguous and literal, and can be
understood entirely by analysis of the tokens and structure.
1 This idiom means that someone realized something after a period of confusion.
1.8. The first program 11
Here are some suggestions for reading programs (and other formal languages). First, re-
member that formal languages are much more dense than natural languages, so it takes
longer to read them. Also, the structure is very important, so it is usually not a good idea
to read from top to bottom, left to right. Instead, learn to parse the program in your head,
identifying the tokens and interpreting the structure. Finally, the details matter. Small er-
rors in spelling and punctuation, which you can get away with in natural languages, can
make a big difference in a formal language.
This is an example of a print statement2 , which doesn’t actually print anything on paper.
It displays a value on the screen. In this case, the result is the words
Hello, World!
The quotation marks in the program mark the beginning and end of the text to be displayed;
they don’t appear in the result.
Some people judge the quality of a programming language by the simplicity of the “Hello,
World!” program. By this standard, Python does about as well as possible.
1.9 Debugging
It is a good idea to read this book in front of a computer so you can try out the examples as
you go. You can run most of the examples in interactive mode, but if you put the code into
a script, it is easier to try out variations.
Whenever you are experimenting with a new feature, you should try to make mistakes.
For example, in the “Hello, world!” program, what happens if you leave out one of the
quotation marks? What if you leave out both? What if you spell print wrong?
This kind of experiment helps you remember what you read; it also helps with debugging,
because you get to know what the error messages mean. It is better to make mistakes now
and on purpose than later and accidentally.
Programming, and especially debugging, sometimes brings out strong emotions. If you are
struggling with a difficult bug, you might feel angry, despondent or embarrassed.
2 In Python 3.0, print is a function, not a statement, so the syntax is print(’Hello, World!’). We will get
to functions soon!
12 Chapter 1. Why should you learn to write programs?
There is evidence that people naturally respond to computers as if they were people3 . When
they work well, we think of them as teammates, and when they are obstinate or rude, we
respond to them the same way we respond to rude, obstinate people.
Preparing for these reactions might help you deal with them. One approach is to think of
the computer as an employee with certain strengths, like speed and precision, and particular
weaknesses, like lack of empathy and inability to grasp the big picture.
Your job is to be a good manager: find ways to take advantage of the strengths and mitigate
the weaknesses. And find ways to use your emotions to engage with the problem, without
letting your reactions interfere with your ability to work effectively.
Learning to debug can be frustrating, but it is a valuable skill that is useful for many activi-
ties beyond programming. At the end of each chapter there is a debugging section, like this
one, with my thoughts about debugging. I hope they help!
1.10 Glossary
central processing unit: The heart of any computer. It is what runs the software that we
write; also called “CPU” or “the processor”.
main memory: Stores programs and data. Main memory loses its information when the
power is turned off.
secondary memory: Stores programs and data and retains its information even when the
power is turned off. Generally slower than main memory. Examples of secondary
memory include disk drives and flash member in USB sticks.
problem solving: The process of formulating a problem, finding a solution, and express-
ing the solution.
high-level language: A programming language like Python that is designed to be easy for
humans to read and write.
machine code: The lowest level language for software which is the language that is di-
rectly executed by the central processing unit (CPU).
portability: A property of a program that can run on more than one kind of computer.
object code: The output of the compiler after it translates the program.
executable: Another name for object code that is ready to be executed.
prompt: Characters displayed by the interpreter to indicate that it is ready to take input
from the user.
script: A program stored in a file (usually one that will be interpreted).
interactive mode: A way of using the Python interpreter by typing commands and expres-
sions at the prompt.
script mode: A way of using the Python interpreter to read and execute statements in a
script.
program: A set of instructions that specifies a computation.
semantic error: An error in a program that makes it do something other than what the
programmer intended.
natural language: Any one of the languages that people speak that evolved naturally.
formal language: Any one of the languages that people have designed for specific pur-
poses, such as representing mathematical ideas or computer programs; all program-
ming languages are formal languages.
token: One of the basic elements of the syntactic structure of a program, analogous to a
word in a natural language.
1.11 Exercises
Exercise 1.2 Use a web browser to go to the Python Website python.org. This page
contains information about Python and links to Python-related pages, and it gives you the
ability to search the Python documentation.
For example, if you enter print in the search window, the first link that appears is the
documentation of the print statement. At this point, not all of it will make sense to you,
but it is good to know where it is.
Exercise 1.3 Start the Python interpreter and type help() to start the online help utility.
Or you can type help('print') to get information about the print statement.
If this example doesn’t work, you may need to install additional Python documentation or
set an environment variable; the details depend on your operating system and version of
Python.
Exercise 1.4 Start the Python interpreter and use it as a calculator. Python’s syntax for
math operations is almost the same as standard mathematical notation. For example, the
symbols +, - and / denote addition, subtraction and division, as you would expect. The
symbol for multiplication is *.
If you run a 10 kilometer race in 43 minutes 30 seconds, what is your average time per
mile? What is your average speed in miles per hour? (Hint: there are 1.61 kilometers in a
mile).
Chapter 2
These values belong to different types: 2 is an integer, and 'Hello, World!' is a string,
so-called because it contains a “string” of letters. You (and the interpreter) can identify
strings because they are enclosed in quotation marks.
The print statement also works for integers. We use the python command to start the
interpreter.
python
>>> print 4
4
If you are not sure what type a value has, the interpreter can tell you.
Not surprisingly, strings belong to the type str and integers belong to the type int. Less
obviously, numbers with a decimal point belong to a type called float, because these
numbers are represented in a format called floating-point.
16 Chapter 2. Variables, expressions and statements
>>> type(3.2)
<type 'float'>
What about values like '17' and '3.2'? They look like numbers, but they are in quotation
marks like strings.
>>> type('17')
<type 'str'>
>>> type('3.2')
<type 'str'>
They’re strings.
When you type a large integer, you might be tempted to use commas between groups of
three digits, as in 1,000,000. This is not a legal integer in Python, but it is legal:
Well, that’s not what we expected at all! Python interprets 1,000,000 as a comma-
separated sequence of integers, which it prints with spaces between.
This is the first example we have seen of a semantic error: the code runs without producing
an error message, but it doesn’t do the “right” thing.
2.2 Variables
One of the most powerful features of a programming language is the ability to manipulate
variables. A variable is a name that refers to a value.
This example makes three assignments. The first assigns a string to a new variable named
message; the second assigns the integer 17 to n; the third assigns the (approximate) value
of π to pi.
A common way to represent variables on paper is to write the name with an arrow pointing
to the variable’s value. This kind of figure is called a state diagram because it shows what
state each of the variables is in (think of it as the variable’s state of mind). This diagram
shows the result of the previous example:
2.3. Variable names and keywords 17
You might want to keep this list handy. If the interpreter complains about one of your
variable names and you don’t know why, see if it is on this list.
2.4 Statements
A statement is a unit of code that the Python interpreter can execute. We have seen two
kinds of statements: print and assignment.
When you type a statement in interactive mode, the interpreter executes it and displays the
result, if there is one.
A script usually contains a sequence of statements. If there is more than one statement, the
results appear one at a time as the statements execute.
For example, the script
print 1
x = 2
print x
1
2
>>> minute = 59
>>> minute/60
0
The value of minute is 59, and in conventional arithmetic 59 divided by 60 is 0.98333, not
0. The reason for the discrepancy is that Python is performing floor division2 .
When both of the operands are integers, the result is also an integer; floor division chops
off the fraction part, so in this example it rounds down to zero.
>>> minute/60.0
0.98333333333333328
2.6 Expressions
An expression is a combination of values, variables, and operators. A value all by itself
is considered an expression, and so is a variable, so the following are all legal expressions
(assuming that the variable x has been assigned a value):
17
x
x + 17
If you type an expression in interactive mode, the interpreter evaluates it and displays the
result:
>>> 1 + 1
2
But in a script, an expression all by itself doesn’t do anything! This is a common source of
confusion for beginners.
Exercise 2.1 Type the following statements in the Python interpreter to see what they do:
5
x = 5
x + 1
2 In Python 3.0, the result of this division is a float. In Python 3.0, the new operator // performs integer
division.
Another Random Document on
Scribd Without Any Related Topics
yhä kiihkeämmäksi eikä pysähtynyt, ennenkuin itse oli tästä
tavattomasta hakkaamisesta lopen uupunut.
Talon ovi oli auki. Hölppä painui sisälle ja hänen perässään tulla
töytäsi kiukun vimmassa koko vihainen, kostonjanoinen lauma, joka
hänet auttamattomasti olisi repinyt kappaleiksi, jolleivät miehet olisi
kavahtaneet pystyyn katsomaan tätä äkkiarvaamatonta hyökkäystä
ja ajamaan kaakattavaa laumaa pakosalle.
*****
*****
Joka päivä joi hän enemmän ja hänen paha tuulensa yltyi. Pyry
ärisi jo hänelle vihaisesti, juopot potkivat häntä hieman takapuoleen
ja Mirri raapaisi häntä kynsillään joitakin kertoja tosissaan.
Nyt hän ei enää tahtonut juoda muuta kuin viiniä, ja kun joskus
joku juopottelija tarjosi hänelle huvikseen vesilasin, kasteli hän siinä
nokkansa; näykkäsi sitten hetkellisen mielialansa vallassa
suutuksissaan aikalailla tarjoojaa tästä huonosta pilasta tai kaasi
päällään ja kaulallaan tämän lasin äkkiä kumoon.
*****
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.