"Jupyter notebooks are extremely cool ways to demo your code or work interactively. They are separated into cells, just like Mathematica. You enter insert mode by clicking \"enter\" when a cell is selected, and you exit by hitting the escape key. You evaluate a cell using shift-enter."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Cell types"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can control the type of cell in Cell -> Cell Type, and choose whether your cell is\n",
"- text\n",
"- code \n",
"\n",
"Text cells can be formatted following Markdown conventions."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Random cool things"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"x = 'Hello, world!'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that the output of a cell in the last line is automatically printed if it's a variable or unassigned output:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 4\n",
"2 * x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are a bunch of \"magic\" commands that can really up your game."
"Automagic is ON, % prefix IS NOT needed for line magics."
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%lsmagic"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For example, let's test how long a silly function takes..."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"def silly_function():\n",
" np.random.randint(0, high=100, size=int(1e6))"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.7 ms ± 19.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"silly_function()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define $x$ and $y$ below such that $x$ is a numpy array from 1 to 9 (inclusive) and $y=1/x$. Then, run your cell and the next cell and look at the resulting plot. Does it make sense? Run your mouse over the plot. Notice anything cool?\n",
"\n",
"__HINT__: Google \"numpy arange function\" to figure out how to define $x$."
Jupyter notebooks are extremely cool ways to demo your code or work interactively. They are separated into cells, just like Mathematica. You enter insert mode by clicking "enter" when a cell is selected, and you exit by hitting the escape key. You evaluate a cell using shift-enter.
%% Cell type:markdown id: tags:
# Cell types
%% Cell type:markdown id: tags:
You can control the type of cell in Cell -> Cell Type, and choose whether your cell is
- text
- code
Text cells can be formatted following Markdown conventions.
%% Cell type:markdown id: tags:
# Random cool things
%% Cell type:code id: tags:
``` python
x='Hello, world!'
```
%% Cell type:markdown id: tags:
Note that the output of a cell in the last line is automatically printed if it's a variable or unassigned output:
%% Cell type:code id: tags:
``` python
x=4
2*x
```
%% Output
8
%% Cell type:markdown id: tags:
There are a bunch of "magic" commands that can really up your game.
Automagic is ON, % prefix IS NOT needed for line magics.
%% Cell type:markdown id: tags:
For example, let's test how long a silly function takes...
%% Cell type:code id: tags:
``` python
importnumpyasnp
defsilly_function():
np.random.randint(0,high=100,size=int(1e6))
```
%% Cell type:code id: tags:
``` python
%%timeit
silly_function()
```
%% Output
1.7 ms ± 19.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%% Cell type:markdown id: tags:
# Exercise
%% Cell type:markdown id: tags:
Define $x$ and $y$ below such that $x$ is a numpy array from 1 to 9 (inclusive) and $y=1/x$. Then, run your cell and the next cell and look at the resulting plot. Does it make sense? Run your mouse over the plot. Notice anything cool?
__HINT__: Google "numpy arange function" to figure out how to define $x$.