Building Skills in Programming

How To Write Your Own Software Using 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.

12/30/2008


Table of Contents

Preface
Why Read This Book?
What Is This Book About?
Audience
Organization of This Book
Conventions Used in This Book
Acknowledgements
I. Getting Started
1. About Computers
Terminology
What is a Program?
Where Is The Program?
Concepts Exercises
2. About Programs
What Happens When a Program “Runs?
The Python Program and What It Does
What is Programming?
Programs Exercises
So How Do They Create Binary Executables?
Concepts FAQ's
3. Let There Be Python
Teminology
Installation FAQ's
4. Two Minimally-Geeky Problems
About Gambling
The Roulette Problem
The Craps Problem
Directions
Problem FAQ's
5. Why Python is So Cool
Core Coolness
Coolness FAQ's
II. Using Python
6. Instant Gratification
The Windows Command Prompt
The Mac OS Terminal Tool
The GNU/Linux Terminal Tool
How Do We Stop?
Your First Conversation in Python: miles per gallon
Decimal-Points and Accuracy
More Conversations on Arithmetic
Parenthesis and Precedence
Long-Winded Statements
More About Punctuation
Conversational Python Exercises
Direct Python Interaction FAQ
7. IDLE Time
The Development Environment
Starting and Stopping IDLE
Using IDLE — Python Shell Window
A Conversation Using IDLE
IDLE Exercises
IDLE Interaction FAQ
III. Arithmetic and Expressions
8. Simple Arithmetic
Plain Integers, Also Known As Whole Numbers
Floating-Point Numbers, Also Known As Scientific Notation
Long Integers — Whole Numbers on Steroids
Mixing Numbers, Some More Rules
Complex Numbers — For The Mathematically Inclined
Strings — Anything Not A Number
Octal and Hexadecimal — Counting by 8's or 16's
Expression Exercises
9. Better Arithmetic Through Functions
Say It With Functions
pow and round Definitions
Multiple Steps
Accuracy?
Another Round, Please
Functions are Factories (really!)
Going the Other Way
Most and Least
Basic Function Exercises
10. Extra Functions: math and random
Meaningful Chunks and Modules
The math Module — Trig and Logs
More math Functions
Misuse and Abuse
The random Module — Rolling the Dice
Extra Function Exercises
Function FAQ's
11. Special Ops
Bits and Bytes
Different Bases and Representations
Operators for Bit Manipulation
The Two Specialized Division Operators
Special Ops Exercises
Special Ops FAQ's
12. Peeking Under the Hood
Execution — Two Points of View
Expression Style Notes
One Way To Tackle Fixed Point Math
IV. Programming Essentials
13. Seeing Results
The print Statement
Dressing Up Our Output
Print Exercises
Print FAQ
14. Turning Python Loose With a Script
Making A Script File
Running Scripts in IDLE
Script Exercises
Scripting FAQ
15. Generalizing a Calculation
Putting Name Tags on Results
The Assignment Statement
Assignment Combo Package
Where Exactly Did We Expect To Be?
Assignment Exercises
16. Assignment Bonus Features
Combining Assignment Statements
More About Python Conversations
Variables and Assignment Style Notes
17. Can We Get Your Input?
Getting Raw Input
Getting More Useful Input
Additional Notes and Caveats
Input Function Definitions
The Standard Files
Simple Input Exercises
The del Statement
Input FAQ
V. Some Self-Control
18. Truth and Logic
Truth
Logic
Exercises in Truth
Truth and Logic FAQ
19. Making Decisions
The Comparison Operators: More or Less
More Sophisticated Comparisons
Taking A Short-Cut
Logic and Comparison Exercises
Comparison FAQ
20. Conditional Processing: Only When Necessary
Conditional Processing
The if Statement
Example if Statements
The elif Condition for Alternatives
The else Condition as a Catch-All
The pass Statement — a Do-Nothing
Condition Exercises
21. Iterative Processing: While We Have More To Do
Iterative Processing
Patterns of Iteration
The for Statement
Multi-Dimensional Loop-the-Loop
Simulating All 100 Rolls of the Dice
The while Statement
Counting Sevens
Iteration Exercises
Iteration FAQ
22. Becoming More Controlling
More Iteration Control: break and continue
The assert Statement
Conditions and Loops Style Notes
The Hidden Dangers of else
A Digression On Design
23. Turning Python Loose with More Sophisticated Scripts
Let Python Run It
Giving the Shell a Hint in GNU/Linux or MacOS
Giving the Shell a Hint — Windows Detailing
Double-Clicking Icons
Another Script Example
Additional Flexibility
Script Exercises
VI. Organizing Programs with Function Definitions
24. Function Definitions — Adding New Verbs
What is a Function, Really?
Function Definition: The def and return Statements
Function Design Patterns — Rules of the Game
Function Example
Hacking Out A Solution
Function Exercises
Optional Function Exercises — Recurrence
25. Flexibility, Clarity and a Close Relative
Flexible Definitions with Optional Parameters
Optional Parameter Example
What About the range Function?
Adding Clarity with Keyword Parameters
Optional and Keyword Parameter Exercises
Object Methods — A Cousin of Functions
Functions Style Notes
26. A Few More Tools
Returning Multiple Values
Keeping Track of Variable Names — The Namespace
Talking About Functions Behind Their Backs
More Function Exercises
The global Statement
VII. Getting Our Bearings
27. Our Course
VIII. Basic Collections of Data: Strings, Lists and Tuples
28. Collecting Items in Sequence
Sequence means “In Order
Working With a Sequence
Subspecies of Sequences
Features of a Sequence
Sequence Exercises
Style Notes
29. Sequences of Characters: Strings
What Does Python mean by “String?
Writing a String in Python
String Factory Functions
Operating on String Data
% — The Message Formatting Operator
Built-in Functions for Strings
Comparing Two Strings — Alphabetical Order
Methods Strings Perform
Modules That Help Work With Strings
Some Common Processing Patterns
String Exercises
30. Doubles, Triples, Quadruples: Tuples!
What Does “Tuple” Mean?
How We Write Tuples
The Tuple Factory Function
Operations on Tuples
Built-in Functions for Tuples
Making Comparisons Between Tuples
Statements and Tuples
Translating From Math To Python: Conjugating The Verb “To Sigma
Tuple Exercises
31. Using Lists To Stay Organized
What Does Python Mean by “List?
How We Write Lists
List Factory Functions
Operations We Perform On Lists
Built-in Functions for Lists
Comparing Two Lists
Methods to Transform Lists
Statements and Lists: Review of for and del
From Outlines to Bank Lines — Stacks and Queues
List Exercises
More Advanced List Exercises
32. Common List Design Patterns
The One-Two Punch: Lists of Tuples
List Construction Shortcuts
Sorting a List: Expanding on the Rules
Tables and Matrices — More Multi-Dimensional Loop-the-Loops
List Processing Exercises
Sequence FAQ's
IX. Additional Processing Patterns — Exceptions and Iterations
33. Exceptions and Unusual Events
What Does Python Mean By “Exception”?
How Do We Handle Exceptional Events?
Exception Handling, Minimal Math Version
Exception Handling, Advanced Math Version
Nested Exception Handling
Some Concrete Examples
Designing Exceptions
Raising The White Flag in Exceptional Situations
A More Complete Example
Debugging an Exception Handler
What the Built-in Exceptions Really Mean
Exception Exercises
Style Notes
Exception FAQ's
34. Looping Back To Look At Iteration
The Iteration Contract — What the for Statement Expects
Customizing Iteration
Generator Definition: The def and yield Statements
Putting Generators To Use
Geeky Generator Example: Web Server Logs
Generator Example: Roulette Spins
Generator Exercises
Iterator and Generator FAQ's
X. More Data Collections: Sets, Mappings, Dictionaries and Files
35. Collecting Items in a Set
What does Python mean by "Set"?
How Do We Create a Set?
Operations We Can Perform On A Set
Comparing Sets: Subset and Superset
Method Functions of Sets
Statements and Sets
Built-in Functions For Sets
Example of Using Sets
Set Exercises
Set FAQ's
36. Mappings and Dictionaries
What Does Python Mean by “Dictionary”?
How We Create A Dictionary
Operations We Can Perform On A Dictionary
Comparing Dictionaries — Not A Good Idea
Method Functions That Dictionaries Offer
Statements and Dictionaries
Built-in Functions That Work With Dictionaries
Dictionary Exercises
Mapping FAQ's
37. Defining More Flexible Functions
A List of Extra Positional Values
A Dictionary of Extra Keyword Values
Dictionary Use Under the Hood
Exercises
Advanced Feature FAQ's
38. Files — The Permanent Record
File Objects — Our Connection To The File System
The File Factory
File Statements: Reading and Writing (but no Arithmetic)
Basic File Exercises
File FAQ's
39. Files II — Beyond the Basics
Methods We Use on File Objects
File Processing with the CSV Module
File Processing Case Studies
Additional File-Related Modules
Advanced File Exercises
40. Files III — The Grand Unification
Additional File-Processing Modules
The Grand Unification of Device Types
Files are the Plumbing of a Software Architecture
XI. Data + Processing = Objects
41. Objects: A Retrospective
The Ubiquitous Object
The Built-in Classes — A Review
Data, Processing and Philosophy — What Does It All Mean?
Object and Class Exercises
Class FAQ's
42. Defining New Objects
Class Definition: The class and def Statements
Class Use: Making New Objects
The State of Being — Instance Variables
At The Starting Line — Setting The Initial Values
Operations — Access and Manipulation
Politics: Collaboration and Responsibility
Keeping Organized
Class Definition Exercises
43. Inheritance, Generalization and Specialization
Leveraging the Superclass — Simplification Through Inheritance
Extending a Class Through Inheritance
Inheriting and Extending a Superclass Method
It's All In the Cards — A Case Study
Advanced Class Definition Exercises
More Class FAQ's
44. Additional Classy Topics
What If It's True For All Members Of The Class?
Built-in Functions for Classes
Simplifying Inherited Functions
Some Examples
Style Notes
45. Special Behavior Requires Special Methods
Looking Under The Hood
Basic Special Method Names
Special Attribute Names
Basic Special Method Exercises
Special Methods FAQs
46. Sophisticated Numbers: Fractions and Currency
What Makes Something Numeric?
How The Numeric Operations Work
Rational Numbers — A Case Study
More Sophistication — Currency Calculations
The Official Rules
Numeric Class Definition Exercises
47. Creating New Types of Collections
Collections: The Superclass
How Do I Make An Object Behave Like A Sequence?
Making It Behave Like A Mapping
Collection Class Definition Exercises
Collection FAQ's
XII. Organizing Programs with Modules
48. Module Definitions — Adding New Concepts
Divide and Conquer with Modules
Defining a Module: Creating Python Files
Using A Module: The import Statement
Some Variations On the import Statement
Thinking In Modules, and the Declaration of Dependence
Module Exercises
Dividing and Conquering — The Art Of Design
Style Notes
Module FAQ's
49. Essential Modules
Overview of the Python Library
Always Useful Modules
Internet Data Structure Modules
50. Fixed-Point Numbers — Doing High Finance
The Problem With Numbers
Using Decimal Numbers
Rounding, known as Quantization
Controlling Rounding
Decimal Exercises
51. Time and Date Processing
Concepts: Point in Time and Duration
The datetime Module
Formal Definitions in datetime
The time module
Formal Definitions in time
Date and Time Exercises
Date and Time FAQ
52. Text Processing and Pattern Matching
How Does Pattern Matching Help Us?
How To Create Patterns Using Regular Expressions
Some Examples
Objects We Use For Pattern Matching and Parsing
Some Debugging Hints
Geeky Text Processing Example: Web Server Logs
Text Processing Exercises
Patterns and Regular Expression FAQ's
XIII. Fit and Finish: Complete Programs
53. Wrapping and Packaging Our Solution
Script or Library? The Main Program Switch
The Standard Command-Line Interface
An Example Program
Main Program Exercises
BTW — The exec Statement
54. Architectural Patterns — A Family Tree
Command-Line Interface Variations
GUI and Web Variations
A. Debugging
Bibliography

