Tải bản đầy đủ (.pdf) (491 trang)

Tài liệu Programming for Non-Programmers ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.98 MB, 491 trang )

Programming for Non-Programmers
Release 2.6.2
Steven F. Lott
January 23, 2012

CONTENTS
I How To Write Your Own Software Using Python 1
1 Preface 5
1.1 Why Read This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2 What Is This Book About? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Audience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4 Conventions Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2 Getting Started 11
2.1 About Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2 About Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.3 Let There Be Python: Downloading and Installing . . . . . . . . . . . . . . . . . . . . . . . . 24
2.4 Two Minimally-Geeky Problems : Examples of Things Best Done by Customized Software . 30
2.5 Why Python is So Cool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3 Using Python 39
3.1 Instant Gratification : The Simplest Possible Conversation . . . . . . . . . . . . . . . . . . . 39
3.2 IDLE Time : Using Tools To Be More Productive . . . . . . . . . . . . . . . . . . . . . . . . 48
4 Arithmetic and Expressions 55
4.1 Simple Arithmetic : Numbers and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4.2 Better Arithmetic Through Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
4.3 Extra Functions: math and random . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
4.4 Special Ops : Binary Data and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
4.5 More Advanced Expression Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
5 Programming Essentials 95
5.1 Seeing Results : The print Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
5.2 Turning Python Loose With a Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
5.3 Expressions, Constants and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102


5.4 Assignment Bonus Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
5.5 Can We Get Your Input? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
6 Some Self-Control 117
6.1 Truth and Logic : Boolean Data and Operators . . . . . . . . . . . . . . . . . . . . . . . . . 117
6.2 Making Decisions : The Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 123
6.3 Advanced Logic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
6.4 Processing Only When Necessary : The if Statement . . . . . . . . . . . . . . . . . . . . . . 130
6.5 While We Have More To Do : The for Statement . . . . . . . . . . . . . . . . . . . . . . . . 137
6.6 While We Have More To Do : The while Statement . . . . . . . . . . . . . . . . . . . . . . . 144
6.7 Becoming More Controlling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
i
6.8 Comments and Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
7 Organizing Programs with Function Definitions 165
7.1 Adding New Verbs : The def Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
7.2 Flexibility and Clarity : Optional Parameters, Keyword Arguments . . . . . . . . . . . . . . 177
7.3 A Few More Function Definition Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
8 Getting Our Bearings 197
8.1 Where We’ve Been; Where We’re Going . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
9 Basic Sequential Collections of Data 201
9.1 Collecting Items in Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
9.2 Sequences of Characters : str and Unicode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
9.3 Doubles, Triples, Quadruples : The tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
9.4 Flexible Sequences : The list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
9.5 Common List Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
10 Additional Processing Control Patterns 263
10.1 The Unexpected : The try and except statements . . . . . . . . . . . . . . . . . . . . . . . 263
10.2 Looping Back : Iterators, the for statement and Generators . . . . . . . . . . . . . . . . . . 277
11 More Data Collections 293
11.1 Collecting Items : The set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
11.2 Mappings : The dict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309

11.3 Defining More Flexible Functions with Mappings . . . . . . . . . . . . . . . . . . . . . . . . . 322
11.4 Another Mapping : The defaultdict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
12 Working with Files 335
12.1 External Data and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
12.2 Files, Contexts and Patterns of Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
12.3 File-Related Library Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
13 Data + Processing = Objects 365
13.1 Objects: A Retrospective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
13.2 Defining New Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
14 Modules : The unit of software packaging and assembly 387
14.1 Module Definitions – Adding New Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
14.2 Fixed-Point Numbers : Doing High Finance with decimal . . . . . . . . . . . . . . . . . . . 402
14.3 Time and Date Processing : The time and datetime Modules . . . . . . . . . . . . . . . . . 407
14.4 Text Processing and Pattern Matching : The re Module . . . . . . . . . . . . . . . . . . . . 422
15 Fit and Finish: Complete Programs 433
15.1 Wrapping and Packaging Our Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
15.2 Architectural Patterns – A Family Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
15.3 Professionalism : Additional Tips and Hints . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
16 Appendices 455
16.1 Debugging Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
16.2 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
16.3 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
17 Indices and Tables 479
18 Other Back Matter 481
ii
Bibliography 483
Python Module Index 485
iii
iv
Part I

How To Write Your Own Software
Using Python
1

