|
Basics
|
Python programs are plain text files.
We can write Python in the Jupyter Notebook as well (which isn’t plain text).
Most common atomic data types are int, float, str, and bool.
Most common type of collection is list.
Use variables to store values.
Use print to display values.
Variables persist between cells.
Variables must be created before they are used.
Use an index to get a single character from a string or list.
Use a slice to get a substring or sub-list.
Use the built-in function len to find the length of a string.
Python is case-sensitive.
|
|
Control Flow
|
Repeat actions for each element in a collection with for loops.
Use range to generate a list of numbers.
Use if/elif/else to make choices.
Use built-in functions like len and max to do calculations.
Objects like strings and lists have methods that operate on them.
Use if statements to control whether or not a block of code is executed.
Conditionals are often used inside loops.
Use else to execute a block of code when an if condition is not true.
Use elif to specify additional tests.
Conditions are tested once, in order.
Create a table showing variables’ values to trace a program’s execution.
|
|
Libraries
|
Use import to load a library.
Use dot notation to get library’s contents.
The math library has common mathematical functions.
The random library produces pseudo-random numbers.
The csv library can read CSV files correctly.
|
|
Writing Functions
|
Use def to define a new function.
Give parameters default values to make use easier and intent clearer.
Use *args to handle variable-length parameter lists.
Use return at any point to return values.
Turn repeated or deeply-nested pieces of code into functions.
Functions temporarily store values on a call stack.
|
|
Dictionaries
|
|
|
NumPy Arrays
|
Use NumPy arrays to store multi-dimensional arrays.
Algebraic matrices are a special case of arrays.
Array operations make (most) loops unnecessary.
|
|
Pandas
|
|
|
Plotting
|
|
|
File I/O
|
Open a file for reading or writing with open.
Use read or readline to read directly.
Use file in for loop to process lines.
Use write to add data to a file.
|
|
Programming Style
|
Fail early, often, and meaningfully.
Use assert to check that programs are working correctly.
Give assert statements meaningful error messages to help users.
|
|
Testing
|
|
|
Wrap-Up
|
|
FIXME: reference material.