Contents

Prefacexv

Chapter 0 Modern Computers

0.1  Calculations by Machine

0.2  How Computers Work and Why We Made Them

0.2.1  Numbers

Example: Base

Convert Binary Numbers to Decimal

Convert Decimal Numbers to Binary

Arithmetic in Binary

0.2.2  Memory

0.2.3  Stored Programs

0.3  Computer Systems Are Built in Layers

0.3.1  Assemblers and Compilers

0.3.2  Graphical User Interfaces (GUIs)

Widgets

0.4  Computer Networks

0.4.1  Internet

0.4.2  World Wide Web

0.5  Representation

0.6  Summary

Chapter 1 Computers and Programming

1.1  Solving a Problem Using a Computer

1.2  Executing Python

1.3  Guess a Number

1.4  Rock-Paper-Scissors

1.5  Solving the Guess a Number Problem

1.6  Solving the Rock-Paper-Scissors Problem

1.6.1  Variables and Values – Experimenting with the Graphical User Interface

1.6.2  Exchanging Information with the Computer

1.6.3  Example 1: Draw a Circle Using Characters

1.6.4  Strings, Integers, and Real Numbers

1.6.5  Number Bases

1.6.6  Example 2: Compute the Circumference of any Circle

1.6.7  Guess a Number Again

1.7  IF Statements

1.7.1  Else

1.8  Documentation

1.9  Rock-Paper-Scissors Again

1.10  Types Are Dynamic (Advanced)

1.11  Summary

Chapter 2 Repetition

2.1  The WHILE Statement

2.1.1  The Guess-A-Number Program Yet Again

2.1.2  Modifying the Game

2.2  Rock-Paper-Scissors Yet Again

2.2.1  Random Numbers

2.3  Counting Loops

2.4  Prime or Non-Prime

2.4.1  Exiting from a Loop

2.4.2  Else

2.5  Loops That are Nested

2.6  Draw a Histogram

2.7  Loops in General

2.8  Exceptions and Errors

2.8.1  Problem: A Final Look at Guess a Number

2.9  Summary

Chapter 3 Sequences: Strings, Tuples, and Lists

3.1  Strings

3.1.1  Comparing Strings

Problem: Does a City Name, Entered at the Console, Come before or after the Name Denver?

3.1.2  Slicing – Extracting Parts of Strings

Problem: Identify a “Print” Statement in a String

3.1.3  Editing Strings

Problem: Create a JPEG File Name from a Basic String

Problem: Change the Suffix of a File Name

Problem: Reverse the Order of Characters in a String

Problem: Is a Given File Name That of a Python Program?

3.1.4  String Methods

3.1.5  Spanning Multiple Lines

3.1.6  For Loops Again

3.2  The Type Bytes

3.3  Tuples

3.3.1  Tuples in For Loops

Problem: Print the Number of Neutrons in an Atomic Nucleus

3.3.2  Membership

Problem: What Even Numbers Less than or Equal to 100 are Also Perfect Squares?

3.3.3  Delete

Problem: Delete the Element Lithium from the Tuple Atoms, along with Its Atomic Number.

3.3.4  Update

Problem: Change the Entry for Lithium to an Entry for Oxygen

3.3.5  Tuple Assignment

3.3.6  Built-In Functions for Tuples

3.4  Lists

Problem: Compute the Average (Mean) of a List of Numbers

3.4.1  Editing Lists

3.4.2  Insert

3.4.3  Append

3.4.4  Extend

3.4.5  Remove

3.4.6  Index

3.4.7  Pop

3.4.8  Sort

3.4.9  Reverse

3.4.10  Count

3.4.11  List Comprehension

3.4.12  Lists and Tuples

3.4.13  Exceptions

Problem: Delete the Element Helium from a List

Problem: Delete a Specified Element from a List

3.5  Set Types

3.5.1  Example: Craps

3.6  Summary

Chapter 4 Functions

4.1  Function Definition: Syntax and Semantics

4.1.1  Problem: Use poundn to Draw a Histogram

4.1.2  Problem: Generalize the Histogram Code for Other Years

4.2  Function Execution

4.2.1  Returning a Value

Problem: Write a Function to Calculate the Square Root of its Parameter

4.2.2  Parameters

4.2.3  Default Parameters

4.2.4  None

4.2.5  Example: The Game of Sticks

4.2.6  Scope

4.2.7  Variable Parameter Lists

4.2.8  Variables as Functions

Example: Find the Maximum Value of a Function

4.2.9  Functions as Return Values

4.3  Recursion

4.3.1  Avoiding Infinite Recursion

4.4  Creating Python Modules

4.5  Program Design Using Functions – Example: The Game of Nim

4.5.1  The Development Process Exposed

4.6  Summary

Chapter 5 Files: Input and Output

5.1  What Is a File? A Little “Theory”

5.1.1  How Are Files Stored on a Disk?

5.1.2  File Access is Slow

5.2  Keyboard Input

5.2.1  Problem: Read a Number from the Keyboard and Divide It by 2

5.3  Using Files in Python: Less Theory, More Practice

5.3.1  Open a File

File Not Found Exceptions

5.3.2  Reading from Files

End of File

Common File Input Operations

CSV Files

Problem: Print the Names of Planets Having Fewer Than Ten Moons

Problem: Play Jeopardy Using a CSV Data Set

The With Statement

5.4  Writing To Files

Example: Write a Table of Squares to a File

5.4.1  Appending Data to a File

Example: Append Another 20 Squares to the Table of Squares File

