Python Summary

  1. Expressions:
    1. Operands and operators.
    2. Operator precedence and associativity.
    3. Non-container data types: int, float, string, date, datetime.
    4. Interactive use of IDLE.

  2. Control structure:
    1. Loops with while, for, break, continue, else.
      1. Loop until the user provides valid input.
      2. Loop until the end of a list or until the end of input.
      3. Loop a certain number of times by looping through the integers in a range.
    2. Conditional statements with if, elif, else to steer the computer in one of several directions. Abbreviations: conditional expressions, assert.
    3. Raise and catch exceptions: raise, try, except.

  3. Packaging:
    1. Package statements in a function. Function definition, arguments (positional and keyword), default argument value, argument unpacking, return value. Global vs. local variables.
    2. Package functions and attributes in a class. Class definition.
    3. Package classes and functions in a module. The __name__ variable. Import modules with pip3.

  4. Sources of input:
    1. from the keyboard with input
    2. from a file on the disk with open
    3. from a URL with urllib.request.urlopen
    4. from a child process via a pipe with os.popen
    5. from the operating system’s environment variables with os.environ

  5. Destinations of output:
    1. Standard output and standard error output with print, or to an open file
    2. Exit status with sys.exit
    3. Graphical output with tkinter

  6. Formats of input:
    1. CSV (comma separated values) format
    2. JSON (JavaScript Object Notation) format
    3. lines of text in none of the above formats
    4. SQLite database file
    5. binary (non-textual) input: gif, jpg, png.

  7. Sequences and other containers:
    1. numbers in a range
    2. characters in a string; indices.
    3. items in a list; indices.
    4. items in a tuple; indices.
    5. keys and values in a dictionary
    6. dictionary as a sparse list
    7. keys in a set
    8. attributes in an object
    9. items yielded by a generator

  8. Examples of processing (using the word “list” to stand for various kinds of container):
    1. Print each item in a list.
    2. Pick a random value from a list.
    3. Search for a given value in a list.
    4. Find the largest or smallest value.
    5. Split a string into a list (of lines, of words, of characters).
    6. Create and copy a list; list comprehensions.
    7. Create a list of lists; rows and columns of data.
    8. Sum a list.
    9. Sort a list.
    10. Discard duplicates from a list.
    11. Find the union, intersection, and difference of lists.
    12. Convert between different types of lists (e.g, convert string of characters into list of characters)
    13. Convert a list of bytes read from input into a list of characters.
    14. A rare example of a loop without a list: compute the value of π.

Distinguishing features of Python

  1. Syntax.
    1. Python uses indentation, rather than a non-whitespace token such as }, to delimit a suite (a block of statements or a control structure).
    2. A Python function can accept keyword arguments as well as the more conventional positional arguments.
    3. A Python function can accept an argument that is a lambda function.

  2. Control structure.
    1. EAFP error checking (using try and except) is favored over LBYL error checking (using if).
    2. for and while loops can have an else clause.
    3. for loops are favored over while loops. A for loop can loop through anything that is iterable.

  3. Data structures.
    1. tuple more efficient than a list.
    2. Often what we require is merely an iterable, rather than specifically a list.
    3. Create list with a list comprehension instead of repeatedly appending to an initially empty list. Dictionary comprehension, set comprehension.
    4. Generator function containing a yield statement instead of a return statement, to avoid creating a list.
    5. Generator expression as an abbreviation for a generator function that consists of a yield statement (possibly in an if statement) in a for loop.

  4. Semantics.
    1. Many Python variables contain a reference to a value, rather than the value itself. Two or more variables can refer to the same value.