Key Points
- JupyterLab is an application for running, managing, and organizing
Python code in files called Jupyter notebooks.
- You will only need to install JupyterLab once for this
workshop.
- Your working directory determines where programs are run and how
filepaths are interpreted.
- Use the JupyterLab Notebook interface for editing and running
Python.
- Use the keyboard and mouse to select and edit cells.
- Jupyter Notebooks turn Markdown into pretty-printed
documentation.
- Markdown can be used to format commentary on Python code.
- Use variables to store values.
- Use
print to display values.
- Variables persist between cells.
- Variables must be created before they are used.
- Variables can be used in calculations.
- Python is case-sensitive.
- Use meaningful variable names.
- 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 mulitiplied.
- Strings have a length (but numbers don’t).
- Use an index to get a single character from a string.
- Use a slice to get a substring.
- Use the built-in function
len to find the length of a
string.
- 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.
- Use comments to add documentation to programs.
- 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.
- Python reports a syntax error when it can’t understand the source of
a program.
- 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.
- A list stores many values in a single structure.
- Use an item’s index to fetch it from a list.
- Lists’ values can be replaced by assigning to them.
- Appending items to a list lengthens it.
- Use
del to remove items from a list entirely.
- The empty list contains no values.
- Lists may contain values of different types.
- Character strings can be indexed like lists.
- Character strings are immutable.
- Indexing beyond the end of the collection is an error.
- A for loop executes commands once for each value in a
collection.
- A for loop is made up of a collection, a loop variable, and a
body.
- The first line of the for loop must end with a colon, and the body
must be indented.
- Indentation is always meaningful in Python.
- Loop variables can be called anything (but it is strongly advised to
have a meaningful name to the looping variable).
- The body of a loop can contain many statements.
- Use range() to iterate over a sequence of numbers.
- 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.
- 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.
- Most of the power of a programming language is in its
libraries.
- A program must import a library module in order to use it.
- Use
help to learn about the contents of a library
module.
- The
math and statistics modules contain
helpful functions and constants for mathematical operations.
- Import specific items from a library or create an alias to shorten
programs.
- Use the Pandas library to get basic statistics out of tabular
data.
- Use
index_col to specify that a column’s values should
be used as row headings.
- Use
DataFrame.info to find out more about
DataFrame.
- The
DataFrame.columns variable stores information about
the DataFrame’s columns.
- Use
DataFrame.T to transpose a DataFrame.
- Use
DataFrame.describe to get summary statistics about
data.
- Use
DataFrame.iloc[..., ...] to select values by
integer location.
- Use
: on its own to mean all columns or all rows.
- Select multiple columns or rows using
DataFrame.loc and
a named slice.
- Result of slicing can be used in further operations.
- Use comparisons to select data based on value.
- Select values from a DataFrame using a Boolean mask.
-
matplotlib is the
most widely used scientific plotting library in Python.
- You can plot data directly from a Pandas dataframe using
.plot().
- Many styles of plot are available: see the Python Graph
Gallery for more options.
- Break programs down into functions to make them easier to
understand.
- Define a function using
def with a name, parameters,
and a block of code.
- Defining a function does not run it.
- Arguments in a function call are matched to its defined
parameters.
- Functions may return a result to their caller using
return.
- The scope of a variable is the part of a program that can ‘see’ that
variable.
- Follow standard Python style in your code.
- Use docstrings to provide builtin help.
- Python supports a large and diverse community across academia and
industry.