List of Figures

1.1. Layers of Abstraction
2.1. The Python Abstract Computer
5.1. C Compiler vs. Python
11.1. The Basic Number Line
11.2. Encoding Signs On The Number Line
24.1. Evaluation of a Function
33.1. How Functions Nest
35.1. Union of Sets, S1|S2
35.2. Intersection Of Sets, S1&S2
35.3. Difference of Sets, S1-S2
35.4. Symmetric Difference, S1^S2
35.5. Subset, S1 in S2
38.1. Python File and OS File

List of Examples

1. Python Example
6.1. Our First Dialog: Miles per Gallon
6.2. Using Floating Decimal Point Numbers
14.1. example1.py
15.1. example3.py
15.2. portfolio.py
15.3. craps.py
16.1. line.py
17.1. rawdemo.py
17.2. stock.py
17.3. inputdemo.py
19.1. floatequal.py
20.1. comeoutroll.py
21.1. table.py
21.2. roll100.py
21.3. countsevens.py
22.1. sixodds.py
23.1. Command Line Execution
23.2. example2.py
23.3. example1 Shell Script
23.4. example1.bat Batch File
24.1. functions.py
24.2. function1.py Initial Version
24.3. function1.py Final Version
25.1. badcall.py
26.1. rolldice.py
26.2. rolldice.py — First Version
26.3. rolldice.py — Second Version
29.1. temperature.py
30.1. Sigma Using a Numeric Index
30.2. Sigma Using an Iterator
33.1. windchill1.py
33.2. windchill2.py
33.3. ckyorn.py
34.1. logScanGenerator.py
38.1. addrpage.py
38.2. name_addr.csv
38.3. nameaddr.py
39.1. stockquote.py
39.2. stocksort.py
39.3. portfolio.py
42.1. die.py
42.2. die.py, version 2
42.3. die.py, version 3
43.1. vehicles.py
43.2. dice.py
44.1. wheel.py
48.1. die.py
48.2. Original File
48.3. New Library Module
48.4. New Application Script
53.1. dicesim.py

List of Equations

8..
8..
8.1. Convert °C (Celsius) to °F (Fahrenheit)
8.2. Convert °F (Fahrenheit) to °C (Celsius)
8.3. Mortgage Payment, version 1
8.4. Mortgage, payments due at the end of each period
8.5. Mortgage, payments due at the beginning of each period
8.6. Surface Air Consumption Rate
8.7. Time and Depth from SACR
8.8. Wind Chill, new model
8.9. Wind Chill, old model
8.10. Sail Clew Load
21.1. Development Effort
21.2. Development Cost
21.3. Project Duration
21.4. Staffing
21.5. Definition of e
21.6. Definition of Factorial, n!
30.1. Basic Summation
30.2. Summation with Half-Open Interval
30.3. Summing Elements of an Array, x
30.4. Mean
30.5. Standard Deviation
46.1. Adding Fractions
46.2. Multiplying Fractions