Programming for Non-Programmers, Release 2.6.2
Legal Notice This work is licensed under a Creative Commons License. You are free
to copy, distribute, display, and perform the work under the following conditions:
• Attribution. You must give the original author, Steven F. Lott, credit.
• Noncommercial. You may not use this work for commercial purposes.
• No Derivative Works. You may not alter, transform, or build upon this work.
For any reuse or distribution, you must make clear to others the license terms of this work.
The Walrus and the Carpenter – Lewis Carroll
“The time has come,” the Walrus said,
“To talk of many things:
Of shoes – and ships – and sealing-wax –
Of cabbages – and kings –
And why the sea is boiling hot –
and whether pigs have wings.”
3
Programming for Non-Programmers, Release 2.6.2
4
CHAPTER
ONE
PREFACE
1.1 Why Read This Book?
You’ll need to read this book when you have the following three things happening at the same time:
• You have a problem to solve that involves data and processing.
• You’ve found that the common desktop tools (word processors, spread sheets, databases, organizers,
graphics) won’t really help. You’ve found that they require too much manual pointing and clicking, or
they don’t do the right kinds of processing on your data.

• You’re ready to invest some of your own time to learn how to write customized software that will solve
your problem.
You’ll want to read this book if you are tinkerer who likes to know how things really work. For many people,
a computer is just an appliance. You may not find this satisfactory, and you want to know more. People
who tinker with computers are called hackers, and you are about to join their ranks.
Python is what you’ve been looking for. It is an easy-to-use tool that can do any kind of processing on any
kind of data. Seriously: any processing, any data. Programming is the term for setting up a computer to
do the processing you define on your data. Once you learn the Python language, you can solve your data
processing problem.
Our objective is to get you, a non-programming newbie, up and running. When you’re done with this book,
you’ll be ready to move on to a more advanced Python book. For example, a book about the Python
libraries. You can use these libraries can help you build high-quality software with a minimum of work.
1.2 What Is This Book About?
This book is about many things. The important topics include Python, programming, languages, data,
processing, and some of the skills that make up the craft of programming. We’ll talk about the core
intellectual tools of abstraction, algorithms and the formality of computer languages. We’ll also touch on
math and logic, statistics, and casino games.
Python. Python is a powerful, flexible toolbox and workbench that can help solve your data processing
problem. If you need to write customized software that does precisely what you want, and you want that
software to be readable, maintainable, adaptable, inexpensive and make best use of your computer, you need
Python.
Programming. When we’ve written a sequence of statements in the Python language, we can then use
that sequence over and over again. We can process different sets of data in a standard, automatic fashion.
We’ve created a program that can automate data processing tasks, replacing tedious or error-prone pointing
5
Programming for Non-Programmers, Release 2.6.2
and clicking in other software tools. Also, we can create programs that do things that other desktop tools
can’t do at all.
The big picture is this: the combination of the Python program plus a unique sequence of Python language
statements that we create can have the effect of creating a new application for our computer. This means

that our application uses the existing Python program as its foundation. The Python program, in turn,
depends on many other libraries and programs on your computer. The whole structure forms a kind of
technology stack, with our program on top, controlling the whole assembly.
Languages. We’ll look at three facets of a programming language: how you write it, what it means, and the
additional practical considerations that make a program useful. We’ll use these three concepts to organize
our presentation of the language. We need to separate these concepts to assure that there isn’t a lot of
confusion between the real meaning and the ways we express that meaning.
The sentences “Xander wrote a tone poem for chamber orchestra” and “The chamber orchestra’s tone poem
was written by Xander” have the same meaning, but express it different ways. They have the same semantics,
but different syntax. For example, in one sentence the verb is “wrote”, in the other sentence it is “was written
by” : different forms of the verb to write. The first form is written in active voice, and second form is called
the passive voice. Pragmatically, the first form is slightly clearer and more easily understood.
The syntax of the Python language is covered here, and in the Python Reference Manual [PythonRef]. Python
syntax is simple, and very much like English. We’ll provide many examples of language syntax. We’ll also
provide additional tips and hints focused on the newbies and non-programmers. Also, when you install
Python, you will also install a Python Tutorial [PythonTut] that presents some aspects of the language, so
you’ll have at least three places to learn syntax.
The semantics of the language specify what a statement really means. We’ll define the semantics of each
statement by showing what it makes the Python program do to your data. We’ll also be able to show
where there are alternative syntax choices that have the same meaning. In addition to semantics being
covered in this book, you’ll be able to read about the meaning of Python statements in the Python Reference
Manual [PythonRef], the Python Tutorial [PythonTut], and chapter two of the Python Library Reference
[PythonLib].
In this book, we’ll try to provide you with plenty of practical advice. In addition to breaking the topic
into bite-sized pieces, we’ll also present lots of patterns for using Python that you can apply to real-world
problems.
Extensions. Part of the Python technology stack are the extension libraries. These libraries are added
onto Python, which has the advantage of keeping the language trim and fit. Software components that you
might need for specialized processing are kept separate from the core language. Plus, you can safely ignore
the components you don’t need.

