\n",
"\n",
"If you want to display data from Python in tabular format, you may want to use the [pandas](http://pandas.pydata.org/) library."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Internal links in a notebook\n",
"You can link to any section heading as follows:\n",
"```\n",
" [link text to display](#heading-text)\n",
"```\n",
"For example, [here is a link to the executing cells section](#Executing-Cells) above."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"

\n",
"\n",
"The heading text is case sensitive, and spaces should be replaced with dashes."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"## HTML in a notebook\n",
"In addition to Markdown, you can use any HTML commands in a notebook (at least in markdown mode)."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## LaTeX in a notebook\n",
"LaTeX is a powerful typesetting language in its own right, but a subset of LaTeX is supported in Jupyter.\n",
"If you are learning LaTeX, [here is a cheat sheet](https://artofproblemsolving.com/wiki/index.php/LaTeX:Symbols) that you may find useful.\n",
"Built-in to Jupyter notebooks you will find support for inline equations by encapsulating them in single `$` and for displayed equations by encapsulating them in double `$$`"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Some Common LaTeX Usage\n",
" | LaTeX | Displayed |\n",
" |:------|:----------|\n",
" |`\\int_{\\alpha}^{\\beta} f(x) dx` | $\\int_{\\alpha}^{\\beta} f(x) dx$ |\n",
" |`\\frac{a}{b}` | $\\frac{a}{b}$ |\n",
" |`\\left( \\frac{\\partial f}{\\partial x} \\right)` | $\\left( \\frac{\\partial f}{\\partial x}\\right)$ |\n",
" |`\\vec{x}` | $\\vec{x}$ |\n",
" |`\\overline{abc}` | $\\overline{abc}$ |\n",
" |`\\int_a^b \\underbrace{ \\frac{1-x}{e^x}}_\\mathrm{integrand} dx` | $\\int_a^b \\underbrace{ \\frac{1-x}{\\exp(x^2)}}_\\mathrm{integrand} dx$ |\n",
" |`\\mathsf{abcde}` | $\\mathsf{abcde}$ |\n",
" |`\\mathrm{abcde}` | $\\mathrm{abcde}$ |\n",
" |`\\boldsymbol{\\alpha}` | $\\boldsymbol{\\alpha}$ |"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
" \n",
"### LaTeX Macros\n",
"LaTeX macros can be used to create shortcuts to simplify frequently used, complex expressions.\n",
"Put them at the top of your notebook in a markdown cell and they will be available in any later markdown cell.\n",
"Here are some examples:\n",
"$\\def\\del#1#2{\\frac{\\partial#1}{\\partial#2}}$\n",
"$\\def\\d#1#2{\\frac{\\mathrm{d}#1}{\\mathrm{d}#2}}$\n",
"$\\newcommand{\\Ls}{\\mathcal{L}_\\sigma}$\n",
"$\\def\\D#1#2{\\frac{\\mathrm{D}#1}{\\mathrm{D}#2}}$\n",
"\n",
" | Macro definition | Example Usage | Example Output |\n",
" |:---|:---|:---|\n",
" |`$\\def\\del#1#2{\\frac{\\partial#1}{\\partial#2}}$` | `$\\del{f}{x}$` | $\\del{f}{x}$ |\n",
" |`$\\def\\d#1#2{\\frac{\\mathrm{d}#1}{\\mathrm{d}#2}}$` | `$\\d{f}{x}$` | $\\d{f}{x}$ |\n",
" |`$\\def\\D#1#2{\\frac{\\mathrm{D}#1}{\\mathrm{D}#2}}$` | `$\\D{f}{x}$` | $\\D{f}{x}$ |\n",
" |`$\\newcommand{\\Ls}{\\mathcal{L}_\\sigma}$` | `$\\Ls$` | $\\Ls$ |"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpresent": {
"id": "1855a294-5905-4907-a6b0-fdff175dbe54"
},
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### LaTeX extensions\n",
"If you install the [nbextensions](https://github.com/ipython-contrib/jupyter_contrib_nbextensions) package then you get support for a few additional LaTeX features, including:\n",
" * The `equation` environment for numbered equations:\n",
"```\n",
"\\begin{equation}\n",
" a = b + c \\label{eq:my special equation}\n",
"\\end{equation}\n",
"```\n",
" To disable numbering, put `\\nonumber` after the equation. That is basically equivalent to putting the equation within double `$$`:\n",
"```\n",
"$$ a = b + c $$\n",
"```\n",
" * The `align` environment for aligned (and numbered) equations:\n",
"```\n",
"\\begin{align}\n",
" a &\\rightarrow b + c \\\\\n",
" c + d &\\rightarrow e \\\\\n",
"\\end{align}\n",
"```\n",
" This produces equations aligned at the `&` symbol. These equations are also numbered and can be labeled.\n",
" * To cross-reference an equation, use the `\\ref{}` command and supply the label you created:\n",
"```\n",
"\\ref{eq:my special equation}\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Debugging a notebook\n",
"There are a few techniques to use in debugging a notebook:\n",
" 1. Add `print()` statements in to see what your code is doing.\n",
" 1. Use the [`%who` or `whos` cell magics](#who-and-whos). This will tell you what variables are currently defined.\n",
" 1. Activate the __variable inspector__ plugin from the [nbextensions](https://github.com/ipython-contrib/jupyter_contrib_nbextensions) package. This will give you a floating window containing a list of variables currently defined. It is very handy!\n",
" 1. Sometimes it is helpful to restart the kernel (press the ⟳ button). This will clear all variables defined in the notebook and give you a clean slate.\n",
" 1. Add the `%%debug` [cell magic](#Magics-in-Jupyter-Notebooks) to the beginning of a code cell. This will produce a terminal prompt at the bottom of the cell that allows you to enter python commands (like `print`, etc.) without needing to rerun the whole cell.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpresent": {
"id": "afb7d369-94c2-4e14-b321-27ac7a60055d"
},
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"-------\n",
"\n",
"# Python in a Jupyter Notebook\n",
"One of the great things about Jupyter is the ability to intersperse documentation (Markdown) with code that runs interactively and generates results.\n",
"\n",
"We'll next consider a few useful things that you will need to know to get started in Python\n",
"\n",
"## Defining Functions\n",
"In python, a function is defined using the `def` keyword. For example, we could create a function `add`"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"nbpresent": {
"id": "e1d7f2d3-0369-4ccd-8013-cef2424a66d0"
},
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"def add( x1, x2 ):\n",
" return x1+x2;"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"

