My first exposure to computer programming was at school nearly 40 years ago. My math teacher was a fan of computing and he established the first A-Level Computer Science course in the sixth form college. I didn’t take the CS A-Level, as I was committed to Math, Physics, and Chemistry. But my math teacher invited all the scientists to do an informal class in programming, once a week, after hours. It sounded interesting, so I enrolled.
We were introduced to a programming language called CESIL,
1
CESIL a cut-down version of an Assembler language
2
with instructions that had more meaningful names like LOAD, STORE, ADD, and JUMP. We were given green cards on which the instructions and numbers were printed. Next to each instruction was a small oval shape. Beyond that, there was a shape for every letter and numeric value.
Filling in the shapes with a pencil indicated the instructions and data we wanted to use. To make the “job,” work we topped and tailed our card deck with some standard instructions on more cards.
Our card decks were secured with rubber bands and sent off to Manchester University for processing. A week later, we usually (but not always) got our cards back together with a printout of the results. If we were lucky, our trivial programs generated some results. More often, our programs did not work, or did not even compile; that is, the computer did not understand our stumbling attempts to write meaningful program code.
I can’t remember what programs I wrote in those days. Probably calculating squares of integers or factorials or if I was really ambitious, the sine of an angle using Taylor series. Looping (and more often, infinite looping) was a wonderful feature that had to be taken advantage of. Doing something that simply could not be done by humans was fascinating to me.
The challenge of thinking like the computer and of treating the mysterious machine in Manchester as an infallible wizard that must be obeyed—or at least communicated with in its own pedantic, arcane language—sticks in my mind. You could, with some practice, treat the wizard as your very own tireless slave. Those after-hours classes were great and I looked forward to them every week.
Programming was great fun, if you had a certain interest in control, procedure, and systematic thinking. Nearly 40 years later, I still enjoy battling with code. My programming language of choice nowadays is Python.
3
Introducing Python
The Python programming language was created by Dutchman
Guido van Rossum
in the late 1980s [1]. Here is a concise summary of Python from Wikipedia [2]:
Python is a widely used, general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C. The language provides constructs intended to enable clear programs on both a small and large scale.
If you choose to learn Python as your first or your 15th programming language, you are making an excellent choice.
Of all the languages I have used (and I think it is about 15, over the years) Python is my favorite. I can’t say exactly why, and I don’t pretend to be an expert in these matters, but here are some of the things I like about Python:
Programs are not cluttered up with braces ({…}) and semicolons (;).
Python implements structure using indentation (white space) rather than punctuation.
The Python keywords are powerful, limited in number, and do what you expect them to do.
If you can’t work out a way to do something in your code, there is always a library somewhere that does it for you.
You can get an awful lot done with a limited knowledge of the language’
It is this last feature that I like the most.
Lean Python
I freely admit that I don’t know all the features of this wonderful language by heart. In that way, I am a less-than-perfect programmer and I beat myself up about it regularly. I have written about 40,000 lines of Python in the past five years, but I discovered recently that actually, I only need a distinct subset of the language to get things done. I do use all the core elements of the language, of course—lists, dictionaries, objects, and so on—but I don’t (and can’t) memorize all of the standard functions for each element. I haven’t needed them.
I’m looking at a list of the functions and methods for sequences. There are 58 listed in my main Python source book [13]. I have only used 15 of them; I haven’t found a need for the rest.
I call this subset
Lean Python
and it is all you need to know as a beginner and some way beyond.
Now, the code I have written with the
Lean Python
subset of language features means that on occasion, I have written less optimal code. For example, I discovered only recently that there is a
reverse()
function that provides a list in reverse order. Of course there is, and why wouldn’t there be? Needless to say, I had overlooked this neat feature and have written code to access list elements in reverse order more than once.
These things happen to all programmers. In general, we don’t consult the manual unless we have to, so it’s a good idea, every now and then, to review the standard list of features for the language to see what might be useful in the future.
Beyond Lean Python
There are many excellent resources available that provide more comprehensive content than this little book. Web sites I would recommend as essential include these:
python.org
. This is the official site for the Python language, and often the best starting point.
docs.python.org
. This site provides the definitive documentation of the standard Python libraries.
There are several excellent sites that offer free, online tutorials. Of course, I also have my own; visit
leanpy.com
to access it.
Regarding books, there are three that sit on a shelf right above my desk at all times:
Core Python Programming,
by Wesley Chun
The Python Standard Library by Example,
by Doug Hellmann
Python Cookbook,
by Alex Martelli, Anna Ravenscroft, and David Ascher
There are many other excellent books, and you might find better ones, but these are the three that I use myself.
Code Examples in the Book
In this book, you will see quite a lot of example code. Early on you’ll see some small code fragments with some narrative text. All code listings are presented in the
Courier New
font. The shaded text is the code, the unshaded text to the right provides some explanation.
#
# some comments and code # in here
#
myName = ’Paul’
myAge = 21 # if only
| Some explanation appears on the right-hand side. |
Later on you’ll see longer listings and whole programs. These appear in the book as shaded areas. Some listings have line numbers on the left for reference, but the line numbers are not part of the program code. For example:
1 def len(seq):
2 if type(seq) in [list,dict]: # is it a seq?
3 return -1 # if not, fail!
4 nelems=0 # length is zero
5 for elem in seq: # for each elem
6 nelems+=1 # +1 to length
7
8 return nelems # return length
There are also some examples of interactions with the Python command-line shell. The shell gives you the
>>>
prompt
. Here’s an example:
>>> type(23)
<type ’int’>
>>> type(’some text’)
<type ’str’>
>>> c=[1,2,’some more text’]
>>> type(c)
<type ’list’>
The lines are not numbered. The lines without the
>>>
prompt are the outputs printed by the shell.
Target Audience
This book is aimed at three categories of readers:
The experienced programmer
: If you already know a programming language, this book gives you a shortcut to understanding the Python language and some of its design philosophy.
You work in IT and need a programming primer
: You might be a tester who needs to have more informed technical discussions with programmers. Working through the examples will help you to appreciate the challenge of good programming.
First-timer
: You want a first book on programming that you can assimilate quickly to help you decide whether programming is for you.
If you require a full-fat, 1,000-page reference book for the Python language, this book is not for you. If you require a primer, appetizer, or basic reference, this book should satisfy your needs.
What This Book Is
This little book provides a sequential learning guide to a useful and usable subset of the Python programming language. Its scope and content are deliberately limited and based on my own experience of using Python to build interactive web sites (using the
Web2py
web development framework [3]) and many command-line utilities.
This book accompanies the one- and two-day programming courses that I created to help people grasp the basics of a programming language quickly. It isn’t a full language reference book, but a reference for people in the course and for whom the
Lean Python
subset is enough (at least initially).
What This Book Is Not
This book is not intended to be a definitive guide to Python.
Code Comprehension
The initial motivation for writing this book was to help provide nontechnical (i.e., nonprogrammer) testers with an appreciation of programming so they could work more closely with the professional programmers on their teams. Critical to this is the skill I call
code comprehension,
which is your ability to read and understand program code.
Like spoken and written languages, it is usually easier to comprehend written language than write it from scratch. If the book helps you to appreciate and understand written program code, then the book will have succeeded in its first goal.
Python Style Guidelines
One of the most important attributes of code is that it is written to be read by people, not just computers. The Python community gives this goal a high priority. In your own company, you might already have programming or Python guidelines; the Python team have provided some that are widely used [4].
I have tried to follow the guidelines in the sample code and programs. However, in the pocket book format, there is less horizontal space, so sometimes I have had to squeeze code a little to fit it on the page. I tend to use mixed case, e.g.,
addTwoNumbers
in my variable and function names.
4
Some of my code comments, particularly in the early pages, are there to explain what, for example, an assignment does. You would not normally expect to see such “stating the obvious” comments in real code.
“Pythonistas” take the readability goal seriously, and so should you.
There is also a set of design principles you might consult. The Zen of Python sets them out [5]. I’m sure I could have written better examples; if you see an opportunity to improve readability or design, let me know.
Structure
The first seven chapters cover the core features of Python. The later chapters introduce some key libraries and how you can use them to write useful applications.
Chapter
1
introduces the interpreter, the basic syntax of the language, the normal layout, and the conventions of Python. Chapter
2
describes the core Python objects that you will use and need to understand. Chapter
3
sets out how programs are structured and controlled using decisions and loops. Chapter
4
tells you how to get data into and out of your programs with the command line, display, and disk files. Chapter
5
introduces modules that help you to manage your own code and access the thousands of existing libraries. Chapter
6
gives you a flavor for object orientation. Objects and classes are the key building blocks that programmers use. Chapter
7
presents methods for trapping errors and exceptions to allow your programs to be “under control” whatever happens.
Chapter
8
describes how you can use the
unittest
framework to test your code in a professional manner. Chapter
9
introduces libraries allowing you to create a web client and download pages from web sites. Chapter
10
presents regular expressions as the mechanism for more sophisticated searching and pattern-matching. Chapter
11
gives you techniques for creating and using the SQLite relational database for persistent storage. Chapter
12
asks “What Next?” and offers some suggestions for further development of your Python programming skills.
An Appendix contains references to web sites, books and tools, and the Python exception hierarchy. An index is included at the end of the book.
Using Python
Downloading Python
You need to choose a Python version before you download. There are currently two versions:
The example code in this book assumes you are using Version 3. If you use Python Version 2 you will notice a few differences. You can read a discussion of the two Python versions in [6].
Sample Programs Download
All the sample programs have been tested on Windows 8, Ubuntu Linux 13, and my trusty Raspberry Pi running Linux. If you use a Mac, you should not have problems.
External Libraries
A major benefit to using Python is the enormous range of free libraries that are available for use. The vast majority of these libraries can be found on the
PyPI
site [7]. When I last looked, there were 46,554 packaged libraries hosted there.
Depending on your operating system (Windows, Mac or Linux), there are several ways of performing installations of Python libraries. The one I find easiest to use is the PIP installer [19] which works nicely with the PyPI site.
Editing Your Python Code
I recommend using either a language-sensitive editor or the editor that comes with your Python installation.
On Windows, use the IDLE Integrated Development Environment (
IDE
) or perhaps Notepad++.
On Linux, there is a selection of editors—
vi, vim, emacs, gedit
, and so on; I use
gedit
.
On OS X, TextMate works fine, but there are other options.
Feedback, Please!
I am very keen to receive your feedback and experience to enhance the format and content of the book. Give me feedback and I’ll acknowledge you in the next edition.
Any errors or omissions are my fault entirely. Please let me know how I can improve this book. E-mail me at
paul@gerrardconsulting.com
with suggestions or errors.
Downloads, errata, further information, and a reading list can be found on the book’s web site at
leanpy.com
.