This means that we actually have two things to learn. First, we’ll learn the language. After that, we’ll look
at a few of the essential libraries. Once we’ve seen that, we can see how to make our own libraries, and our
own application programs.
1.3 Audience
Programming and Computer Skills. We’re going to focus on programming skills, which means we have
to presume that you already have general computer skills. You should fit into one of these populations.
• You have good computer skills, but you want to learn to program. You are our target crew. Welcome
aboard.
• You have some programming experience, and you want to learn Python. You’ll find that most of
Getting Started is something you can probably skim through. We’ve provided some advanced material
that you may find interesting.
6 Chapter 1. Preface
Programming for Non-Programmers, Release 2.6.2
What skills will you need? How will we build up your new skills?
Skills You’ll Need. This book assumes an introductory level of skill with any of the commonly-available
computer systems. Python runs on almost any computer; because of this, we call it platform-independent.
We won’t presume a specific computer or operating system. Some basic skills will be required. If these are
a problem, you’ll need to brush up on these before going too far in this book.
• Can you download and install software from the internet? You may need to do this to get the Python
distribution kit from . If you’ve never downloaded and installed software before,
you may need some help with that skill.
• Do you know how to create text files? We will address doing this using a program called IDLE, the
Python Integrated Development Environment. If you don’t know how to create folders and files, or if
you have trouble finding files you’ve saved on your computer, you’ll need to expand those skills before
trying to do any programming.
• Do you know some basic algebra? Some of the exercises make use of some basic algebra. A few will
compute some statistics. We shouldn’t get past high-school math, and you probably don’t need to
brush up too much on this.
How We Help. Newbie programmers with an interest in Python are our primary audience. We provide
specific help for you in a number of ways.

• Programming is an activity that includes the language skills, but also includes design, debugging and
testing; we’ll help you develop each of these skills.
• We’ll address some of the under-the-hood topics in computers and computing, discussing how things
work and why they work that way. Some things that you’ve probably taken for granted as a user
become more important as you grow to be a programmer.
• We won’t go too far into software engineering and design. We need to provide some hints on how
software gets written, but this is not a book for computer professionals; it’s for computer amateurs
with interesting data or processing needs.
• We cover a few of the most important modules to specifically prevent newbie programmers from
struggling or – worse – reinventing the wheel with each project. We can’t, however, cover too much in
a newbie book. When you’re ready for more information on the various libraries, you’re also ready for
a more advanced Python book.
When you’ve finished with this book you should be able to do the following.
• Use the core language constructs: variables, statements, exceptions, functions and classes. There are
only twenty statements in the language, so this is an easy undertaking.
• Use the Python collection classes to work with more than one piece of data at a time.
• Use a few of the Python extension libraries. We’re only going to look at libraries that help us with
finishing a polished and complete program.
A Note on Clue Absorption. Learning a programming language involves accumulating many new and
closely intertwined concepts. In our experience teaching, coaching and doing programming, there is an upper
limit on the “Clue Absorption Rate”. In order to keep below this limit, we’ve found that it helps to build up
the language as ever-expanding layers. We’ll start with a very tiny, easy to understand subset of statements;
to this we’ll add concepts until we’ve covered the entire Python language and all of the built-in data types.
Our part of the agreement is to do things in small steps. Here’s your part: you learn a language by using it.
In order for each layer to act as a foundation for the following layers, you have to let it solidify by doing small
programming exercises that exemplify the layer’s concepts. Learning Python is no different from learning
Swedish. You can read about Sweden and Swedish, but you must actually use the language to get it off the
page and into your head. We’ve found that doing a number of exercises is the only way to internalize each
1.3. Audience 7
Programming for Non-Programmers, Release 2.6.2

