Skip to content
Snippets Groups Projects
Commit 285c9eab authored by Jenni Rinker's avatar Jenni Rinker
Browse files

updating nb from kenneth

parent 4409e055
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Jupyter notebooks are extremely cool ways to demo your code or work interactively. They are separated into cells, just like Mathematica. Jupyter notebooks are extremely cool ways to demo your code or work interactively. They are separated into cells, just like Mathematica.
To... To...
* **Close a notebook**: File > "Close and halt". Do NOT close the tab! Python will still run in the background. * **Close a notebook**: File > "Close and halt". Do NOT close the tab! Python will still run in the background.
* **Edit text**: Double-click a cell. Shift+Enter when done. * **Edit text**: Double-click a cell. Shift+Enter when done.
* **Evaluate code in a cell**: Shif+Enter. * **Evaluate code in a cell**: Shif+Enter.
* **Create a new cell above or below**: ESC+a, ESC+b * **Create a new cell above or below**: ESC+a, ESC+b
* **See all the keyboard shortcuts**: ESC+h * **See all the keyboard shortcuts**: ESC+h
See the Jupyter cheatsheet for many more commands! See the Jupyter cheatsheet for many more commands!
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Cell types # Cell types
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
You can control the type of cell in Cell -> Cell Type, and choose whether your cell is You can control the type of cell in Cell -> Cell Type, and choose whether your cell is
- text - text
- code - code
Text cells can be formatted following Markdown conventions. Text cells can be formatted following Markdown conventions.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Exercise 0 # Exercise 0
0.1: Open the keyboard shortcuts (see bullet list above). What are the two different keyboard input modes? 0.1: Open the keyboard shortcuts (see bullet list above). What are the two different keyboard input modes?
0.2: Create a new cell below this one, and change it from a code cell to a text cell. Write a cool message to yourselves, using some markdown formatting. 0.2: Create a new cell below this one, and change it from a code cell to a text cell. Write a cool message to yourselves, using some markdown formatting.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Some code and cool things # Some code and cool things
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
x = 'Hello, world!' x = 'Hello, world!'
``` ```
%% Cell type:markdown id: tags: %% 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: 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: %% Cell type:code id: tags:
``` python ``` python
x = 4 x = 4
2 * x 2 * x
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
There are a bunch of "magic" commands that can really up your game. There are a bunch of **"magic" commands** that can really up your game.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
%lsmagic %lsmagic
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
For example, let's test how long a silly function takes... For example, let's test how long a silly function takes...
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import numpy as np import numpy as np
def silly_function(): def silly_function():
np.random.randint(0, high=100, size=int(1e6)) np.random.randint(0, high=100, size=int(1e6))
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
%%timeit %%timeit
silly_function() silly_function()
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
**Auto-complete suggestion** Place the cursor at the end of `np.ar` below and hit *TAB* on the keyboard
%% Cell type:code id: tags:
``` python
x = np.ar
```
%% Cell type:markdown id: tags:
you should get at list of all the function in numpy starting with `ar`
**Get function documentation** Place the cursor inside the brackets and hit and hold *SHIFT* and then hit *TAB*
%% Cell type:code id: tags:
``` python
x = np.arange()
```
%% Cell type:markdown id: tags:
you should get a window showing the first part of the documentation string for the function. Press the "plus" to extend the view or the "arrow" to make it a full width window. The documentation string can also be written to an output cell as shown below.
%% Cell type:code id: tags:
``` python
help(np.arange)
```
%% Cell type:markdown id: tags:
# Exercise 1 # Exercise 1
%% Cell type:markdown id: tags: %% 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? 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?
__HINT__: Google "numpy arange function" to figure out how to define $x$. __HINT__: Use the documentation "arange" from above to define $x$.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import numpy as np import numpy as np
# x = ??? # x = ???
# y = ??? # y = ???
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1, figsize=(6, 3)) fig, ax = plt.subplots(1, 1, figsize=(6, 3))
ax.scatter(x, y) ax.scatter(x, y)
ax.set(title='Hello, world!', xlabel='x', ylabel='y = 1/x'); ax.set(title='Hello, world!', xlabel='x', ylabel='y = 1/x');
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# When you're done # When you're done
1. Close the notebook by hitting File > "Close and halt". 1. Close the notebook by hitting File > "Close and halt".
2. In the "Home page" tab, click "Quit". 2. In the "Home page" tab, click "Quit".
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment