Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions




Building Skills in Python

A Programmer's Introduction to Python

Steven F. Lott

Creative Commons License; some rights reserved.

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.


Table of Contents

Why Read This Book?
Organization of This Book
Programming Style
Conventions Used in This Book
I. Language Basics
1. Background and History
Features of Python
2. Python Installation
Windows Installation
Macintosh Installation
GNU/Linux and UNIX Overview
YUM Installation
RPM Installation
"Build from Scratch" Installation
3. Getting Started
Command-Line Interaction
The IDLE Development Environment
Script Mode
Syntax Formalities
Other Tools
Style Notes: Wise Choice of File Names
4. Simple Numeric Expressions and Output
The print Statement
Numeric Types and Operators
Numeric Conversion Functions
Built-In Functions
Expression Exercises
Expression Style Notes
5. Advanced Expressions
Using Modules
The math Module
The random Module
Advanced Expression Exercises
Bit Manipulation Operators
Division Operators
6. Variables, Assignment and Input
The Assignment Statement
Input Functions
Multiple Assignment Statement
The del Statement
Interactive Mode Revisited
Variable and Assignment Exercises
Variables and Assignment Style Notes
7. Truth, Comparison and Conditional Processing
Truth and Logic
Conditional Processing: the if Statement
The pass Statement
The assert Statement
The if-else Operator
Condition Exercises
Condition Style Notes
8. Looping
Iterative Processing: For All and There Exists
Iterative Processing: The for Statement
Iterative Processing: The while Statement
More Iteration Control: break and continue
Iteration Exercises
Condition and Loops Style Notes
A Digression
9. Functions
Function Definition: The def and return Statements
Function Use
Function Varieties
Some Examples
Hacking Mode
More Features
Function Exercises
Object Method Functions
Functions Style Notes
10. Additional Notes On Functions
Functions and Namespaces
The global Statement
Call By Value and Call By Reference
Function Objects
II. Data Structures
11. Sequences: Strings, Tuples and Lists
Overview of Sequences
Style Notes
12. Strings
String Semantics
String Literal Values
String Operations
String Comparison Operations
String Built-in Functions
String Methods
String Modules
String Exercises
Digression on Immutability of Strings
13. Tuples
Tuple Semantics
Tuple Literal Values
Tuple Operations
Tuple Comparison Operations
Tuple Statements
Tuple Built-in Functions
Tuple Exercises
Digression on The Sigma Operator
14. Lists
List Semantics
List Literal Values
List Operations
List Comparison Operations
List Statements
List Built-in Functions
List Methods
List Exercises
15. Mappings and Dictionaries
Dictionary Semantics
Dictionary Literal Values
Dictionary Operations
Dictionary Comparison Operations
Dictionary Statements
Dictionary Built-in Functions
Dictionary Methods
Dictionary Exercises
Advanced Parameter Handling For Functions
16. Sets
Set Semantics
Set Literal Values
Set Operations
Set Comparison Operators
Set Statements
Set Built-in Functions
Set Methods
Set Exercises
17. Exceptions
Exception Semantics
Basic Exception Handling
Raising Exceptions
An Exceptional Example
Complete Exception Handling and The finally Clause
Exception Functions
Exception Attributes
Built-in Exceptions
Exception Exercises
Style Notes
A Digression
18. Generators and the yield Statement
Generator Semantics
Defining a Generator
Generator Functions
Generator Statements
Generator Methods
Generator Example
Generator Exercises
Subroutines and Coroutines
19. Files
File Semantics
Additional Background
Built-in Functions
File Methods
Several Examples
File Exercises
20. Advanced Sequences
Lists of Tuples
List Comprehensions
Sequence Processing Functions: map, filter, reduce and zip
Advanced List Sorting
Multi-Dimensional Arrays or Matrices
The Lambda
III. Data + Processing = Objects
21. Classes
Class Definition: the class Statement
Creating and Using Objects
Special Method Names
Some Examples
Object Collaboration
Class Definition Exercises
22. Advanced Class Definition
Built-in Functions
Collaborating with max, min and sort
Initializer Techniques
Class Variables
Static Methods and Class Method
Design Approaches
Advanced Class Definition Exercises
Style Notes
23. Some Design Patterns
Factory Method
Design Pattern Exercises
24. Creating or Extending Data Types
Semantics of Special Methods
Basic Special Methods
Special Attribute Names
Numeric Type Special Methods
Container Special Methods
Iterator Special Method Names
Attribute Handling Special Method Names
Extending Built-In Classes
Special Method Name Exercises
25. Properties and Descriptors
Semantics of Attributes
Attribute Access Exercises
26. Decorators
Semantics of Decorators
Built-in Decorators
Defining Decorators
Defining Complex Decorators
Decorator Exercises
27. Managing Contexts: the with Statement
Semantics of a Context
Using a Context
Defining a Context Manager Class
IV. Components, Modules and Packages
28. Modules
Module Semantics
Module Definition
Module Use: The import Statement
Finding Modules: The Path
Variations on An import Theme
The exec Statement
Module Exercises
Style Notes
29. Packages
Package Semantics
Package Definition
Package Use
Package Exercises
Style Notes
30. The Python Library
Overview of the Python Library
Most Useful Library Sections
Library Exercises
31. Complex Strings: the re Module
Creating a Regular Expression
Using a Regular Expression
Regular Expression Exercises
32. Dates and Times: the time and datetime Modules
Semantics: What is Time?
Some Class Definitions
Creating a Date-Time
Date-Time Calculations and Manipulations
Presenting a Date-Time
Time Exercises
Additional time Module Features
33. File Handling Modules
The os.path Module
The os Module
The fileinput Module
The tempfile Module
The glob and fnmatch Modules
The shutil Module
The File Archive Modules: tarfile and zipfile
The Data Compression Modules: zlib, gzip, bz2
The sys Module
Additional File-Processing Modules
File Module Exercises
34. File Formats: CSV, Tab, XML, Logs and Others
Comma-Separated Values: The csv Module
Tab Files: Nothing Special
Property Files and Configuration (or.INI) Files: The ConfigParser Module
Fixed Format Files, A COBOL Legacy: The codecs Module
XML Files: The xml.minidom and xml.sax Modules
Log Files: The logging Module
File Format Exercises
35. Programs: Standing Alone
Kinds of Programs
Command-Line Programs: Servers and Batch Processing
The getopt Module
The optparse Module
Command-Line Examples
Other Command-Line Features
Command-Line Exercises
36. Programs: Clients, Servers, the Internet and the World Wide Web
About TCP/IP
Web Servers and the HTTP protocol
Web Services: The xmlrpclib Module
Mid-Level Protocols: The urllib2 Module
Client-Server Exercises
Socket Programming
V. Projects
37. Areas of the Flag
38. The Date of Easter
39. Musical Pitches
Equal Temperament
Circle of Fifths
Pythagorean Tuning
Five-Tone Tuning
40. Bowling Scores
41. Mah Jongg Hands
Tile Class Hierarchy
Wall Class
Set Class Hierarchy
Hand Class
Some Test Cases
Hand Scoring - Points
Hand Scoring - Doubles
Limit Hands
42. Chess Game Notation
Algebraic Notation
Descriptive Notation
Game State
PGN Processing Specifications

List of Tables

23.1. Craps Game States

  Published under the terms of the Open Publication License Design by Interspire