|
Running and Quitting
|
Python programs are plain text files.
Use the Jupyter Notebook for editing and running Python.
The Notebook has Control and Edit modes.
Use the keyboard and mouse to select and edit cells.
The Notebook will turn Markdown into pretty-printed documentation.
Markdown does most of what HTML does.
|
|
Python Fundamentals
|
Basic data types in Python include integers, strings, and floating-point numbers.
Use variable = value to assign a value to a variable in order to record it in memory.
Variables are created on demand whenever a value is assigned to them.
Use print(something) to display the value of something.
|
|
Data Types and Type Conversion
|
Every value has a type.
Use the built-in function type to find the type of a value.
Types control what operations can be done on values.
Strings can be added and multiplied.
Strings have a length (but numbers don’t).
Must convert numbers to strings or vice versa when operating on them.
Can mix integers and floats freely in operations.
Variables only change value when something is assigned to them.
|
|
Built-in Functions and Help
|
A function may take zero or more arguments.
Commonly-used built-in functions include max, min, and round.
Functions may only work for certain (combinations of) arguments.
Functions may have default values for some arguments.
Use the built-in function help to get help for a function.
The Jupyter Notebook has two ways to get help.
Every function returns something.
|
|
Error Messages
|
Use comments to add documentation to programs.
Python reports a syntax error when it can’t understand the source of a program.
Indentation is meaningful in Python.
Python reports a runtime error when something goes wrong while a program is executing.
Fix syntax errors by reading the source code, and runtime errors by tracing the program’s execution.
|
|
Libraries
|
Most of the power of a programming language is in its libraries.
A program must import a library in order to use it.
Use help to find out more about a library’s contents.
Import specific items from a library to shorten programs.
Create an alias for a library when importing it to shorten programs.
|
|
Analyzing Patient Data
|
Import a library into a program using import libraryname.
Use the numpy library to work with arrays in Python.
The expression array.shape gives the shape of an array.
Use array[x, y] to select a single element from a 2D array.
Array indices start at 0, not 1.
Use low:high to specify a slice that includes the indices from low to high-1.
Use # some kind of explanation to add comments to programs.
Use numpy.mean(array), numpy.max(array), and numpy.min(array) to calculate simple statistics.
Use numpy.mean(array, axis=0) or numpy.mean(array, axis=1) to calculate statistics across the specified axis.
|
|
Visualizing Tabular Data
|
|
|
Storing Multiple Values in Lists
|
[value1, value2, value3, ...] creates a list.
Lists can contain any Python object, including lists (i.e., list of lists).
Lists are indexed and sliced with square brackets (e.g., list[0] and list[2:9]), in the same way as strings and arrays.
Lists are mutable (i.e., their values can be changed in place).
Strings are immutable (i.e., the characters in them cannot be changed).
|
|
Sets (Kai)
|
A set stores unsorted unique values.
The set list contains no values.
Sets may contain values of different types.
|
|
Sets (Kai)
|
A set stores unsorted unique values.
The set list contains no values.
Sets may contain values of different types.
|
|
Repeating Actions with Loops
|
Use for variable in sequence to process the elements of a sequence one at a time.
The body of a for loop must be indented.
Use len(thing) to determine the length of something that contains other values.
|
|
Analyzing Data from Multiple Files
|
Use glob.glob(pattern) to create a list of files whose names match a pattern.
Use * in a pattern to match zero or more characters, and ? to match any single character.
|
|
Making Choices
|
Use if condition to start a conditional statement, elif condition to provide additional tests, and else to provide a default.
The bodies of the branches of conditional statements must be indented.
Use == to test for equality.
X and Y is only true if both X and Y are true.
X or Y is true if either X or Y, or both, are true.
Zero, the empty string, and the empty list are considered false; all other numbers, strings, and lists are considered true.
True and False represent truth values.
|
|
Creating Functions
|
Define a function using def function_name(parameter).
The body of a function must be indented.
Call a function using function_name(value).
Numbers are stored as integers or floating-point numbers.
Variables defined within a function can only be seen and used within the body of the function.
If a variable is not defined within the function it is used, Python looks for a definition before the function call
Use help(thing) to view help for something.
Put docstrings in functions to provide help for that function.
Specify default values for parameters when defining a function using name=value in the parameter list.
Parameters can be passed by matching based on name, by position, or by omitting them (in which case the default value is used).
Put code whose parameters change frequently in a function, then call it with different parameter values to customize its behavior.
|
|
Programming Style
|
|
|
Debugging (Niko)
|
Know what code is supposed to do before trying to debug it.
Make it fail every time.
Make it fail fast.
Change one thing at a time, and for a reason.
Keep track of what you’ve done.
Be humble.
|
|
Defensive Programming
|
Program defensively, i.e., assume that errors are going to arise, and write code to detect them when they do.
Put assertions in programs to check their state as they run, and to help readers understand how those programs are supposed to work.
Use preconditions to check that the inputs to a function are safe to use.
Use postconditions to check that the output from a function is safe to use.
Write tests before writing code in order to help determine exactly what that code is supposed to do.
|
|
Command-Line Programs
|
The sys library connects a Python program to the system it is running on.
The list sys.argv contains the command-line arguments that a program was run with.
Avoid silent failures.
The pseudo-file sys.stdin connects to a program’s standard input.
|