5.5  Summary

Chapter 6 Classes

6.1  Classes and Types

6.1.1  The Python Class – Syntax and Semantics

6.1.2  A Really Simple Class

6.1.3  Encapsulation

6.2  Classes and Data Types

6.2.1  Example: A Deck of Cards

6.2.2  A Bouncing Ball

6.2.3  Cat-A-Pult

Basic Design

Detailed Design

6.3  Subclasses and Inheritance

6.3.1  Non-Trivial Example: Objects in a Video Game

6.4  Duck Typing

6.5  Summary

Chapter 7 Graphics

7.1  Introduction to Graphics Programming

7.1.1  Essentials: The Graphics Window and Colors

7.1.2  Pixel Level Graphics

Example: Create a Page of Notepaper

Example: Creating a Color Gradient

7.1.3  Lines and Curves

Example: Notepaper Again

7.1.4  Polygons

7.1.5  Text

7.1.6  Example: A Histogram

7.1.7  Example: A Pie Chart

7.1.8  Images

Pixels

Example: Identifying a Green Car

Example: Thresholding

Transparency

7.1.9  Generative Art

7.2  Summary

Chapter 8 Manipulating Data

8.1  Dictionaries

8.1.1  Example: A Naive Latin – English Translation

8.1.2  Functions for Dictionaries

8.1.3  Dictionaries and Loops

8.2  Arrays

8.3  Formatted Text, Formatted I/O

8.3.1  Example: NASA Meteorite Landing Data

8.4  Advanced Data Files

8.4.1  Binary File

Example: Create a File of Integers

8.4.2  The Struct Module

Example: A Video Game High Score File

8.4.3  Random Access

Example: Maintaining the High Score File in Order

8.5  Standard File Types

8.5.1  Image Files

8.5.2  GIF

8.5.3  JPEG

8.5.4  TIFF

8.5.5  PNG

8.5.6  Sound Files

WAV

8.5.7  Other Files

HTML

EXE

8.6   Summary

Chapter 9 Multimedia

9.1  Mouse Interaction

Example: Draw a Circle at the Mouse Cursor

Example: Change Background Color Using the Mouse

9.1.1  Mouse Buttons

Example: Draw Lines Using the Mouse

Example: A Button

9.2  The Keyboard

Example: Pressing a “+” Creates a Random Circle

Example: Reading a Character String

9.3  Animation

9.3.1  Object Animation

Example: A Ball in a Box

Example: Many Balls in a Box

9.3.2  Frame Animation

Example: Read Frames and Play Them Back as an Animation

Example: Simulation of the Space Shuttle Control Console (A Class That Will Draw an Animation at a Specific Location)

9.4  RGBA Colors – Transparency

9.5  Sound

Example: Play a Sound

Example: Control Volume Using the Keyboard. Pause and Unpause

Example: Play a Sound Effect at the Right Moment: Bounces

9.6  Video

Example: Carclub – Display the Video carclub2.mpg (Annotated)

Exercise: Threshold a Video (Processing Pixels)

9.7  Summary

Chapter 10 Basic Algorithms

10.1  Sorting

10.1.1  Selection Sort

10.1.2  Merge Sort

10.2  Searching

10.2.1  Timings

10.2.2  Linear Search

10.2.3  Binary Search

10.3  Random Number Generation

10.3.1  Linear Congruential Method

10.4  Cryptography

10.4.1  One-Time Pad

10.4.2  Public Key Encryption (RSA)

Example: Encrypt the Message “Depart at Dawn” Using RSA

10.5  Compression

10.5.1  Huffman Encoding

10.5.2  LZW Compression

10.6  Hashing

djb2

10.6.1  sdbm

10.7  Summary

Chapter 11 Programming for the Sciences

11.1  Finding Roots of Equations

11.2  Differentiation

11.3  Integration

11.4  Optimization: Finding Maxima and Minima

11.4.1  Newton Again

11.4.2  Fitting Data to Curves – Regression

11.4.3  Evolutionary Methods

11.5  Longest Common Subsequence (Edit Distance)

11.5.1  Determining Longest Common Subsequence (LCS)

11.6  Summary

Chapter 12 How to Write Good Programs

12.1  Procedural Programming – Word Processing

12.1.1  Top-Down

12.1.2  Centering

12.1.3  Right Justification

12.1.4  Other Commands

12.2  Object Oriented Programming – Breakout

12.3  Describing the Problem as a Process

12.3.1  Initial Coding for a Tile

12.3.2  Initial Coding for the Paddle

12.3.3  Initial Coding for the Ball

12.3.4  Collecting the Classes

12.3.5  Developing the Paddle

12.3.6  Ball and Tile Collisions

12.3.7  Ball and Paddle Collisions

12.3.8  Finishing the Game

12.4  Rules for Programmers

12.5  Summary

Chapter 13 Communicating with the Outside World

13.1  Email

Example: Send an Email

13.1.1  Reading Email

Example: Display the Subject Headers for Emails in Inbox

13.2  FTP

Example: Download and Display the README File from an FTP Site

13.3  Communication Between Processes

Example: A Server That Calculates Squares

13.4  Twitter

Example: Connect to the Twitter Stream and Print Specific Messages

13.5  Communicating with Other Languages

Example: Find Two Large Relatively Prime Numbers

13.6  Summary

Chapter 14 A Brief Glib Reference

14.1  Glib tkinter

14.2  Images

14.3  Dynamic Glib

14.4  Video

14.5  Audio

14.6  Interaction

14.7  Other

Index