language concept. There is no substitute for hands-on use of Python. You’ll need to follow the examples
and do the exercises. As you can probably tell from this paragraph, we can’t emphasize this enough.
The big difference between learning Python and learning Swedish is that you can immediately interact with
the Python program, doing real work in the Python language. Interacting in Swedish can more difficult.
The point of learning Swedish is to interact with people: for example, buying some kanelbulle (cinnamon
buns) for fika (snack). However, unless you live in Sweden, or have friends or neighbors who speak Swedish,
this interactive part of learning a human language is difficult. Interacting with Python only requires a
working computer, not a trip to Kiruna.
Also, your Swedish phrase-book gives you little useful guidance on how to pronounce words like sked (spoon)
or sju (seven); words which are notoriously tricky for English-speakers to get right. Python, however, is a
purely written language so you don’t have subtleties of pronunciation, you only have spelling and grammar.
1.4 Conventions Used in This Book
Here is how we’ll show Python programs in the rest of the book. The programs will be in separate boxes,
in a different font, often with numbered “callouts” to help explain the program. This example is way too
advanced to read in detail (it’s part of Mappings : The dict) it just shows what examples look like.
Python Example
1 from __future__ import print_function, division
2 combo = { }
3 for i in range(1,7):
4 for j in range(1,7):
5 roll= i+j
6 combo.setdefault( roll, 0 )
7 combo[roll] += 1
8 for n in range(2,13):
9 print("{0:d} {1:.2f}".format(n, combo[n]/36))
Line 1 creates a Python dictionary, a map from key to value. In this case, the key will be a roll, a number
from 2 to 12. The value will be a count of the number of times that roll occurred.
Line 5 assures that the rolled number exists in the dictionary. If it doesn’t exist, it will default, and will be
assigned frequency count of 0.
Line 7 prints each member of the resulting dictionary.

The output from the above program will be shown as follows:
2 0.03%
3 0.06%
4 0.08%
5 0.11%
6 0.14%
7 0.17%
8 0.14%
9 0.11%
10 0.08%
11 0.06%
12 0.03%
Tool completed successfully
8 Chapter 1. Preface
Programming for Non-Programmers, Release 2.6.2
We will use the following type styles for references to a specific Class, method(), attribute, which includes
both class variables or instance variables.
Sidebars
When we do have a digression, it will appear in a sidebar, like this.
Tip: Tips
There will be design tips, and warnings, in the material for each exercise. These reflect considerations and
lessons learned that aren’t typically clear to starting programmers.
1.4. Conventions Used in This Book 9
Programming for Non-Programmers, Release 2.6.2
10 Chapter 1. Preface
CHAPTER
TWO
GETTING STARTED
Tools and Toys
This part provides some necessary background to help non-programming newbies get ready to write their own

programs. If you have good computer skills, this section may be all review. If you are new to programming,
our objective is to build up your skills by providing as complete an introduction as we can. Computing has
a lot of obscure words, and we’ll need some consistent definitions.
In Let There Be Python: Downloading and Installing we’ll describe how to install Python. This is mostly for
folks using Windows. Mac OS X and Linux users will find they already have Python installed. This chapter
has the essential first step in starting to build programs: getting our tools organized.
We’ll describe two typical problems that Python can help us solve in Two Minimally-Geeky Problems :
Examples of Things Best Done by Customized Software. We’ll provide many, many more exercises and
problems than just these two. But these are representative of the problems we’ll tackle.
2.1 About Python
Our goal is to write Python programs to solve problems that involve data and processing. Doing Python
programming involves two things.
1. Designing and writing programs: statements in the Python language that could control a computer
system.
2. Running the Python program to evaluate (or interpret) these statements and actually do the useful
work we imagined. This is the goal.
This leads to the very important distinction:
• Python is a program that does data processing. This is an operational view.
• You control the Python program using the Python programming language. This is a software design
view.
What does this distinction mean? First, there is an opportunity for us to confuse Python (the program) and
Python (the language). We’ll attempt to be as clear as we can on the things the Python program does when
you give it commands in the Python Language. This is the distinction beytween semantics (what Python
does) and syntax (how we spell it).
For people very new to programming, this raises further questions like “what is a programming language?”
and “why can’t it just use English?” and “what if I’m not good with languages?” We’ll return to these
topics in Concepts FAQ’s. For now, we’ll emphasize the point that the Python language is more precise than
English, but still very easy to read and write.
11
Programming for Non-Programmers, Release 2.6.2

