Key Points
Getting Started
- You can launch JupyterLab from the command line or from Anaconda Navigator.
- You can use a JupyterLab notebook to edit and run Python.
- Notebooks can include both code and markdown (text) cells.
Variables and Data Types
- Use
print()to display values. - Use
#to add comments that explain why the code does what it does. - Use
=to assign a value to a variable. - Basic data types are strings (
str), integers (int), floating point numbers (float), and booleans (bool). - Use
type()to find out the type of a value. - Jupyter displays the value of the last line of a cell.
Operators, Functions and Methods
- The type of a value determines what an operator does with it.
- Read error messages from the bottom up: the last line names the error.
- Use
int(),float()andstr()to convert between types. - Functions are called with
function(arguments), methods withobject.method(arguments). - String methods return a new value and do not change the original string.
Lists and Dictionaries
- A list stores many values in order:
[value1, value2, ...]. - Use
list[index]to access an element; indexing starts at0. - Use
list[start:stop]to select a slice;stopis not included. - A dictionary stores key-value pairs:
{key: value, ...}. - Use
dictionary[key]to look up a value.
Loops and Conditionals
- A
forloop runs its indented body once for each element of a collection. - Use
if,elifandelseto choose which code to run. - Conditions are tested in order; the first one that is
Truewins. - Indentation defines which lines belong to a loop or an
ifstatement.
Introduction to pandas
- Use
import library as aliasto load a library. -
pd.read_csv()reads a CSV file into a DataFrame. - Use
.head(),.shapeand.columnsto get an overview of a DataFrame. -
df["column"]selects a column,df[["col1", "col2"]]several columns. -
df[condition]keeps only the rows where the condition isTrue. - Series have methods for statistics (
.mean(),.median(), …) and plotting (.hist(),.plot.bar(), …).
Retrieving Metadata from an API
- A web API lets programs request data from a web service via a URL.
-
urllib.request.urlopen(url).read()downloads the response of a URL. -
json.loads()converts JSON text into Python dictionaries and lists. - Navigate nested data step by step:
data["titles"][0]["title"]. - DataCite registers DOIs for data and software, Crossref for most articles.