- Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathJupyter_Notebooks.html
574 lines (519 loc) · 38.3 KB
/
Jupyter_Notebooks.html
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
<!DOCTYPE html>
<htmlclass="writer-html5" lang="en" >
<head>
<metacharset="utf-8" /><metacontent="Topic: Jupyter notebooks, Difficulty: Easy, Category: Tutorial" name="description" />
<metacontent="jupyter, jupyter lab, notebook, kernel, basics, server, console, plot, beginner, data science" name="keywords" />
<metaname="viewport" content="width=device-width, initial-scale=1.0" />
<title>Jupyter Notebooks — Python Like You Mean It</title>
<linkrel="stylesheet" href="../_static/pygments.css" type="text/css" />
<linkrel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<linkrel="stylesheet" href="../_static/my_theme.css" type="text/css" />
<!--[if lt IE 9]>
<script src="../_static/js/html5shiv.min.js"></script>
<![endif]-->
<scriptdata-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<scriptsrc="../_static/jquery.js"></script>
<scriptsrc="../_static/underscore.js"></script>
<scriptsrc="../_static/doctools.js"></script>
<scriptasync="async" src="https://www.googletagmanager.com/gtag/js?id=UA-115029372-1"></script>
<scriptsrc="../_static/gtag.js"></script>
<scriptcrossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"></script>
<script>window.MathJax={"tex": {"inlineMath": [["$","$"],["\\(","\\)"]],"processEscapes": true},"options": {"ignoreHtmlClass": "tex2jax_ignore|mathjax_ignore|document","processHtmlClass": "tex2jax_process|mathjax_process|math|output_area"}}</script>
<scriptdefer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<scriptsrc="../_static/js/theme.js"></script>
<linkrel="index" title="Index" href="../genindex.html" />
<linkrel="search" title="Search" href="../search.html" />
<linkrel="next" title="Setting Up a Development Environment" href="Getting_Started_With_IDEs_and_Notebooks.html" />
<linkrel="prev" title="An Informal Introduction to Python" href="Informal_Intro_Python.html" />
</head>
<bodyclass="wy-body-for-nav">
<divclass="wy-grid-for-nav">
<navdata-toggle="wy-nav-shift" class="wy-nav-side">
<divclass="wy-side-scroll">
<divclass="wy-side-nav-search" >
<ahref="../index.html" class="icon icon-home"> Python Like You Mean It
</a>
<divclass="version">
1.4
</div>
<divrole="search">
<formid="rtd-search-form" class="wy-form" action="../search.html" method="get">
<inputtype="text" name="q" placeholder="Search docs" />
<inputtype="hidden" name="check_keywords" value="yes" />
<inputtype="hidden" name="area" value="default" />
</form>
</div>
</div><divclass="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<pclass="caption" role="heading"><spanclass="caption-text">Table of Contents:</span></p>
<ulclass="current">
<liclass="toctree-l1"><aclass="reference internal" href="../intro.html">Python Like You Mean It</a></li>
<liclass="toctree-l1 current"><aclass="reference internal" href="../module_1.html">Module 1: Getting Started with Python</a><ulclass="current">
<liclass="toctree-l2"><aclass="reference internal" href="SiteFormatting.html">A Quick Guide to Formatting</a></li>
<liclass="toctree-l2"><aclass="reference internal" href="GettingStartedWithPython.html">Introducing the Python Programming Language</a></li>
<liclass="toctree-l2"><aclass="reference internal" href="Installing_Python.html">Installing Python</a></li>
<liclass="toctree-l2"><aclass="reference internal" href="Informal_Intro_Python.html">An Informal Introduction to Python</a></li>
<liclass="toctree-l2 current"><aclass="current reference internal" href="#">Jupyter Notebooks</a><ul>
<liclass="toctree-l3"><aclass="reference internal" href="#Jupyter-Lab">Jupyter Lab</a></li>
<liclass="toctree-l3"><aclass="reference internal" href="#Running-a-Notebook-Server-&-Creating-a-Notebook">Running a Notebook Server & Creating a Notebook</a></li>
<liclass="toctree-l3"><aclass="reference internal" href="#Notebook-Cells">Notebook Cells</a></li>
<liclass="toctree-l3"><aclass="reference internal" href="#An-Example-Notebook">An Example Notebook</a></li>
<liclass="toctree-l3"><aclass="reference internal" href="#Familiarizing-Yourself-with-Jupyter-Notebooks">Familiarizing Yourself with Jupyter Notebooks</a><ul>
<liclass="toctree-l4"><aclass="reference internal" href="#Markdown-Cells">Markdown Cells</a></li>
<liclass="toctree-l4"><aclass="reference internal" href="#Using-Jupyter-Notebooks-with-Other-Languages">Using Jupyter Notebooks with Other Languages</a></li>
<liclass="toctree-l4"><aclass="reference internal" href="#Jupyter-Notebook-Support-in-Visual-Studio-Code">Jupyter Notebook Support in Visual Studio Code</a></li>
</ul>
</li>
</ul>
</li>
<liclass="toctree-l2"><aclass="reference internal" href="Getting_Started_With_IDEs_and_Notebooks.html">Setting Up a Development Environment</a></li>
</ul>
</li>
<liclass="toctree-l1"><aclass="reference internal" href="../module_2.html">Module 2: The Essentials of Python</a></li>
<liclass="toctree-l1"><aclass="reference internal" href="../module_2_problems.html">Module 2: Problems</a></li>
<liclass="toctree-l1"><aclass="reference internal" href="../module_3.html">Module 3: The Essentials of NumPy</a></li>
<liclass="toctree-l1"><aclass="reference internal" href="../module_3_problems.html">Module 3: Problems</a></li>
<liclass="toctree-l1"><aclass="reference internal" href="../module_4.html">Module 4: Object Oriented Programming</a></li>
<liclass="toctree-l1"><aclass="reference internal" href="../module_5.html">Module 5: Odds and Ends</a></li>
<liclass="toctree-l1"><aclass="reference internal" href="../changes.html">Changelog</a></li>
</ul>
</div>
</div>
</nav>
<sectiondata-toggle="wy-nav-shift" class="wy-nav-content-wrap"><navclass="wy-nav-top" aria-label="Mobile navigation menu" >
<idata-toggle="wy-nav-top" class="fa fa-bars"></i>
<ahref="../index.html">Python Like You Mean It</a>
</nav>
<divclass="wy-nav-content">
<divclass="rst-content">
<divrole="navigation" aria-label="Page navigation">
<ulclass="wy-breadcrumbs">
<li><ahref="../index.html" class="icon icon-home"></a> »</li>
<li><ahref="../module_1.html">Module 1: Getting Started with Python</a> »</li>
<li>Jupyter Notebooks</li>
<liclass="wy-breadcrumbs-aside">
<ahref="../_sources/Module1_GettingStartedWithPython/Jupyter_Notebooks.md.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<divrole="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<divitemprop="articleBody">
<style>
/* CSS for nbsphinx extension */
/* remove conflicting styling from Sphinx themes */
div.nbinput.containerdiv.prompt*,
div.nboutput.containerdiv.prompt*,
div.nbinput.containerdiv.input_areapre,
div.nboutput.containerdiv.output_areapre,
div.nbinput.containerdiv.input_area .highlight,
div.nboutput.containerdiv.output_area .highlight {
border: none;
padding:0;
margin:0;
box-shadow: none;
}
div.nbinput.container>div[class*=highlight],
div.nboutput.container>div[class*=highlight] {
margin:0;
}
div.nbinput.containerdiv.prompt*,
div.nboutput.containerdiv.prompt* {
background: none;
}
div.nboutput.containerdiv.output_area .highlight,
div.nboutput.containerdiv.output_areapre {
background: unset;
}
div.nboutput.containerdiv.output_areadiv.highlight {
color: unset; /* override Pygments text color */
}
/* avoid gaps between output lines */
div.nboutput.containerdiv[class*=highlight] pre {
line-height: normal;
}
/* input/output containers */
div.nbinput.container,
div.nboutput.container {
display: -webkit-flex;
display: flex;
align-items: flex-start;
margin:0;
width:100%;
}
@media (max-width:540px) {
div.nbinput.container,
div.nboutput.container {
flex-direction: column;
}
}
/* input container */
div.nbinput.container {
padding-top:5px;
}
/* last container */
div.nblast.container {
padding-bottom:5px;
}
/* input prompt */
div.nbinput.containerdiv.promptpre {
color:#307FC1;
}
/* output prompt */
div.nboutput.containerdiv.promptpre {
color:#BF5B3D;
}
/* all prompts */
div.nbinput.containerdiv.prompt,
div.nboutput.containerdiv.prompt {
width:4.5ex;
padding-top:5px;
position: relative;
user-select: none;
}
div.nbinput.containerdiv.prompt>div,
div.nboutput.containerdiv.prompt>div {
position: absolute;
right:0;
margin-right:0.3ex;
}
@media (max-width:540px) {
div.nbinput.containerdiv.prompt,
div.nboutput.containerdiv.prompt {
width: unset;
text-align: left;
padding:0.4em;
}
div.nboutput.containerdiv.prompt.empty {
padding:0;
}
div.nbinput.containerdiv.prompt>div,
div.nboutput.containerdiv.prompt>div {
position: unset;
}
}
/* disable scrollbars on prompts */
div.nbinput.containerdiv.promptpre,
div.nboutput.containerdiv.promptpre {
overflow: hidden;
}
/* input/output area */
div.nbinput.containerdiv.input_area,
div.nboutput.containerdiv.output_area {
-webkit-flex:1;
flex:1;
overflow: auto;
}
@media (max-width:540px) {
div.nbinput.containerdiv.input_area,
div.nboutput.containerdiv.output_area {
width:100%;
}
}
/* input area */
div.nbinput.containerdiv.input_area {
border:1px solid #e0e0e0;
border-radius:2px;
/*background: #f5f5f5;*/
}
/* override MathJax center alignment in output cells */
div.nboutput.containerdiv[class*=MathJax] {
text-align: left !important;
}
/* override sphinx.ext.imgmath center alignment in output cells */
div.nboutput.containerdiv.mathp {
text-align: left;
}
/* standard error */
div.nboutput.containerdiv.output_area.stderr {
background:#fdd;
}
/* ANSI colors */
.ansi-black-fg { color:#3E424D; }
.ansi-black-bg { background-color:#3E424D; }
.ansi-black-intense-fg { color:#282C36; }
.ansi-black-intense-bg { background-color:#282C36; }
.ansi-red-fg { color:#E75C58; }
.ansi-red-bg { background-color:#E75C58; }
.ansi-red-intense-fg { color:#B22B31; }
.ansi-red-intense-bg { background-color:#B22B31; }
.ansi-green-fg { color:#00A250; }
.ansi-green-bg { background-color:#00A250; }
.ansi-green-intense-fg { color:#007427; }
.ansi-green-intense-bg { background-color:#007427; }
.ansi-yellow-fg { color:#DDB62B; }
.ansi-yellow-bg { background-color:#DDB62B; }
.ansi-yellow-intense-fg { color:#B27D12; }
.ansi-yellow-intense-bg { background-color:#B27D12; }
.ansi-blue-fg { color:#208FFB; }
.ansi-blue-bg { background-color:#208FFB; }
.ansi-blue-intense-fg { color:#0065CA; }
.ansi-blue-intense-bg { background-color:#0065CA; }
.ansi-magenta-fg { color:#D160C4; }
.ansi-magenta-bg { background-color:#D160C4; }
.ansi-magenta-intense-fg { color:#A03196; }
.ansi-magenta-intense-bg { background-color:#A03196; }
.ansi-cyan-fg { color:#60C6C8; }
.ansi-cyan-bg { background-color:#60C6C8; }
.ansi-cyan-intense-fg { color:#258F8F; }
.ansi-cyan-intense-bg { background-color:#258F8F; }
.ansi-white-fg { color:#C5C1B4; }
.ansi-white-bg { background-color:#C5C1B4; }
.ansi-white-intense-fg { color:#A1A6B2; }
.ansi-white-intense-bg { background-color:#A1A6B2; }
.ansi-default-inverse-fg { color:#FFFFFF; }
.ansi-default-inverse-bg { background-color:#000000; }
.ansi-bold { font-weight: bold; }
.ansi-underline { text-decoration: underline; }
div.nbinput.containerdiv.input_areadiv[class*=highlight] >pre,
div.nboutput.containerdiv.output_areadiv[class*=highlight] >pre,
div.nboutput.containerdiv.output_areadiv[class*=highlight].math,
div.nboutput.containerdiv.output_area.rendered_html,
div.nboutput.containerdiv.output_area>div.output_javascript,
div.nboutput.containerdiv.output_area:not(.rendered_html) >img{
padding:5px;
margin:0;
}
/* fix copybtn overflow problem in chromium (needed for 'sphinx_copybutton') */
div.nbinput.containerdiv.input_area>div[class^='highlight'],
div.nboutput.containerdiv.output_area>div[class^='highlight']{
overflow-y: hidden;
}
/* hide copybtn icon on prompts (needed for 'sphinx_copybutton') */
.prompt .copybtn {
display: none;
}
/* Some additional styling taken form the Jupyter notebook CSS */
div.rendered_htmltable {
border: none;
border-collapse: collapse;
border-spacing:0;
color: black;
font-size:12px;
table-layout: fixed;
}
div.rendered_htmlthead {
border-bottom:1px solid black;
vertical-align: bottom;
}
div.rendered_htmltr,
div.rendered_htmlth,
div.rendered_htmltd {
text-align: right;
vertical-align: middle;
padding:0.5em0.5em;
line-height: normal;
white-space: normal;
max-width: none;
border: none;
}
div.rendered_htmlth {
font-weight: bold;
}
div.rendered_htmltbodytr:nth-child(odd) {
background:#f5f5f5;
}
div.rendered_htmltbodytr:hover {
background:rgba(66,165,245,0.2);
}
/* CSS overrides for sphinx_rtd_theme */
/* 24px margin */
.nbinput.nblast.container,
.nboutput.nblast.container {
margin-bottom:19px; /* padding has already 5px */
}
/* ... except between code cells! */
.nblast.container+ .nbinput.container {
margin-top:-19px;
}
.admonition>p:before {
margin-right:4px; /* make room for the exclamation icon */
}
/* Fix math alignment, see https://github.com/rtfd/sphinx_rtd_theme/pull/686 */
.math {
text-align: unset;
}
</style>
<divclass="section" id="Jupyter-Notebooks">
<h1>Jupyter Notebooks<aclass="headerlink" href="#Jupyter-Notebooks" title="Permalink to this headline"></a></h1>
<p>In recent years, the Jupyter Notebook has become a massively popular tool for doing research-oriented work in Python and other languages alike. Its emergence marked a paradigm shift in the way data science is conducted.</p>
<p>A Jupyter notebook is similar to the IPython console, but, instead of only being able to work with a single line of code at a time, you can easily edit and re-execute <em>any</em> code that had been written in a notebook. Furthermore, you can save a notebook, and thus return to it later. Additionally, a notebook provides many terrific features. For instance, you can embed visualizations of data within a notebook, and write blocks of nicely-formatted text (using the <aclass="reference external" href="https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet">Markdown
syntax</a>), for presenting and explaining the contents of the notebook.</p>
<p>In this way, the Jupyter Notebook stands out as an excellent tool for many practical applications. You could work on a notebook while you are working through sections of this website, for instance, testing out snippets of code, and answering reading-comprehension questions as you proceed through the text, and using markdown-headers to visually separate different portions of the notebook. When I do research, I am always creating Jupyter notebooks in which I write code that analyzes data, I plot
various results, presented in different ways, and I write detailed markdown-text blocks to document my work. The end result is something that I can share with my labmates, and easily revisit months later without having to struggle to recall what I had done.</p>
<divclass="section" id="Jupyter-Lab">
<h2>Jupyter Lab<aclass="headerlink" href="#Jupyter-Lab" title="Permalink to this headline"></a></h2>
<p><aclass="reference external" href="https://jupyterlab.readthedocs.io/">Jupyter lab</a> is a new web interface from Project Jupyter that provides a rich web-based interface for managing and running Jupyter notebooks, console terminals, and text editors, all within your browser. Among its useful features and polished user interface, Jupyter lab provides moveable panes for viewing data, images, and code output apart from the rest of the notebook. This is facilitates effective data science work flows.</p>
<p>It is recommended that you peruse the <aclass="reference external" href="https://jupyterlab.readthedocs.io/en/stable/getting_started/overview.html">Jupyter lab documentation</a> to get a feel for all of its added capabilities.</p>
<p>The following instructions are laid out for running a Jupyter notebook server. That being said, the process for running a Jupyter lab server and working with notebooks therein is nearly identical. Both Jupyter notebook and Jupyter lab should already be <aclass="reference external" href="https://www.pythonlikeyoumeanit.com/Module1_GettingStartedWithPython/Installing_Python.html">installed via Anaconda</a>.</p>
</div>
<divclass="section" id="Running-a-Notebook-Server-&-Creating-a-Notebook">
<h2>Running a Notebook Server & Creating a Notebook<aclass="headerlink" href="#Running-a-Notebook-Server-&-Creating-a-Notebook" title="Permalink to this headline"></a></h2>
<p>Enough gushing about Jupyter notebooks. Let’s start using them!</p>
<p>In your terminal, navigate to a directory (a.k.a folder) that you are okay creating files in. If you don’t know how to do this, Google it!</p>
<p>Once you are in the desired directory, execute in your terminal (type the following, and then hit <codeclass="docutils literal notranslate"><spanclass="pre"><ENTER></span></code>): <codeclass="docutils literal notranslate"><spanclass="pre">jupyter</span><spanclass="pre">notebook</span></code></p>
<p>Alternatively, if you want to work in Jupyter lab, run: <codeclass="docutils literal notranslate"><spanclass="pre">jupyter</span><spanclass="pre">lab</span></code></p>
<p>You should see some text appear in your terminal:</p>
<p><imgalt="Starting a jupyter notebook server on your machine" src="../_images/jupyter_login.PNG" /></p>
<p>This is a “notebook server” that is running on your machine - it basically handles all of the communication between your browser and your machine. A new window or tab should open in your web browser, which looks like a file explorer.</p>
<p><imgalt="File explorer that opens in your browser" src="../_images/jp_files.PNG" /></p>
<p>You can use this to enter subdirectories and to open up any Jupyter notebooks that you have saved. <em>You will need to use this file explorer any time that you want to open up a Jupyter notebook, old or new.</em> In the top-right corner of this window, click on the dropdown menu labeled “New”, and select the option <codeclass="docutils literal notranslate"><spanclass="pre">Python</span><spanclass="pre">3</span></code>.</p>
<p><imgalt="image0" src="../_images/jp_dropdown.PNG" /></p>
<p>A new tab will open in your browser, revealing a “Jupyter notebook” called <codeclass="docutils literal notranslate"><spanclass="pre">Untitled.ipynb</span></code> running a Python 3 kernel. Clicking <codeclass="docutils literal notranslate"><spanclass="pre">File</span><spanclass="pre">></span><spanclass="pre">Rename</span></code> in the notebook will enable you to name your notebook. <codeclass="docutils literal notranslate"><spanclass="pre">.ipynb</span></code> is the file-type suffix used for Jupyter notebooks (<codeclass="docutils literal notranslate"><spanclass="pre">ipynb</span></code> stands for “IPython-notebook”, which is what these notebooks were called prior to 2014). The commands that you run in this notebook are interpreted and executed by Python in the essentially same way that they would be in a
IPython console.</p>
<divclass="admonition warning">
<pclass="admonition-title fa fa-exclamation-circle"><strong>Jupyter Notebooks Do Not Use the Internet</strong>:</p>
<p>Although a Jupyter notebook opens in your browser, <em>everything is happening locally on your machine</em>. You don’t need to be connected to the internet to work on a Jupyter notebook! The notebook server that you ran in your terminal is simply routing communication from your browser to your local machine. For instance, it is sending code to your CPython interpreter to be executed.</p>
</div>
</div>
<divclass="section" id="Notebook-Cells">
<h2>Notebook Cells<aclass="headerlink" href="#Notebook-Cells" title="Permalink to this headline"></a></h2>
<p>Whereas a Python console only allows you to work on one line of code at a time, a notebook allows you to write code within “cells” and to execute these chunks of code cell-by-cell. In the first cell, write the lines of code:</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="n">x</span><spanclass="o">=</span><spanclass="mi">3</span>
<spanclass="n">y</span><spanclass="o">=</span><spanclass="mi">4</span>
</pre></div>
</div>
<p>then press <codeclass="docutils literal notranslate"><spanclass="pre"><SHIFT>+<ENTER></span></code>. This will execute all of the code within the given cell (in this instance, assigning the variables <codeclass="docutils literal notranslate"><spanclass="pre">x</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">y</span></code> with the values 3 and 4, respectively) and then creates a new cell below. In the next cell type the code:</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="n">x</span><spanclass="o">+</span><spanclass="n">y</span>
</pre></div>
</div>
<p>and hit <codeclass="docutils literal notranslate"><spanclass="pre"><SHIFT>+<ENTER></span></code> to execute this code. The number 7 will appear beneath the cell - this, of course, is the value that is returned when <codeclass="docutils literal notranslate"><spanclass="pre">3</span><spanclass="pre">+</span><spanclass="pre">4</span></code> is evaluated:</p>
<p><imgalt="jupyter notebook example" src="../_images/jupyter_early.png" /></p>
<p>Notice that the notebook “knows” about its variables across its cells. This doesn’t just work from top to bottom - you can define <codeclass="docutils literal notranslate"><spanclass="pre">z</span><spanclass="pre">=</span><spanclass="pre">2</span></code> in the third cell, and then execute code that references <codeclass="docutils literal notranslate"><spanclass="pre">z</span></code> in the first cell. What really matters is the <em>order</em> in which the cells are executed. Notice that <codeclass="docutils literal notranslate"><spanclass="pre">In[1]</span></code> denotes that the top cell with the first input-cell executed in the notebook, and <codeclass="docutils literal notranslate"><spanclass="pre">In[2]</span></code> denotes the second cell that was executed.</p>
<p>Formally, the cells within a given notebook share a common “namespace”: any variable defined in a cell can be referenced or redefined in any other cell within the notebook. On the other hand, separate notebooks are completely independent from one another. You can be working on multiple notebooks at once, and they will never “know” about one another.</p>
<p>A major value of using a notebook is that you can rapidly edit these cells (say, change <codeclass="docutils literal notranslate"><spanclass="pre">x</span><spanclass="pre">=</span><spanclass="pre">3</span></code> to <codeclass="docutils literal notranslate"><spanclass="pre">x</span><spanclass="pre">=</span><spanclass="pre">10</span></code>), and re-execute them to nimbly tinker with whatever code you are developing. Although simple, this is a hugely powerful environment for prototyping code.</p>
</div>
<divclass="section" id="An-Example-Notebook">
<h2>An Example Notebook<aclass="headerlink" href="#An-Example-Notebook" title="Permalink to this headline"></a></h2>
<p>To show off a more exciting use-case, let’s create a notebook that plots some data for us. We’ll use matplotlib, a Python library that is used for plotting data, and NumPy, the premiere library for doing numerical work in Python. We will import these libraries for use in our code. Next we’ll define some mathematical functions. And finally, we’ll plot these functions evaluated on a large number of closely-spaced points on the domain.</p>
<divclass="nbinput nblast docutils container">
<divclass="prompt highlight-none notranslate"><divclass="highlight"><pre><span></span>[1]:
</pre></div>
</div>
<divclass="input_area highlight-ipython3 notranslate"><divclass="highlight"><pre><span></span><spanclass="kn">import</span><spanclass="nn">numpy</span><spanclass="k">as</span><spanclass="nn">np</span>
<spanclass="kn">import</span><spanclass="nn">matplotlib.pyplot</span><spanclass="k">as</span><spanclass="nn">plt</span>
<spanclass="c1"># this tells Jupyter to embed matplotlib plots in the notebook</span>
<spanclass="o">%</span><spanclass="k">matplotlib</span> inline
</pre></div>
</div>
</div>
<divclass="nbinput nblast docutils container">
<divclass="prompt highlight-none notranslate"><divclass="highlight"><pre><span></span>[2]:
</pre></div>
</div>
<divclass="input_area highlight-ipython3 notranslate"><divclass="highlight"><pre><span></span><spanclass="k">def</span><spanclass="nf">sinc</span><spanclass="p">(</span><spanclass="n">x</span><spanclass="p">):</span>
<spanclass="k">return</span><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">sin</span><spanclass="p">(</span><spanclass="n">x</span><spanclass="p">)</span><spanclass="o">/</span><spanclass="n">x</span>
<spanclass="k">def</span><spanclass="nf">d_sinc</span><spanclass="p">(</span><spanclass="n">x</span><spanclass="p">):</span>
<spanclass="s2">"derivative of sinc-function"</span>
<spanclass="k">return</span><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">cos</span><spanclass="p">(</span><spanclass="n">x</span><spanclass="p">)</span><spanclass="o">/</span><spanclass="n">x</span><spanclass="o">-</span><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">sin</span><spanclass="p">(</span><spanclass="n">x</span><spanclass="p">)</span><spanclass="o">/</span><spanclass="n">x</span><spanclass="o">**</span><spanclass="mi">2</span>
</pre></div>
</div>
</div>
<divclass="nbinput nblast docutils container">
<divclass="prompt highlight-none notranslate"><divclass="highlight"><pre><span></span>[3]:
</pre></div>
</div>
<divclass="input_area highlight-ipython3 notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># evaluate functions at 1000 points evenly spaced in [-15, 15]</span>
<spanclass="n">x</span><spanclass="o">=</span><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">linspace</span><spanclass="p">(</span><spanclass="o">-</span><spanclass="mi">15</span><spanclass="p">,</span><spanclass="mi">15</span><spanclass="p">,</span><spanclass="mi">1000</span><spanclass="p">)</span>
<spanclass="n">f</span><spanclass="o">=</span><spanclass="n">sinc</span><spanclass="p">(</span><spanclass="n">x</span><spanclass="p">)</span>
<spanclass="n">df</span><spanclass="o">=</span><spanclass="n">d_sinc</span><spanclass="p">(</span><spanclass="n">x</span><spanclass="p">)</span>
</pre></div>
</div>
</div>
<divclass="nbinput docutils container">
<divclass="prompt highlight-none notranslate"><divclass="highlight"><pre><span></span>[4]:
</pre></div>
</div>
<divclass="input_area highlight-ipython3 notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># plot the sinc-function and its derivative</span>
<spanclass="n">fig</span><spanclass="p">,</span><spanclass="n">ax</span><spanclass="o">=</span><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">subplots</span><spanclass="p">()</span>
<spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">plot</span><spanclass="p">(</span><spanclass="n">x</span><spanclass="p">,</span><spanclass="n">f</span><spanclass="p">,</span><spanclass="n">color</span><spanclass="o">=</span><spanclass="s2">"red"</span><spanclass="p">,</span><spanclass="n">label</span><spanclass="o">=</span><spanclass="sa">r</span><spanclass="s2">"$sinc(x)$"</span><spanclass="p">)</span>
<spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">plot</span><spanclass="p">(</span><spanclass="n">x</span><spanclass="p">,</span><spanclass="n">df</span><spanclass="p">,</span><spanclass="n">color</span><spanclass="o">=</span><spanclass="s2">"blue"</span><spanclass="p">,</span><spanclass="n">ls</span><spanclass="o">=</span><spanclass="s2">"--"</span><spanclass="p">,</span><spanclass="n">label</span><spanclass="o">=</span><spanclass="sa">r</span><spanclass="s2">"$\frac{d(sinc(x))}</span><spanclass="si">{dx}</span><spanclass="s2">$"</span><spanclass="p">)</span>
<spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">set_title</span><spanclass="p">(</span><spanclass="s2">"Example Notebook Plot"</span><spanclass="p">)</span>
<spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">set_xlabel</span><spanclass="p">(</span><spanclass="sa">r</span><spanclass="s2">"$x$ [radians]"</span><spanclass="p">)</span>
<spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">grid</span><spanclass="p">(</span><spanclass="kc">True</span><spanclass="p">)</span>
<spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">legend</span><spanclass="p">();</span>
</pre></div>
</div>
</div>
<divclass="nboutput nblast docutils container">
<divclass="prompt empty docutils container">
</div>
<divclass="output_area docutils container">
<imgalt="../_images/Module1_GettingStartedWithPython_Jupyter_Notebooks_10_0.png" src="../_images/Module1_GettingStartedWithPython_Jupyter_Notebooks_10_0.png" />
</div>
</div>
<p>Notice that this notebook interface is great for making adjustments to this plot. You can easily change the color or line-style of the plot and redraw it without having to recompute the functions. You simply re-execute the cell containing the plot code. This is especially nice when the numerical computations required to generate the curves are costly.</p>
</div>
<divclass="section" id="Familiarizing-Yourself-with-Jupyter-Notebooks">
<h2>Familiarizing Yourself with Jupyter Notebooks<aclass="headerlink" href="#Familiarizing-Yourself-with-Jupyter-Notebooks" title="Permalink to this headline"></a></h2>
<p>Here, we will review some essential keyboard shortcuts and notebook features, which will enable you to use notebooks competently. A notebook has two modes of operation:</p>
<p><strong>Command Mode (Press</strong><codeclass="docutils literal notranslate"><spanclass="pre"><ESC></span></code><strong>to activate)</strong></p>
<p>When in command mode, you can use keyboard shortcuts to create/delete/cut/paste notebook cells, and to change a cell’s type between code and markdown modes. Your selected cell will be surrounded by a <em>blue</em> border when you are in command mode. For a complete listing of keyboard shortcuts, toward the top of the notebook click <codeclass="docutils literal notranslate"><spanclass="pre">Help</span><spanclass="pre">></span><spanclass="pre">Keyboard</span><spanclass="pre">Shortcuts</span></code>. The most critical shortcuts are:</p>
<ulclass="simple">
<li><p>create a new cell above the current cell: <codeclass="docutils literal notranslate"><spanclass="pre">a</span></code></p></li>
<li><p>create a new cell below the current cell: <codeclass="docutils literal notranslate"><spanclass="pre">b</span></code></p></li>
<li><p>delete the current cell: <codeclass="docutils literal notranslate"><spanclass="pre">dd</span></code></p></li>
<li><p>restart the notebook kernel (kill all executions and erase all defined variables): <codeclass="docutils literal notranslate"><spanclass="pre">00</span></code></p></li>
<li><p>change the current cell’s type to “Code”: <codeclass="docutils literal notranslate"><spanclass="pre">y</span></code></p></li>
<li><p>change the current cell’s type to “Markdown”: <codeclass="docutils literal notranslate"><spanclass="pre">m</span></code></p></li>
</ul>
<p><strong>Edit Mode (Press</strong><codeclass="docutils literal notranslate"><spanclass="pre"><Enter></span></code><strong>to activate)</strong></p>
<p>Edit mode simply permits you to type text into the selected cell. When in edit mode, the current cell will be surrounded by a <em>green</em> border. There are two commands for executing a cell:</p>
<ulclass="simple">
<li><p>execute current cell: <codeclass="docutils literal notranslate"><spanclass="pre"><CTRL>+<ENTER></span></code></p></li>
<li><p>execute current cell and then create a new cell below: <codeclass="docutils literal notranslate"><spanclass="pre"><SHIFT>+<ENTER></span></code></p></li>
</ul>
<p>By default, a cell will be a code-type cell, meaning that its content will be formatted and executed as Python code. Just as you saw when using the IPython notebook, <codeclass="docutils literal notranslate"><spanclass="pre"><TAB></span></code> can be used to perform autocomplete. Additionally, when your cursor is on the name of a Python function in your code, <codeclass="docutils literal notranslate"><spanclass="pre"><SHIFT>+<TAB></span></code> will bring up a small window with the function’s documentations string. This is <em>very</em> useful.</p>
<p><imgalt="Looking up code documentation in a notebook" src="../_images/jp_docstring.PNG" /></p>
<divclass="section" id="Markdown-Cells">
<h3>Markdown Cells<aclass="headerlink" href="#Markdown-Cells" title="Permalink to this headline"></a></h3>
<p>When a cell is set to Markdown type, its content will not be interpreted as Python code. Instead, its contents will be rendered as stylized text. Refer to <aclass="reference external" href="https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet">this page</a> as a reference for various markdown stylings. Using markdown cells allows you to create section and subsection headers for your notebook, you can <aclass="reference external" href="http://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Working%20With%20Markdown%20Cells.html#LaTeX-equations">render LaTeX math
equations</a>, embed images and videos in your notebook, and much more. In fact, every page in this website was created using Jupyter notebooks! Markdown-type cells are tremendously useful for creating high-quality, well-documented Jupyter notebooks. These are perfect for lab-report assignments.</p>
<divclass="admonition warning">
<pclass="admonition-title fa fa-exclamation-circle"><strong>Be Aware of Cell-Type</strong>:</p>
<p>If you ever find that your Python code isn’t being formatted properly, and doesn’t run at all when you execute the cell (you get no output, not even errors), there is a good chance that you have accidentally set the cell to be a ‘Markdown’-type cell! Change the cell back to code-type by entering command mode (<codeclass="docutils literal notranslate"><spanclass="pre"><ESC></span></code>) and pressing <codeclass="docutils literal notranslate"><spanclass="pre">y</span></code>.</p>
</div>
</div>
<divclass="section" id="Using-Jupyter-Notebooks-with-Other-Languages">
<h3>Using Jupyter Notebooks with Other Languages<aclass="headerlink" href="#Using-Jupyter-Notebooks-with-Other-Languages" title="Permalink to this headline"></a></h3>
<p>The Jupyter Notebook does not work exclusively with Python. A “kernel” can be developed in order to provide support for a given programming language in Jupyter. Indeed, kernels for several prominent programming languages are being developed for use with Jupyter:</p>
<ulclass="simple">
<li><p><aclass="reference external" href="https://github.com/IRkernel/IRkernel">R</a></p></li>
<li><p><aclass="reference external" href="https://github.com/JuliaLang/IJulia.jl">Julia</a></p></li>
<li><p><aclass="reference external" href="https://github.com/SciRuby/iruby">Ruby</a></p></li>
<li><p><aclass="reference external" href="https://github.com/QuantStack/xeus-cling">C++</a></p></li>
<li><p><aclass="reference external" href="https://github.com/gibiansky/IHaskell">Haskell</a></p></li>
<li><p><aclass="reference external" href="https://github.com/gopherdata/gophernotes">Go</a></p></li>
</ul>
<p>The ever-growing list of available kernels for use with Jupyter can be found <aclass="reference external" href="https://github.com/jupyter/jupyter/wiki/Jupyter-kernels">here</a>. It should be noted that these efforts are not all equally mature. For instance, whereas the Python and Julia kernels are robust, the Haskell kernel cannot run natively on Windows machines, and the C++ kernel is still in early development, as of writing this.</p>
</div>
<divclass="section" id="Jupyter-Notebook-Support-in-Visual-Studio-Code">
<h3>Jupyter Notebook Support in Visual Studio Code<aclass="headerlink" href="#Jupyter-Notebook-Support-in-Visual-Studio-Code" title="Permalink to this headline"></a></h3>
<p><aclass="reference external" href="https://code.visualstudio.com/docs/datascience/jupyter-notebooks">Visual Studio Code provides native support for Jupyter notebooks</a>. This means that you can now edit Jupyter notebooks within the <aclass="reference external" href="https://www.pythonlikeyoumeanit.com/Module1_GettingStartedWithPython/Getting_Started_With_IDEs_and_Notebooks.html">Visual Studio Code IDE</a>, and that you will benefit from added features like code-completion, debugging, and variable inspection.</p>
</div>
</div>
</div>
</div>
</div>
<footer><divclass="rst-footer-buttons" role="navigation" aria-label="Footer">
<ahref="Informal_Intro_Python.html" class="btn btn-neutral float-left" title="An Informal Introduction to Python" accesskey="p" rel="prev"><spanclass="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<ahref="Getting_Started_With_IDEs_and_Notebooks.html" class="btn btn-neutral float-right" title="Setting Up a Development Environment" accesskey="n" rel="next">Next <spanclass="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<divrole="contentinfo">
<p>© Copyright 2021, Ryan Soklaski.</p>
</div>
Built with <ahref="https://www.sphinx-doc.org/">Sphinx</a> using a
<ahref="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <ahref="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function(){
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>