The other thing that the distinction between program and language means is that we will focus our efforts
on learning the language. The data processing will be completely defined by a sequence of statements in
the Python language. Learning a computer language isn’t a lot different from learning a human language,
making our job relatively easy. We’ll be reading and writing Python in no time.
We’ll look at the concepts of software design in About Programming.
For now, however, let’s look at the Python the program.
2.1.1 What is a Program?
We’re going to look a closely at these things called “programs”. The concept is multi-faceted, so we’ll have
to look at programs from several points of view. In Software Terminology there’s a kind of road map to
computers and software that may provide helpful background.
The essence of a program is the following:
A program sets up a computer to do a specific task.
We could say that it is a program which applies a general-purpose computer to a specific problem. That’s
why we call them application programs or sometimes just applications; A program applies a generalized
computing appliance to a specific data processing purpose.
There is a kind of parallel between a computer system running programs and a television playing a particular
TV show. Without the program, the computer is just a pile of inert electronics. Similarly, if there is no
TV show, the television just sits there showing a blank screen. (When I was a kid, a TV with no program
showed a flickering “noise” pattern. Modern TV’s don’t do this, they just sit there.)
We will take two differing views of a program: the data side and the processing side. We’re separateing
information from action. We’ll be learning to write programs which read and write files of data, much like
our ordinary desktop tools open and save files. These programs will consume a file of data, the processing
will update an internal state (like a count or a total) and possibly create a resulting file of data.
We aren’t excluding game programs or programs that control physical processes. A game’s input data
includes the control actions from the player (or sensors attached to a device) plus the description of the
game’s levels and environments. The processing that a game does consumes the inputs; based on the current
state, it determines what happens next; and it creates a new game state.
2.1.2 Program Varieties
To understand how a program we write depends on the underlying Python program, we need to make a
distinction between some varieties of programs: specifically, between binary executable and script programs.

A binary executable is a program that is given direct control computer’s processor. We call it binary because
it uses the binary codes specific to the processor chip inside the computer. If you haven’t encountered “binary”
before, see Binary Codes. Most programs that you buy or download fit this description. Most of the office
applications you use are binary executables.
The python program (named python.exe in Windows) is a binary executable. It takes direct control over
the processor.
Note: Other Pythons
There are other varieties of Python, including CPython, Jython and PyPy. We’re focused on CPython.
Jython and PyPy aren’t actually binary exectables, making them somewhat more indirect. This has the
undesirable effect of making the explanation a hair more complex, so we’ll ignore them.
12 Chapter 2. Getting Started
Programming for Non-Programmers, Release 2.6.2
A script, on the other hand, is used to control a program. A script doesn’t take direct control over the
processor. It doesn’t rely (directly) on binary codes. The Python language is a scripting language; it
controls the computer system indirectly, via the Python binary program.
Our programs will be scripts that control the underlying Python program.
Your operating system is a complex collection of binary executables and scripts. These operating system
programs don’t solve any particular problem, but they enable the computer to be used by folks who do have
a particular problem to solve.
A binary executable’s direct control over the processor is beneficial because it gives the best speed and uses
the fewest resources. However, the cost of this control is the relative opacity of the coded instructions that
control the processor chip. The processor instruction codes are focused on the electronic switching arcana
of gates, flip-flops and registers. They are not focused on data processing at a human level. If you want to
see how complex and confusing the processor chip can be, go to Intel or AMD’s web site and download the
technical specifications for one of their processors.
One subtlety that we have to acknowledge is that even the binary applications don’t have complete control
over the entire computer system. A computer system loads a kernel of software when it starts. The parts
we interact with are actually outside this kernel. The binary applications we use do parts of their work by
using the kernel. This important design feature of the operating system assures that all of the application
programs behave consistently and share resources politely.

2.1. About Python 13
Programming for Non-Programmers, Release 2.6.2
Binary Codes
Binary codes were invented by the inhabitants of the planet Binome, the Binome Individual uniTs, or
BITs. These creates had two hands of four fingers each, giving them eight usable digits instead of the
ten that most Earthlings have. Unlike Earthlings, who use their ten fingers to count to ten, the BITs
use only their right hands and can only count to one.
If their hand is down, that’s zero. If they raise their hand, that’s one. They don’t use their left hands
or their fingers. It seems like such a waste, but the BITs have a clever work-around
If a BIT want to count to a larger number, say ten, they recruit three friends. Four BITs can then
chose positions and count to ten with ease. The right-most position is worth 1. The next position to
the left is worth 2. The next position is worth 4, and the last position is worth 8.
The final answer is the sum of the positions with hands in the air.
Say we have BITs named Alpha, Bravo, Charlie and Delta standing around. Alpha is in the first
position, worth only 1, and Delta is in the fourth position, worth 8. If Alpha and Charlie raise their
hands, this is positions worth 1 and 4. The total is 5. If all four BITs raise their hands, it’s 8+4+2+1,
which is 15. Four BITs have 16 different values, from zero (all hands down) to 15 (all hands up).
Delta (8) Charlie (4) Bravo (2) Alpha (1) total
down down down down 0
down down down up 1
down down up down 2
down down up up 2 + 1 = 3
down up down down 4
down up down up 4 + 1 = 5
down up up down 4 + 2 = 6
down up up up 4 + 2 + 1 = 7
up down down down 8
up down down up 8 + 1 = 9
up down up down 8 + 2 = 10
A party of eight BITs can show 256 different values from zero to 255. A group of thirty-two BITs can

