-
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>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" /><meta content="Topic: Jupyter notebooks, Difficulty: Easy, Category: Tutorial" name="description" />
<meta content="jupyter, jupyter lab, notebook, kernel, basics, server, console, plot, beginner, data science" name="keywords" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Jupyter Notebooks — Python Like You Mean It</title>
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../_static/my_theme.css" type="text/css" />
<!--[if lt IE 9]>
<script src="../_static/js/html5shiv.min.js"></script>
<![endif]-->
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script async="async" src="https://www.googletagmanager.com/gtag/js?id=UA-115029372-1"></script>
<script src="../_static/gtag.js"></script>
<script crossorigin="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>
<script defer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<script src="../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Setting Up a Development Environment" href="Getting_Started_With_IDEs_and_Notebooks.html" />
<link rel="prev" title="An Informal Introduction to Python" href="Informal_Intro_Python.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../index.html" class="icon icon-home"> Python Like You Mean It
</a>
<div class="version">
1.4
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<p class="caption" role="heading"><span class="caption-text">Table of Contents:</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../intro.html">Python Like You Mean It</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../module_1.html">Module 1: Getting Started with Python</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="SiteFormatting.html">A Quick Guide to Formatting</a></li>
<li class="toctree-l2"><a class="reference internal" href="GettingStartedWithPython.html">Introducing the Python Programming Language</a></li>
<li class="toctree-l2"><a class="reference internal" href="Installing_Python.html">Installing Python</a></li>
<li class="toctree-l2"><a class="reference internal" href="Informal_Intro_Python.html">An Informal Introduction to Python</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Jupyter Notebooks</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#Jupyter-Lab">Jupyter Lab</a></li>
<li class="toctree-l3"><a class="reference internal" href="#Running-a-Notebook-Server-&-Creating-a-Notebook">Running a Notebook Server & Creating a Notebook</a></li>
<li class="toctree-l3"><a class="reference internal" href="#Notebook-Cells">Notebook Cells</a></li>
<li class="toctree-l3"><a class="reference internal" href="#An-Example-Notebook">An Example Notebook</a></li>
<li class="toctree-l3"><a class="reference internal" href="#Familiarizing-Yourself-with-Jupyter-Notebooks">Familiarizing Yourself with Jupyter Notebooks</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#Markdown-Cells">Markdown Cells</a></li>
<li class="toctree-l4"><a class="reference internal" href="#Using-Jupyter-Notebooks-with-Other-Languages">Using Jupyter Notebooks with Other Languages</a></li>
<li class="toctree-l4"><a class="reference internal" href="#Jupyter-Notebook-Support-in-Visual-Studio-Code">Jupyter Notebook Support in Visual Studio Code</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="Getting_Started_With_IDEs_and_Notebooks.html">Setting Up a Development Environment</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../module_2.html">Module 2: The Essentials of Python</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_2_problems.html">Module 2: Problems</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_3.html">Module 3: The Essentials of NumPy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_3_problems.html">Module 3: Problems</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_4.html">Module 4: Object Oriented Programming</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_5.html">Module 5: Odds and Ends</a></li>
<li class="toctree-l1"><a class="reference internal" href="../changes.html">Changelog</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">Python Like You Mean It</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home"></a> »</li>
<li><a href="../module_1.html">Module 1: Getting Started with Python</a> »</li>
<li>Jupyter Notebooks</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/Module1_GettingStartedWithPython/Jupyter_Notebooks.md.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<style>
/* CSS for nbsphinx extension */
/* remove conflicting styling from Sphinx themes */
div.nbinput.container div.prompt *,
div.nboutput.container div.prompt *,
div.nbinput.container div.input_area pre,
div.nboutput.container div.output_area pre,
div.nbinput.container div.input_area .highlight,
div.nboutput.container div.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.container div.prompt *,
div.nboutput.container div.prompt * {
background: none;
}
div.nboutput.container div.output_area .highlight,
div.nboutput.container div.output_area pre {
background: unset;
}
div.nboutput.container div.output_area div.highlight {
color: unset; /* override Pygments text color */
}
/* avoid gaps between output lines */
div.nboutput.container div[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.container div.prompt pre {
color: #307FC1;
}
/* output prompt */
div.nboutput.container div.prompt pre {
color: #BF5B3D;
}
/* all prompts */
div.nbinput.container div.prompt,
div.nboutput.container div.prompt {
width: 4.5ex;
padding-top: 5px;
position: relative;
user-select: none;
}
div.nbinput.container div.prompt > div,
div.nboutput.container div.prompt > div {
position: absolute;
right: 0;
margin-right: 0.3ex;
}
@media (max-width: 540px) {
div.nbinput.container div.prompt,
div.nboutput.container div.prompt {
width: unset;
text-align: left;
padding: 0.4em;
}
div.nboutput.container div.prompt.empty {
padding: 0;
}
div.nbinput.container div.prompt > div,
div.nboutput.container div.prompt > div {
position: unset;
}
}
/* disable scrollbars on prompts */
div.nbinput.container div.prompt pre,
div.nboutput.container div.prompt pre {
overflow: hidden;
}
/* input/output area */
div.nbinput.container div.input_area,
div.nboutput.container div.output_area {
-webkit-flex: 1;
flex: 1;
overflow: auto;
}
@media (max-width: 540px) {
div.nbinput.container div.input_area,
div.nboutput.container div.output_area {
width: 100%;
}
}
/* input area */
div.nbinput.container div.input_area {
border: 1px solid #e0e0e0;
border-radius: 2px;
/*background: #f5f5f5;*/
}
/* override MathJax center alignment in output cells */
div.nboutput.container div[class*=MathJax] {
text-align: left !important;
}
/* override sphinx.ext.imgmath center alignment in output cells */
div.nboutput.container div.math p {
text-align: left;
}
/* standard error */
div.nboutput.container div.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.container div.input_area div[class*=highlight] > pre,
div.nboutput.container div.output_area div[class*=highlight] > pre,
div.nboutput.container div.output_area div[class*=highlight].math,
div.nboutput.container div.output_area.rendered_html,
div.nboutput.container div.output_area > div.output_javascript,
div.nboutput.container div.output_area:not(.rendered_html) > img{
padding: 5px;
margin: 0;
}
/* fix copybtn overflow problem in chromium (needed for 'sphinx_copybutton') */
div.nbinput.container div.input_area > div[class^='highlight'],
div.nboutput.container div.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_html table {
border: none;
border-collapse: collapse;
border-spacing: 0;
color: black;
font-size: 12px;
table-layout: fixed;
}
div.rendered_html thead {
border-bottom: 1px solid black;
vertical-align: bottom;
}
div.rendered_html tr,
div.rendered_html th,
div.rendered_html td {
text-align: right;
vertical-align: middle;
padding: 0.5em 0.5em;
line-height: normal;
white-space: normal;
max-width: none;
border: none;
}
div.rendered_html th {
font-weight: bold;
}
div.rendered_html tbody tr:nth-child(odd) {
background: #f5f5f5;
}
div.rendered_html tbody tr: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>
<div class="section" id="Jupyter-Notebooks">
<h1>Jupyter Notebooks<a class="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 <a class="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>
<div class="section" id="Jupyter-Lab">
<h2>Jupyter Lab<a class="headerlink" href="#Jupyter-Lab" title="Permalink to this headline"></a></h2>
<p><a class="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 <a class="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 <a class="reference external" href="https://www.pythonlikeyoumeanit.com/Module1_GettingStartedWithPython/Installing_Python.html">installed via Anaconda</a>.</p>
</div>
<div class="section" id="Running-a-Notebook-Server-&-Creating-a-Notebook">
<h2>Running a Notebook Server & Creating a Notebook<a class="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 <code class="docutils literal notranslate"><span class="pre"><ENTER></span></code>): <code class="docutils literal notranslate"><span class="pre">jupyter</span> <span class="pre">notebook</span></code></p>
<p>Alternatively, if you want to work in Jupyter lab, run: <code class="docutils literal notranslate"><span class="pre">jupyter</span> <span class="pre">lab</span></code></p>
<p>You should see some text appear in your terminal:</p>
<p><img alt="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><img alt="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 <code class="docutils literal notranslate"><span class="pre">Python</span> <span class="pre">3</span></code>.</p>
<p><img alt="image0" src="../_images/jp_dropdown.PNG" /></p>
<p>A new tab will open in your browser, revealing a “Jupyter notebook” called <code class="docutils literal notranslate"><span class="pre">Untitled.ipynb</span></code> running a Python 3 kernel. Clicking <code class="docutils literal notranslate"><span class="pre">File</span> <span class="pre">></span> <span class="pre">Rename</span></code> in the notebook will enable you to name your notebook. <code class="docutils literal notranslate"><span class="pre">.ipynb</span></code> is the file-type suffix used for Jupyter notebooks (<code class="docutils literal notranslate"><span class="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>
<div class="admonition warning">
<p class="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>
<div class="section" id="Notebook-Cells">
<h2>Notebook Cells<a class="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>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="mi">3</span>
<span class="n">y</span> <span class="o">=</span> <span class="mi">4</span>
</pre></div>
</div>
<p>then press <code class="docutils literal notranslate"><span class="pre"><SHIFT>+<ENTER></span></code>. This will execute all of the code within the given cell (in this instance, assigning the variables <code class="docutils literal notranslate"><span class="pre">x</span></code> and <code class="docutils literal notranslate"><span class="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>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span>
</pre></div>
</div>
<p>and hit <code class="docutils literal notranslate"><span class="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 <code class="docutils literal notranslate"><span class="pre">3</span> <span class="pre">+</span> <span class="pre">4</span></code> is evaluated:</p>
<p><img alt="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 <code class="docutils literal notranslate"><span class="pre">z</span> <span class="pre">=</span> <span class="pre">2</span></code> in the third cell, and then execute code that references <code class="docutils literal notranslate"><span class="pre">z</span></code> in the first cell. What really matters is the <em>order</em> in which the cells are executed. Notice that <code class="docutils literal notranslate"><span class="pre">In[1]</span></code> denotes that the top cell with the first input-cell executed in the notebook, and <code class="docutils literal notranslate"><span class="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 <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">3</span></code> to <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">=</span> <span class="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>
<div class="section" id="An-Example-Notebook">
<h2>An Example Notebook<a class="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>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[1]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="c1"># this tells Jupyter to embed matplotlib plots in the notebook</span>
<span class="o">%</span><span class="k">matplotlib</span> inline
</pre></div>
</div>
</div>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[2]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">sinc</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">/</span> <span class="n">x</span>
<span class="k">def</span> <span class="nf">d_sinc</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="s2">"derivative of sinc-function"</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">/</span><span class="n">x</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">/</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span>
</pre></div>
</div>
</div>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[3]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># evaluate functions at 1000 points evenly spaced in [-15, 15]</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="mi">15</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">1000</span><span class="p">)</span>
<span class="n">f</span> <span class="o">=</span> <span class="n">sinc</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">d_sinc</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[4]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># plot the sinc-function and its derivative</span>
<span class="n">fig</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">()</span>
<span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s2">"red"</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="sa">r</span><span class="s2">"$sinc(x)$"</span><span class="p">)</span>
<span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">df</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s2">"blue"</span><span class="p">,</span> <span class="n">ls</span><span class="o">=</span><span class="s2">"--"</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="sa">r</span><span class="s2">"$\frac{d(sinc(x))}</span><span class="si">{dx}</span><span class="s2">$"</span><span class="p">)</span>
<span class="n">ax</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s2">"Example Notebook Plot"</span><span class="p">)</span>
<span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="sa">r</span><span class="s2">"$x$ [radians]"</span><span class="p">)</span>
<span class="n">ax</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
<span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">();</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<img alt="../_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>
<div class="section" id="Familiarizing-Yourself-with-Jupyter-Notebooks">
<h2>Familiarizing Yourself with Jupyter Notebooks<a class="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> <code class="docutils literal notranslate"><span class="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 <code class="docutils literal notranslate"><span class="pre">Help</span> <span class="pre">></span> <span class="pre">Keyboard</span> <span class="pre">Shortcuts</span></code>. The most critical shortcuts are:</p>
<ul class="simple">
<li><p>create a new cell above the current cell: <code class="docutils literal notranslate"><span class="pre">a</span></code></p></li>
<li><p>create a new cell below the current cell: <code class="docutils literal notranslate"><span class="pre">b</span></code></p></li>
<li><p>delete the current cell: <code class="docutils literal notranslate"><span class="pre">dd</span></code></p></li>
<li><p>restart the notebook kernel (kill all executions and erase all defined variables): <code class="docutils literal notranslate"><span class="pre">00</span></code></p></li>
<li><p>change the current cell’s type to “Code”: <code class="docutils literal notranslate"><span class="pre">y</span></code></p></li>
<li><p>change the current cell’s type to “Markdown”: <code class="docutils literal notranslate"><span class="pre">m</span></code></p></li>
</ul>
<p><strong>Edit Mode (Press</strong> <code class="docutils literal notranslate"><span class="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>
<ul class="simple">
<li><p>execute current cell: <code class="docutils literal notranslate"><span class="pre"><CTRL>+<ENTER></span></code></p></li>
<li><p>execute current cell and then create a new cell below: <code class="docutils literal notranslate"><span class="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, <code class="docutils literal notranslate"><span class="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, <code class="docutils literal notranslate"><span class="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><img alt="Looking up code documentation in a notebook" src="../_images/jp_docstring.PNG" /></p>
<div class="section" id="Markdown-Cells">
<h3>Markdown Cells<a class="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 <a class="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 <a class="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>
<div class="admonition warning">
<p class="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 (<code class="docutils literal notranslate"><span class="pre"><ESC></span></code>) and pressing <code class="docutils literal notranslate"><span class="pre">y</span></code>.</p>
</div>
</div>
<div class="section" id="Using-Jupyter-Notebooks-with-Other-Languages">
<h3>Using Jupyter Notebooks with Other Languages<a class="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>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/IRkernel/IRkernel">R</a></p></li>
<li><p><a class="reference external" href="https://github.com/JuliaLang/IJulia.jl">Julia</a></p></li>
<li><p><a class="reference external" href="https://github.com/SciRuby/iruby">Ruby</a></p></li>
<li><p><a class="reference external" href="https://github.com/QuantStack/xeus-cling">C++</a></p></li>
<li><p><a class="reference external" href="https://github.com/gibiansky/IHaskell">Haskell</a></p></li>
<li><p><a class="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 <a class="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>
<div class="section" id="Jupyter-Notebook-Support-in-Visual-Studio-Code">
<h3>Jupyter Notebook Support in Visual Studio Code<a class="headerlink" href="#Jupyter-Notebook-Support-in-Visual-Studio-Code" title="Permalink to this headline"></a></h3>
<p><a class="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 <a class="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><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
<a href="Informal_Intro_Python.html" class="btn btn-neutral float-left" title="An Informal Introduction to Python" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="Getting_Started_With_IDEs_and_Notebooks.html" class="btn btn-neutral float-right" title="Setting Up a Development Environment" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<div role="contentinfo">
<p>© Copyright 2021, Ryan Soklaski.</p>
</div>
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>