|
8 | 8 | "\n", |
9 | 9 | "\n", |
10 | 10 | "**Learning Objectives**:\n", |
11 | | - "- Launch the Jupyter Notebook, create new notebooks, and exit the Notebook.\n", |
12 | | - "- Create and run Python cells in a notebook.\n", |
13 | | - "- Learn key Python formatting principles\n", |
| 11 | + "- Create, open, and exit notebooks.\n", |
| 12 | + "- Edit and run Python cells in a notebook.\n", |
| 13 | + "- Learn key Python formatting principles.\n", |
14 | 14 | "* * * * *" |
15 | 15 | ] |
16 | 16 | }, |
|
20 | 20 | "source": [ |
21 | 21 | "## Navigating Jupyter Notebooks\n", |
22 | 22 | "\n", |
23 | | - "In Jupyter Notebooks, code is typed into cells. Cells are individual units in which code can be separately run. In contrast to many IDEs, running code is done with a keyword combination: `Shift+Enter`. Running `Shift+Enter` on a selected cell will run the code in the cell and then automatically move your cursor to the following cell.\n", |
| 23 | + "In Jupyter Notebooks, code is divided into cells which can each be run separately. This is the main distinction between Jupyter Notebook `.ipynb` format and Python script `.py` format. Running a cell is done with a key combination: `Shift+Enter`. `Shift+Enter` will run the code in the selecte cell and then automatically move to the following cell.\n", |
24 | 24 | "\n", |
25 | | - "Try to run the following code using `Shift+Enter` now." |
| 25 | + "Try to run the following code using `Shift+Enter` now. \n", |
| 26 | + "\n", |
| 27 | + "**Question:** What was the output of the code?" |
26 | 28 | ] |
27 | 29 | }, |
28 | 30 | { |
29 | 31 | "cell_type": "code", |
30 | | - "execution_count": null, |
| 32 | + "execution_count": 1, |
31 | 33 | "metadata": {}, |
32 | | - "outputs": [], |
| 34 | + "outputs": [ |
| 35 | + { |
| 36 | + "name": "stdout", |
| 37 | + "output_type": "stream", |
| 38 | + "text": [ |
| 39 | + "Hello World!\n" |
| 40 | + ] |
| 41 | + } |
| 42 | + ], |
33 | 43 | "source": [ |
34 | 44 | "print(\"Hello World!\")" |
35 | 45 | ] |
|
38 | 48 | "cell_type": "markdown", |
39 | 49 | "metadata": {}, |
40 | 50 | "source": [ |
41 | | - "If you hit **Enter** only, Jupyter Notebook gives you another line in the current cell.\n", |
| 51 | + "If you hit `Enter` only, Jupyter Notebook gives you another line in the current cell.\n", |
42 | 52 | "\n", |
43 | | - "This allows you to compose multi-line commands and submit them to Python all at once." |
| 53 | + "This allows you to compose multi-line commands and submit them to Python all at once.\n", |
| 54 | + "\n", |
| 55 | + "**Question:** What does the following cell do? What is the output?" |
44 | 56 | ] |
45 | 57 | }, |
46 | 58 | { |
47 | 59 | "cell_type": "code", |
48 | | - "execution_count": null, |
| 60 | + "execution_count": 2, |
49 | 61 | "metadata": {}, |
50 | | - "outputs": [], |
| 62 | + "outputs": [ |
| 63 | + { |
| 64 | + "name": "stdout", |
| 65 | + "output_type": "stream", |
| 66 | + "text": [ |
| 67 | + "3\n" |
| 68 | + ] |
| 69 | + } |
| 70 | + ], |
51 | 71 | "source": [ |
52 | 72 | "a = 1 + 2\n", |
53 | 73 | "print(a)" |
|
66 | 86 | "cell_type": "markdown", |
67 | 87 | "metadata": {}, |
68 | 88 | "source": [ |
69 | | - "Try using `Control+Enter` to run this cell a few times. What happens?" |
| 89 | + "**Question**: Try using `Control+Enter` to run this cell three times. What is the output? Run the cell one more time. Is the output the same?" |
70 | 90 | ] |
71 | 91 | }, |
72 | 92 | { |
|
83 | 103 | "cell_type": "markdown", |
84 | 104 | "metadata": {}, |
85 | 105 | "source": [ |
86 | | - "If you want to create new empty cells, you can use Insert -> Insert Cell Below or use the Insert Cell Below button at the top of the notebook. Try entering a new cell below this one." |
| 106 | + "If you want to create new empty cells, you can use Insert ==> Insert Cell Below or use the Insert Cell Below button at the top of the notebook. Try entering a new cell below this one." |
87 | 107 | ] |
88 | 108 | }, |
89 | 109 | { |
|
92 | 112 | "source": [ |
93 | 113 | "## Markdown\n", |
94 | 114 | "\n", |
95 | | - "Jupyter notebooks allow you type in Markdown as well as code. In fact, this very cell is written in Markdown! We use markdown to narrate the workshop and provide context. Markdown is also used for documentation of code in Python notebooks more generally.\n", |
| 115 | + "Jupyter notebooks allow you combine text and code using a system called markdown. In fact, this very cell is written in markdown! We use this formatting language to narrate the workshop and provide context. (Imagine reading this notebook with no markdown!) Markdown is also used for documentation of code in Python notebooks more generally.\n", |
96 | 116 | "\n", |
97 | | - "Markdown has its own syntax, but it's easy to learn. Here's a [cheatsheet](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) that can help.\n", |
| 117 | + "Markdown has its own syntax, but it's fairly straighforward. Here's a [cheatsheet](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) that can help. You can also double-click on any of the markdown cells to see how they are made.\n", |
98 | 118 | "\n", |
99 | | - "Double click the cell below to see the markdown code rendering the output." |
| 119 | + "Double click the cell below to see the markdown code rendering the output. Then do `Shift+Enter` to go back to the formatted text." |
100 | 120 | ] |
101 | 121 | }, |
102 | 122 | { |
|
105 | 125 | "source": [ |
106 | 126 | "## Clearing Jupyter\n", |
107 | 127 | "\n", |
108 | | - "Jupyter remembers everything it executed, **even if it's not currently displayed in the notebook**.\n", |
| 128 | + "Jupyter remembers line of code it executed, **even if it's not currently displayed in the notebook**. This means that deleting a line of code does not delete it from the notebook's memory if it has already been run. Instead, to clear everything from Jupyter use Kernel -> Restart in the menu. The kernel is basically the program actually running the code, so if you reset the kernel, it's as if you just opened up the notebook for the first time. All of the outputs are forgotten, and the variables are reset.\n", |
109 | 129 | "\n", |
110 | | - "To clear everything from Jupyter use Kernel -> Restart in the menu." |
| 130 | + "Let's see how this actually works. First, run the cell below. What is the output?" |
111 | 131 | ] |
112 | 132 | }, |
113 | 133 | { |
114 | 134 | "cell_type": "code", |
115 | | - "execution_count": null, |
| 135 | + "execution_count": 6, |
116 | 136 | "metadata": {}, |
117 | | - "outputs": [], |
| 137 | + "outputs": [ |
| 138 | + { |
| 139 | + "name": "stdout", |
| 140 | + "output_type": "stream", |
| 141 | + "text": [ |
| 142 | + "And three shall be the count.\n" |
| 143 | + ] |
| 144 | + } |
| 145 | + ], |
118 | 146 | "source": [ |
119 | 147 | "mystring = \"And three shall be the count.\" \n", |
120 | 148 | "\n", |
|
125 | 153 | "cell_type": "markdown", |
126 | 154 | "metadata": {}, |
127 | 155 | "source": [ |
128 | | - "Now use Kernel -> Restart in the menu! You can also press the \"Reset\" button in the icon bar." |
| 156 | + "Now use Kernel -> Restart in the menu! You can also press the \"Reset\" button in the icon bar. Then run the code below. What happens?" |
129 | 157 | ] |
130 | 158 | }, |
131 | 159 | { |
132 | 160 | "cell_type": "code", |
133 | | - "execution_count": null, |
| 161 | + "execution_count": 7, |
134 | 162 | "metadata": {}, |
135 | | - "outputs": [], |
| 163 | + "outputs": [ |
| 164 | + { |
| 165 | + "name": "stdout", |
| 166 | + "output_type": "stream", |
| 167 | + "text": [ |
| 168 | + "And three shall be the count.\n" |
| 169 | + ] |
| 170 | + } |
| 171 | + ], |
136 | 172 | "source": [ |
137 | 173 | "print(mystring)" |
138 | 174 | ] |
|
185 | 221 | "source": [ |
186 | 222 | "### Commenting\n", |
187 | 223 | "\n", |
188 | | - "We will discuss how and why to comment code later in this series, but it's also useful when you temporarily don't want to run a section of code.\n", |
| 224 | + "We will discuss how and why to comment code later in this series, but we will introduce it now because it's useful when you temporarily don't want to run a section of code.\n", |
189 | 225 | "\n", |
190 | 226 | "Simply place a pound sign `#` at the beginning of the line, and that line won't run. Any uncommented lines will be treated as code.\n", |
191 | 227 | "\n", |
192 | | - "Try running the cell below, then comment out `bad_thing`, and run it again.\n" |
| 228 | + "Try running the cell below, then comment out `bad_thing`, and run it again. What changes?\n" |
193 | 229 | ] |
194 | 230 | }, |
195 | 231 | { |
|
257 | 293 | "name": "python", |
258 | 294 | "nbconvert_exporter": "python", |
259 | 295 | "pygments_lexer": "ipython3", |
260 | | - "version": "3.8.12" |
| 296 | + "version": "3.9.12" |
261 | 297 | }, |
262 | 298 | "toc": { |
263 | 299 | "base_numbering": 1, |
|
0 commit comments