count to over 4 billion.
The reason this scheme works is that we only have two values: on and off. This two valued (binary)
system is easy to build into electronic circuits: a component is either on or off. Internally, our processor
chip works in this binary arithmetic scheme because its fast and efficient.
2.1.3 The Python Program and What It Does
In What is a Program? we noted the difference between a binary executable and a script. A binary program
uses codes that are specific to our processor chip and operating system. A script, written in an easy-to-read
language like Python, controls that binary program.
The Python program (python or python.exe) is described as an interpreter or a virtual machine. The
Python program’s job is to read statements in the Python language and execute those statements. For
historical reasons, this process is called “interpreting” the program. You might think that an interpreter
should be translating the program into another language; after all, that’s what human interpreters do.
Software people have bent the meaning of this term, and the process of executing a script is called interpreting.
The term virtual machine summarizes the way that the principle of abstraction is applied. Rather than
write Python programs that are specific to each unique machine (with it’s different devices and chipsets and
memory), Python offers a uniform, easier-to-work-with virtual machine that sits squarely on the back of the
real machine.
Ultimately, everything that happens in a computer is the result of the processor executing it’s internal binary
instructions. In the case of Python, the python program contains a set of binary instructions that will read
the Python-language statements we provided and execute those statements.
14 Chapter 2. Getting Started
Programming for Non-Programmers, Release 2.6.2
2.1.4 Layers of Abstraction
There is a sort of parallel between a computer running a program and a TV playing a particular TV show.
The TV device is the computer and the show is the application software.
At a more detailed view, however, our computer is really a composite concept of computer hardware plus
Operating System. It is really this hardware-plus-software device which runs our application programs.
Our TV metaphor starts to break down. We don’t have to get a kernel TV show that allows our TV to
watch a specific channel. A standard television is complete by itself.
A computer, however, can’t do anything without some software. We start with a kernel of OS software to

help us run our desired application software.
This is a slightly more helpful picture.
Figure 2.1: Layers of Abstraction
Looking More Deeply. When we start to write software, however, we need to be aware of multiple layers
of meaning.
• The computer system running programs – in a broad and general sense – is like a TV set playing a
TV show.
• The computer system running the Operating System program can be viewed as a single device. The
computer hardware combined with the operating system software makes a new, abstract device. The
composite (hardware-plus-software) device runs our application software.
• The computer system plus the Operating System plus the Python program can also be viewed as a
single device. This complex, multi-layered device is what runs the application scripts that we write.
This isn’t very much like TV at all: we never tune to one channel (the operating system) that enables
us to watch another channel (Python) that finally lets us watch the video we made with our own
video camera.
Software builds up in stacks and layers, based on the principle of abstraction. TV simply switches channels.
It looks like computers are so strange that metaphors will only cause more confusion. There aren’t many
things that are like computer systems where the behavior we see is built up from independent pieces. We
can’t really talk about them metaphorically, which makes computers a unique intellectual challenge.
Here’s a picture that shows the abstract Computer-Plus-Operating system. This hardware plus software
combination creates a “virtual” machine. The real machine is controlled by a binary executable. The virtual
machine (hardware plus operating system plus Python) is controlled by our Python-language program.
Bottom Line. When we write Python-language statements, those statements will control the Python
program; the Python program controls the OS kernel; the OS kernel controls our computer. These are the
2.1. About Python 15
Programming for Non-Programmers, Release 2.6.2
Figure 2.2: The Python Virtual Machine
most obvious and influential layers of abstraction. It turns out that there are other parts of this technology
stack, but we can safely ignore them. The OS makes hardware differences largely invisible; and Python
makes many OS differences invisible.