\n",
"\n",
"The colon at the end of the first line, which indicates that the body of the function (indented) comes next."
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpresent": {
"id": "e89a09a7-5975-4ac3-8e36-db9642b95785"
},
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"To use this, we can call our `add` function:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"nbpresent": {
"id": "0faed10c-c222-4ca3-8e19-e9e05cdfa172"
},
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add( 5, 6 )"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"nbpresent": {
"id": "5655e352-202b-4418-9169-84ad515233c3"
},
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"13.14\n"
]
}
],
"source": [
"x = 3.14; y = 10;\n",
"z = add( x, y );\n",
"print(z)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpresent": {
"id": "aac6e360-8723-490e-b132-432ab4658b45"
},
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Using Python Libraries\n",
"Python has a vast number of libraries to simplify many tasks. Among those that you will probably use regularly:\n",
" * [matplotlib](http://matplotlib.org) provides very powerful (but sometimes challenging to use) plotting capabilities. A quick way to get started on a plot is to look at the [matplotlib gallery](http://matplotlib.org/gallery.html) to obtain code to generate a plot like the one you want to create.\n",
" * [NumPy](https://docs.scipy.org/doc/numpy/reference) provides really powerful array handling capabilities like those in Matlab to allow you to create and manipulate arrays of data. It also has some algorithms that operate on the data. If you are a matlab user, [here is a great guide](http://mathesaurus.sourceforge.net/matlab-python-xref.pdf) showing you the equivalent numpy/python commands\n",
" * [SciPy](https://docs.scipy.org/doc/scipy/reference) has a large number of algorithms such as interpolation, quadrature, optimization, ODE solvers, linear algebra tools, etc. There is some duplication between NumPy and SciPy.\n",
" * [pandas](http://pandas.pydata.org/pandas-docs/stable/) provides a lot of data analysis tools.\n",
" * [SymPy](http://docs.sympy.org/latest/index.html) provides support for symbolic mathematics within Python.\n",
"\n",
"The first step in using a library is to tell Python that you want it. This is done using the `import` command:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"nbpresent": {
"id": "a5c88707-20b7-4be2-a8fb-9497b8750b95"
},
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"# import the numpy library and alias it to np. numpy provides linspace like matlab. \n",
"# It also provides constants like pi and functions like sin, cos, etc.\n",
"import numpy as np\n",
"x = np.linspace( 0, 2*np.pi ) # create an array spaced from 0 to 2*pi.\n",
"y = np.sin(x)\n",
"z = y**2"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpresent": {
"id": "d5a15660-8e8d-47b6-bd33-a5fe59fbc929"
},
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Plotting\n",
"Python has vast plotting capabilities that can be a bit overwhelming.\n",
"Frequently, we simply want to plot basic 1D functions, which is accomplished through [matplotlib's pyplot package](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot):"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"nbpresent": {
"id": "12959a7f-b7b6-4d8a-bdd7-471fa0588770"
},
"scrolled": true,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"