A Byte of Python: Swaroop C H
A Byte of Python: Swaroop C H
Swaroop C H
A Byte of Python
Swaroop C H
Copyright © 2003-2005 Swaroop C H
Abstract
This book will help you to learn the Python programming language, whether you are new to computers
or are an experienced programmer.
This book is released under the Creative Commons Attribution-NonCommercial-ShareAlike License 2.0 .
Table of Contents
Preface ...................................................................................................................... x
Who This Book Is For .......................................................................................... x
History Lesson ................................................................................................... x
Status of the book ................................................................................................ x
Official Website ................................................................................................. xi
License Terms ................................................................................................... xi
Feedback .......................................................................................................... xi
Something To Think About .................................................................................. xi
1. Introduction ............................................................................................................ 1
Introduction ....................................................................................................... 1
Features of Python .............................................................................................. 1
Summary ................................................................................................... 2
Why not Perl? .................................................................................................... 3
What Programmers Say ........................................................................................ 3
2. Installing Python ..................................................................................................... 4
For Linux/BSD users ........................................................................................... 4
For Windows Users ............................................................................................. 4
Summary ........................................................................................................... 5
3. First Steps .............................................................................................................. 6
Introduction ....................................................................................................... 6
Using the interpreter prompt .................................................................................. 6
Choosing an Editor .............................................................................................. 6
Using a Source File ............................................................................................. 7
Output ...................................................................................................... 7
How It Works ............................................................................................. 8
Executable Python programs ................................................................................. 8
Getting Help ...................................................................................................... 9
Summary ......................................................................................................... 10
4. The Basics ............................................................................................................ 11
Literal Constants ............................................................................................... 11
Numbers ......................................................................................................... 11
Strings ............................................................................................................ 11
Variables ......................................................................................................... 13
Identifier Naming .............................................................................................. 13
Data Types ...................................................................................................... 14
Objects ........................................................................................................... 14
Output .................................................................................................... 15
How It Works ........................................................................................... 15
Logical and Physical Lines ................................................................................. 15
Indentation ...................................................................................................... 17
Summary ......................................................................................................... 18
5. Operators and Expressions ....................................................................................... 19
Introduction ..................................................................................................... 19
Operators ......................................................................................................... 19
Operator Precedence .......................................................................................... 21
Order of Evaluation ................................................................................... 22
Associativity ............................................................................................ 22
Expressions ...................................................................................................... 22
Using Expressions ..................................................................................... 22
Summary ......................................................................................................... 23
6. Control Flow ........................................................................................................ 24
Introduction ..................................................................................................... 24
The if statement ................................................................................................ 24
iv
A Byte of Python
v
A Byte of Python
vi
A Byte of Python
vii
List of Tables
5.1. Operators and their usage ...................................................................................... 19
5.2. Operator Precedence ............................................................................................ 21
15.1. Some Special Methods ....................................................................................... 88
viii
List of Examples
3.1. Using the python interpreter prompt .......................................................................... 6
3.2. Using a Source File ............................................................................................... 7
4.1. Using Variables and Literal constants ...................................................................... 14
5.1. Using Expressions ............................................................................................... 22
6.1. Using the if statement .......................................................................................... 24
6.2. Using the while statement ..................................................................................... 26
6.3. Using the for statement ......................................................................................... 27
6.4. Using the break statement ..................................................................................... 29
6.5. Using the continue statement ................................................................................. 30
7.1. Defining a function .............................................................................................. 32
7.2. Using Function Parameters .................................................................................... 33
7.3. Using Local Variables .......................................................................................... 34
7.4. Using the global statement .................................................................................... 35
7.5. Using Default Argument Values ............................................................................. 36
7.6. Using Keyword Arguments ................................................................................... 37
7.7. Using the literal statement ..................................................................................... 38
7.8. Using DocStrings ................................................................................................ 39
8.1. Using the sys module ........................................................................................... 41
8.2. Using a module's __name__ .................................................................................. 43
8.3. How to create your own module ............................................................................. 43
8.4. Using the dir function .......................................................................................... 45
9.1. Using lists .......................................................................................................... 47
9.2. Using Tuples ...................................................................................................... 49
9.3. Output using tuples .............................................................................................. 50
9.4. Using dictionaries ............................................................................................... 51
9.5. Using Sequences ................................................................................................. 53
9.6. Objects and References ........................................................................................ 55
9.7. String Methods ................................................................................................... 56
10.1. Backup Script - The First Version ......................................................................... 58
10.2. Backup Script - The Second Version ..................................................................... 60
10.3. Backup Script - The Third Version (does not work!) ................................................. 62
10.4. Backup Script - The Fourth Version ...................................................................... 63
11.1. Creating a Class ................................................................................................ 68
11.2. Using Object Methods ........................................................................................ 69
11.3. Using the __init__ method ................................................................................... 69
11.4. Using Class and Object Variables ......................................................................... 71
11.5. Using Inheritance .............................................................................................. 73
12.1. Using files ........................................................................................................ 76
12.2. Pickling and Unpickling ...................................................................................... 77
13.1. Handling Exceptions .......................................................................................... 80
13.2. How to Raise Exceptions .................................................................................... 81
13.3. Using Finally .................................................................................................... 82
14.1. Using sys.argv .................................................................................................. 84
15.1. Using List Comprehensions ................................................................................. 89
15.2. Using Lambda Forms ......................................................................................... 90
ix
Preface
Python is probably one of the few programming languages which is both simple and powerful. This is
good for both and beginners as well as experts, and more importantly, is fun to program with. This book
aims to help you learn this wonderful language and show how to get things done quickly and painlessly -
in effect 'The Perfect Anti-venom to your programming problems'.
The aim is that if all you know about computers is how to save text files, then you can learn Python from
this book. If you have previous programming experience, then you can also learn Python from this book.
If you do have previous programming experience, you will be interested in the differences between Py-
thon and your favorite programming language - I have highlighted many such differences. A little warn-
ing though, Python is soon going to become your favorite programming language!
History Lesson
I first started with Python when I needed to write an installer for my software Diamond
[http://www.g2swaroop.net/software/] so that I could make the installation easy. I had to choose
between Python and Perl bindings for the Qt library. I did some research on the web and I came across
an article where Eric S. Raymond, the famous and respected hacker, talked about how Python has be-
come his favorite programming language. I also found out that the PyQt bindings were very good com-
pared to Perl-Qt. So, I decided that Python was the language for me.
Then, I started searching for a good book on Python. I couldn't find any! I did find some O'Reilly books
but they were either too expensive or were more like a reference manual than a guide. So, I settled for
the documentation that came with Python. However, it was too brief and small. It did give a good idea
about Python but was not complete. I managed with it since I had previous programming experience, but
it was unsuitable for newbies.
About six months after my first brush with Python, I installed the (then) latest Red Hat 9.0 Linux and I
was playing around with KWord. I got excited about it and suddenly got the idea of writing some stuff
on Python. I started writing a few pages but it quickly became 30 pages long. Then, I became serious
about making it more useful in a book form. After a lot of rewrites, it has reached a stage where it has
become a useful guide to learning the Python language. I consider this book to be my contribution and
tribute to the open source community.
This book started out as my personal notes on Python and I still consider it in the same way, although
I've taken a lot of effort to make it more palatable to others :)
In the true spirit of open source, I have received lots of constructive suggestions, criticisms and feedback
from enthusiastic readers which has helped me improve this book a lot.
More chapters are planned for the future, such as on wxPython, Twisted and maybe even Boa Construct-
x
Thank You for previewing this eBook
You can read the full version of this eBook in different formats:
To download this full book, simply select the format you desire below