The cost of these layers of indirection is programs that are somewhat slower than those which use the
computer’s internal codes. The benefit is a huge simplification in how we write and use software: we’re freed
from having to understand the electro-techno-mumbo-jumbo of our processor chip and can describe our data
and processing clearly and succinctly.
Note: Additional Factoids
The Python program was written in the C language and then compiled into a binary executable that is
specific for your hardware chip and operating system. That’s why the various distribution files for Python
have names that include “i386” for Intel 80386-compatible chips and “fc9” for Fedora Core 9 GNU/Linux.
While a bit beyond our scope, we’ll talk about this a little in So How Do They Create Binary Executables?.
2.1.5 It’s Turtles All The Way Down
The Iroquois creation myth involved the world carried on the back of a turtle. The turtle swam through the
infinite void of space, surrounded by stars and planets and other spirits.
Think of our Python program as a world. Our program rests on the back of the Python executable, which
is its turtle. The Python program seems to swim through space, directed by our script.
We now know that the Python turtle is actually a world of it’s own. That world is resting on other pieces
of software. This other software is the turtle that supports the Python turtle. In the case of CPython, this
turtle is the standard C-language libraries. In the case of Jython, this turtle is the Java Virtual Machine.
In the case of PyPy, it’s the RPython kernel.
That software is really a world sitting on the back of another turtle. That turtle supporting Python is the
operating system. The OS turtle is actually seveal turtles standing on the back of a kernel. And the kernel
is standing squarely on the back of the hardware turtle. This is the Abstraction principle in action.
We often say this:
16 Chapter 2. Getting Started
Programming for Non-Programmers, Release 2.6.2
It’s turtles all the way down.
2.1.6 Where Are The Programs?
Our programs (and our data) reside in two places. When we’re using a program, it must be stored in
memory. However, memory is volatile, so when we’ve turned our computer off, the program must also
reside in persistent storage (e.g., a disk) somewhere. Since our disks are organized into file systems, we find
programs residing in files.

When we look at the various files on our computer, we’ll see a number of broad categories.
1. Applications or Programs. These are executable files, they will control the computer-plus-operating
system abstract machine. There are two kinds of programs:
(a) Binary Executable programs use the processor chip’s binary codes. We use these, but won’t be
building them.
(b) Script programs use a script language like Python. We’ll build these.
2. Documents. Our OS associates each document with a program. This is a convenient short-cut for us,
and allows us to double-click the document and have the proper program start running.
When we use the Finder’s Get Info to look at the detailed information for an application icon in MacOS or
GNU/Linux, we can see that our application program icons are marked “executable” and the file type will
be “application” . In Windows, a binary executable program must have a file name that ends with .exe (or
.com, but this is rare).
Starting A Program. Our various operating systems give us several user interface actions that will load
a program into memory so that we can start to use it. Since starting a program is the primary purpose of
an operating system, there are many ways to accomplish this.
• Double click an application icon
• Double click a document icon
• Single click something in the dock or task bar
• Click on a Run menu item in the Start menu
• Use the Windows Command Prompt; in GNU/Linux or the MacOS it is called a Terminal. Through
the terminal window we interact with a shell program that allows us to type the name of another
program to have that started.
All of these actions are just different ways to get the operating system to locate the binary executable, load
it into memory and give it the resources to do its unique task.
2.1.7 Running a Python Program
The Python program’s job is to read statements in the Python language and execute those statements.
From the operating system’s point of view, all of our various Python programs and tools are really just the
python program. Let’s pretend you’re running a program you wrote named roulette.py.
When you double click the Python program file you created (usually a file with a name that ends in .py),
something like this happens under the hood. This is the conceptual sequence of events.

