Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu Programming for Non-Programmers ppt
Nội dung xem thử
Mô tả chi tiết
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 http://www.python.org. 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