Contents
Contents
Contents
Starting Out
Running Python
Literals
Integers
Real Numbers
Strings
True, False, and None
Collections of literals
Indexing into Arrays
Combining Literals
Numeric operators
Comparing things
Combining comparisons
Precedence and Associativity
Precedence
Associativity
Variables
Variables
Variable naming
Assignment
Precedence and Associativity of Assignment
Assignment and Arrays
Assignment Patterns
The Transfer Pattern
The Update Pattern
The Throw-away Pattern
The Throw-away Pattern and Functions
About Patterns
Conditionals
Logical expressions
Logical operators
Short circuiting
If expressions
if-elif-else chains
Practice with Boolean expressions
Functions
Encapsulating a series of operations
Passing arguments
Creating functions on the fly (ADVANCED)
The Function and Procedure Patterns
Python Programs and Using Files
Your first program
Vim and Python
A Neat Macro
Writing Python Programs
Order of definitions
Importing code
Input and Output
Input
Reading from the keyboard
Reading From the command line
Counting the command line arguments
Accessing individual arguments
What command-line arguments are
Reading from files
Reading files using file pointer methods
Using a scanner
Output
Writing to the console
Printing quote characters
Writing to a file
More about Functions
Built-in Functions
Function syntax
Function Objects
Calling Functions
Returning from functions
Scope
In Scope or Out
The Local Variable Pattern
The Non-local Variable Pattern
The Accessible Variable Pattern
The Tinted Windows Pattern
Tinted Windows with Parallel Scopes
Alternate terminology
Three Scope Rules
Shadowing
Modules
Loops
Other loops
The
counting
pattern
The
filtered-count
pattern
The
accumulate
pattern
The
filtered-accumulate
pattern
The
search
pattern
The
extreme
pattern
The
extreme-index
pattern
The
filter
pattern
The
map
pattern
The
shuffle
pattern
The
merge
pattern
The
fossilized
pattern
The
missed-condition
pattern
Code
More on Input
Converting command line arguments en mass
Reading individual items from files
Processing files a line at a time
Using a Scanner
Reading Tokens into an Array
Reading Records into an Array
Creating an Array of Records
Other Scanner Methods
Lists
The Node Data Structure
The Linked-List Data Structure
More on Walking lists
A Walking Application
Processing Lists versus Arrays
The
filter
and
map
patterns
The
shuffle
and
merge
patterns
The
fossilized
pattern
The
wrong-spot
pattern
Why Lists?
Problems
Code
Recursion
The parts of a recursive function
The greatest common divisor
The Fibonacci sequence
Manipulating arrays and lists with recursion
The
counting
pattern
The
accumulate
pattern
The
filtered-count
and
filtered-accumulate
patterns
The
filter
pattern
The
map
pattern
The
search
pattern
The
shuffle
pattern
The
merge
pattern
The
generic
merge
pattern
The
fossilized
pattern
The
bottomless
pattern
Code
Comparing Recursion and Looping
Factorial
The greatest common divisor
The Fibonacci sequence
CHALLENGE: Transforming loops into recursions
Code
Two-dimensional Arrays
Creating a matrix
Retrieving and modifying values in a matrix
Working with matrices
Reading a 2D array from a file
2D patterns
Simulating 2D arrays with simple arrays
Ragged 2D arrays
Problems
Footnotes
lusth@cs.ua.edu
Contents