1. The OS looks up the binary executable associated with the roulette.py file. This is the Python
program, python or python.exe.
2. The OS loads the binary executable Python program into memory, allocates resources and starts it
running. It uses the kernel to share these resources politely with all other programs.
2.1. About Python 17
Programming for Non-Programmers, Release 2.6.2
3. The OS provides the file name you double-clicked (roulette.py) to the Python program.
4. The Python program reads the roulette.py file and executes the Python language statements it finds.
5. When the statements are finished the Python program has nothing more to do, so it terminates.
6. The OS releases the resources allocated to the Python program.
It turns out that step four can have some sub-steps to it. The Python program doesn’t always do a simple
read of our file of statements. There’s room for a small optimization in this step. Under some circumstances
(see Modules : The unit of software packaging and assembly), Python will create a “compiled” version of our
file to save a little bit of time.
What you’ll observe are files with an extension of .pyc. These are compiled versions of a file. They’re
smaller, and encoded in a way that makes them very easy to read and work with.
2.1.8 Concepts Exercises
1. Inventory Your System.
It helps to inventory the various devices and interfaces on your computer system. Start with the
central processor (which may hang behind the display on some iMac’s), and work your way around
your desktop to identify each part.
Use your word processor to write down all of the computer system parts. You’ll need a folder for your
Python programming projects. Create that folder; this inventory will be the first file in that folder.
2. Get Info/Properties.
Find your web browser application. You may have a desktop shortcut, or a MacOS dock icon, a
Windows start menu icon or a Windows toolbar icon for your browser.
In Windows, you can ask for the properties of an application icon. If it is a short cut, you can use the
Find Target button to locate the real application file. With MacOS, you can use control-click to
get information on a particular icon.
In the MacOS, you can ask for the information about an application icon. In the MacOS Finder, you

can click on an application icon and then use the File Get Info to get information on an icon.
3. Get Info/Properties.
Locate a file you made with your favorite word processor. The first exercise in this section was an
opportunity to make a new document file.
In Windows, you can ask for the properties of a document icon. If it is a short cut, you can use the
Find Target button to locate the real application file. With MacOS, you can use control-click to
get information on a particular icon. The properties name the application that is associated with this
document. In Windows, you can see the Type of File and Opens With information about the file.
Using MacOS, you can ask for information about a document icon. In the MacOS Finder, you can
click on an application icon and then use the File Get Info to get information on an icon. The
information has a Kind description. The Open With label shows the application that will open this
document.
2.2 About Programming
Our job as a programmer is to create programs, which are statements in the Python language. When we run
those programs, they will control our computer system to do the data processing we specified. This chapter
18 Chapter 2. Getting Started
Programming for Non-Programmers, Release 2.6.2
takes a closer look at what a program really is and what is means to “run” a program. This will lead us to
the program named python (or python.exe) and how we control it with statements in the Python language.
2.2.1 What is Programming?
While the coffee-shop description of programming is “how we create programs”, that doesn’t help very much.
We can identify a number of skills that are part of the broadly-defined craft of programming. We’ll stick to
two that are foundational: designing Python statements and debugging problems with those statements.
We take much of our guidance on this from Software Project Management [Royce98]. Royce identifies four
stages of software development, with distinct kinds of activities and skills.
• Inception. We have to start by defining the problem. The central skills you use here are observing
and writing. You need to observe the problem and write a clear, simple description of what is wrong
and how software can be used to fix it. If we clearly state our problem, then all of the rest of the
programming activities are directed at a single goal.
If we aren’t clear on what we’re trying to accomplish, we’re very likely to go astray at this point. It’s

more important to clearly define the problem than it is to try and design software. We’ll get to the
software design in stages. We need the problem defined or we’ll never get anywhere.
We’ll return to this in Inception – Getting the Characters Right.
• Elaboration. We elaborate our solution into solid description of how software will sove the problem.
This is the first part of the design effort.
We move from a description of how software will be used to identifying what we will need to buy,
what we will build and what we will download from the open source community. We use design and
architecture skills to create a solution to our problem. We need to be sure that our elaborated solution
really will solve our problem. We also need to be sure that the cost is appropriate for the value we will
create.
This is rarely shows up as a single good idea for a Python program. Instead, this is often a series of
experiments where we imagine something that would solve the problem, and then try to design a more
complete solution. It takes a lot of practice to imagine something that can be written as a Python
program.
We’ll return to this in Elaboration – Overcoming Obstacles.
• Construction. This is where we create our Python language statements and put them into module
files and script files. Here we are building the solution that we designed during the elaboration stage.
This is also involves design skills; but the design is at a much finer grain than the design work done
during Elaboration.
Construction goes beyond the Python language skills. It includes testing our programs to make sure
they work, and debugging our programs to find out why they don’t work.
• Transition. Our programs have to make the transitions from engineering effort to useful tool. That
means they have to be installed on a computer where they can be used. Here is where the problem we
started with in inception is actually solved by using the software.
We know that we’ve done this phase well when we have a nice file that we can double-click, or run
from the Terminal window that does the job we imagined and solves the original problem.
We’ll return to this in Transition – Installing the Final Product.
We’re going to focus on two skills in this book: creating Python language statements, and debugging problems
when we make mistakes. The language is central. However, techniques for debugging are almost as important
as the language itself.

2.2. About Programming 19

×