Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Jupyter notebooks are extremely cool ways to demo your code or work interactively. They are separated into cells, just like Mathematica.\n",
"\n",
"To...\n",
"* **Close a notebook**: File > \"Close and halt\". Do NOT close the tab! Python will still run in the background.\n",
"* **Edit text**: Double-click a cell. Shift+Enter when done.\n",
"* **Evaluate code in a cell**: Shif+Enter.\n",
"* **Create a new cell above or below**: ESC+a, ESC+b\n",
"* **See all the keyboard shortcuts**: ESC+h\n",
"\n",
"See the Jupyter cheatsheet for many more commands!"
]
},
{
"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": [
"# Exercise 0\n",
"\n",
"0.1: Open the keyboard shortcuts (see bullet list above). What are the two different keyboard input modes?\n",
"\n",
"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",
"metadata": {},
"source": [
"# Some code and cool things"
]
},
{
"cell_type": "code",
"execution_count": null,
"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": null,
"metadata": {},
"outputs": [],
"source": [
"x = 4\n",
"2 * x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are a bunch of **\"magic\" commands** that can really up your game."
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%lsmagic"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For example, let's test how long a silly function takes..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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": null,
"metadata": {},
"outputs": [],
"source": [
"%%timeit\n",
"silly_function()"
]
},
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Auto-complete suggestion** Place the cursor at the end of `np.ar` below and hit *TAB* on the keyboard"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = np.ar"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"you should get at list of all the function in numpy starting with `ar`\n",
"\n",
"**Get function documentation** Place the cursor inside the brackets and hit and hold *SHIFT* and then hit *TAB*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = np.arange()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"help(np.arange)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise 1"
]
},
{
"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?\n",
"\n",
"__HINT__: Use the documentation \"arange\" from above to define $x$."
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"# x = ???\n",
"# y = ???"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"fig, ax = plt.subplots(1, 1, figsize=(6, 3))\n",
"ax.scatter(x, y)\n",
"ax.set(title='Hello, world!', xlabel='x', ylabel='y = 1/x');"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# When you're done\n",
"\n",
"1. Close the notebook by hitting File > \"Close and halt\".\n",
"2. In the \"Home page\" tab, click \"Quit\"."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
}
},
"nbformat": 4,
"nbformat_minor": 2
}