0% found this document useful (0 votes)
6 views11 pages

A Byte of Python: Swaroop C H

A Byte of Python is a comprehensive guide designed to teach the Python programming language to both beginners and experienced programmers. The book covers various topics, including installation, basic syntax, control flow, functions, and object-oriented programming, and is released under a Creative Commons license. It aims to provide a practical and enjoyable learning experience for readers new to programming or transitioning from other languages.

Uploaded by

hotchat8686
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

A Byte of Python: Swaroop C H

A Byte of Python is a comprehensive guide designed to teach the Python programming language to both beginners and experienced programmers. The book covers various topics, including installation, basic syntax, control flow, functions, and object-oriented programming, and is released under a Creative Commons license. It aims to provide a practical and enjoyable learning experience for readers new to programming or transitioning from other languages.

Uploaded by

hotchat8686
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

A Byte of Python

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

Using the if statement ................................................................................ 24


How It Works ........................................................................................... 25
The while statement ........................................................................................... 26
Using the while statement ........................................................................... 26
The for loop ..................................................................................................... 27
Using the for statement .............................................................................. 27
The break statement ........................................................................................... 28
Using the break statement ........................................................................... 28
The continue statement ....................................................................................... 30
Using the continue statement ....................................................................... 30
Summary ......................................................................................................... 30
7. Functions ............................................................................................................. 32
Introduction ..................................................................................................... 32
Defining a Function ................................................................................... 32
Function Parameters .......................................................................................... 32
Using Function Parameters ......................................................................... 33
Local Variables ................................................................................................ 33
Using Local Variables ................................................................................ 34
Using the global statement .......................................................................... 34
Default Argument Values ................................................................................... 35
Using Default Argument Values .................................................................. 35
Keyword Arguments .......................................................................................... 36
Using Keyword Arguments ......................................................................... 37
The return statement .......................................................................................... 37
Using the literal statement ........................................................................... 38
DocStrings ....................................................................................................... 38
Using DocStrings ...................................................................................... 39
Summary ......................................................................................................... 40
8. Modules ............................................................................................................... 41
Introduction ..................................................................................................... 41
Using the sys module ................................................................................. 41
Byte-compiled .pyc files ..................................................................................... 42
The from..import statement ................................................................................. 42
A module's __name__ ........................................................................................ 42
Using a module's __name__ ........................................................................ 43
Making your own Modules ................................................................................. 43
Creating your own Modules ........................................................................ 43
from..import ............................................................................................. 44
The dir() function .............................................................................................. 45
Using the dir function ................................................................................ 45
Summary ......................................................................................................... 46
9. Data Structures ...................................................................................................... 47
Introduction ..................................................................................................... 47
List ................................................................................................................ 47
Quick introduction to Objects and Classes ..................................................... 47
Using Lists .............................................................................................. 47
Tuple .............................................................................................................. 49
Using Tuples ............................................................................................ 49
Tuples and the print statement ..................................................................... 50
Dictionary ....................................................................................................... 51
Using Dictionaries ..................................................................................... 51
Sequences ........................................................................................................ 53
Using Sequences ....................................................................................... 53
References ....................................................................................................... 54
Objects and References .............................................................................. 55
More about Strings ............................................................................................ 56
String Methods ......................................................................................... 56
Summary ......................................................................................................... 57

v
A Byte of Python

10. Problem Solving - Writing a Python Script ................................................................ 58


The Problem .................................................................................................... 58
The Solution .................................................................................................... 58
First Version ............................................................................................ 58
Second Version ........................................................................................ 60
Third Version ........................................................................................... 61
Fourth Version ......................................................................................... 63
More Refinements ..................................................................................... 64
The Software Development Process ...................................................................... 65
Summary ......................................................................................................... 65
11. Object-Oriented Programming ................................................................................ 67
Introduction ..................................................................................................... 67
The self ........................................................................................................... 67
Classes ............................................................................................................ 68
Creating a Class ........................................................................................ 68
object Methods ................................................................................................. 69
Using Object Methds ................................................................................. 69
The __init__ method .......................................................................................... 69
Using the __init__ method .......................................................................... 69
Class and Object Variables ................................................................................. 70
Using Class and Object Variables ................................................................. 71
Inheritance ....................................................................................................... 73
Using Inheritance ...................................................................................... 73
Summary ......................................................................................................... 75
12. Input/Output ....................................................................................................... 76
Files ............................................................................................................... 76
Using file ................................................................................................ 76
Pickle ............................................................................................................. 77
Pickling and Unpickling ............................................................................. 77
Summary ......................................................................................................... 78
13. Exceptions .......................................................................................................... 79
Errors ............................................................................................................. 79
Try..Except ...................................................................................................... 79
Handling Exceptions .................................................................................. 79
Raising Exceptions ............................................................................................ 80
How To Raise Exceptions ........................................................................... 81
Try..Finally ...................................................................................................... 82
Using Finally ........................................................................................... 82
Summary ......................................................................................................... 83
14. The Python Standard Library .................................................................................. 84
Introduction ..................................................................................................... 84
The sys module ................................................................................................. 84
Command Line Arguments ......................................................................... 84
More sys ................................................................................................. 86
The os module .................................................................................................. 86
Summary ......................................................................................................... 87
15. More Python ....................................................................................................... 88
Special Methods ............................................................................................... 88
Single Statement Blocks ..................................................................................... 88
List Comprehension ........................................................................................... 89
Using List Comprehensions ........................................................................ 89
Receiving Tuples and Lists in Functions ................................................................ 89
Lambda Forms ................................................................................................. 90
Using Lambda Forms ................................................................................. 90
The exec and eval statements ............................................................................... 91
The assert statement .......................................................................................... 91
The repr function .............................................................................................. 91
Summary ......................................................................................................... 92

vi
A Byte of Python

16. What Next? ......................................................................................................... 93


Graphical Software ............................................................................................ 93
Summary of GUI Tools .............................................................................. 94
Explore More ................................................................................................... 94
Summary ......................................................................................................... 95
A. Free/Libré and Open Source Software (FLOSS) .......................................................... 96
B. About ................................................................................................................. 98
Colophon ......................................................................................................... 98
About the Author .............................................................................................. 98
C. Revision History ................................................................................................... 99
Timestamp ....................................................................................................... 99

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'.

Who This Book Is For


This book serves as a guide or tutorial to the Python programming language. It is mainly targeted at
newbies. It is useful for experienced programmers as well.

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.

Status of the book


This book is a work-in-progress. Many chapters are constantly being changed and improved. However,
the book has matured a lot. You should be able to learn Python easily from this book. Please do tell me
if you find any part of the book to be incorrect or incomprehensible.

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:

 HTML (Free /Available to everyone)

 PDF / TXT (Available to V.I.P. members. Free Standard members can


access up to 5 PDF/TXT eBooks per month each month)

 Epub & Mobipocket (Exclusive to V.I.P. members)

To download this full book, simply select the format you desire below

You might also like