├── .gitignore ├── .learn ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md └── index.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | .ipynb_checkpoints 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /.learn: -------------------------------------------------------------------------------- 1 | tags: 2 | - jupyter 3 | - python 4 | languages: 5 | - python 6 | jupyter_notebook: true 7 | resources: 0 8 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Learn.co Curriculum 2 | 3 | We're really exited that you're about to contribute to the [open curriculum](https://learn.co/content-license) on [Learn.co](https://learn.co). If this is your first time contributing, please continue reading to learn how to make the most meaningful and useful impact possible. 4 | 5 | ## Raising an Issue to Encourage a Contribution 6 | 7 | If you notice a problem with the curriculum that you believe needs improvement 8 | but you're unable to make the change yourself, you should raise a Github issue 9 | containing a clear description of the problem. Include relevant snippets of 10 | the content and/or screenshots if applicable. Curriculum owners regularly review 11 | issue lists and your issue will be prioritized and addressed as appropriate. 12 | 13 | ## Submitting a Pull Request to Suggest an Improvement 14 | 15 | If you see an opportunity for improvement and can make the change yourself go 16 | ahead and use a typical git workflow to make it happen: 17 | 18 | * Fork this curriculum repository 19 | * Make the change on your fork, with descriptive commits in the standard format 20 | * Open a Pull Request against this repo 21 | 22 | A curriculum owner will review your change and approve or comment on it in due 23 | course. 24 | 25 | # Why Contribute? 26 | 27 | Curriculum on Learn is publicly and freely available under Learn's 28 | [Educational Content License](https://learn.co/content-license). By 29 | embracing an open-source contribution model, our goal is for the curriculum 30 | on Learn to become, in time, the best educational content the world has 31 | ever seen. 32 | 33 | We need help from the community of Learners to maintain and improve the 34 | educational content. Everything from fixing typos, to correcting 35 | out-dated information, to improving exposition, to adding better examples, 36 | to fixing tests—all contributions to making the curriculum more effective are 37 | welcome. 38 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # Learn.co Educational Content License 2 | 3 | Copyright (c) 2018 Flatiron School, Inc 4 | 5 | The Flatiron School, Inc. owns this Educational Content. However, the Flatiron 6 | School supports the development and availability of educational materials in 7 | the public domain. Therefore, the Flatiron School grants Users of the Flatiron 8 | Educational Content set forth in this repository certain rights to reuse, build 9 | upon and share such Educational Content subject to the terms of the Educational 10 | Content License set forth [here](http://learn.co/content-license) 11 | (http://learn.co/content-license). You must read carefully the terms and 12 | conditions contained in the Educational Content License as such terms govern 13 | access to and use of the Educational Content. 14 | 15 | Flatiron School is willing to allow you access to and use of the Educational 16 | Content only on the condition that you accept all of the terms and conditions 17 | contained in the Educational Content License set forth 18 | [here](http://learn.co/content-license) (http://learn.co/content-license). By 19 | accessing and/or using the Educational Content, you are agreeing to all of the 20 | terms and conditions contained in the Educational Content License. If you do 21 | not agree to any or all of the terms of the Educational Content License, you 22 | are prohibited from accessing, reviewing or using in any way the Educational 23 | Content. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Jupyter Notebook CodeAlong 3 | 4 | ### Introduction 5 | 6 | So far, many of the readings and all of the labs you have been working with have been interactive. You are working in an environment that allows us to both display text and run Python code. In this lesson, we explore Jupyter, the software powering these interactive documents. 7 | 8 | ### Jupyter Background 9 | 10 | Jupyter is a web application that allows someone to create and work with documents that have live code. It's a very popular tool among data scientists, as it allows for both explanation of thinking behind code as well as the code itself. 11 | 12 | ### Introduction to cells 13 | 14 | The notebook itself consists of cells. Double click on this content to see what I mean. Once we double click on a cell, we are in insert mode. This means that we are able to edit the cells, just as you would if this were a word document. We can tell that we are in insert mode because of the green border around the cell. 15 | 16 | After entering insert mode for this cell, change some content. Don't worry about what you change as we can always undo it. We can revert our changes to a cell by making sure that we are still in insert mode and by pressing `command + z` on a mac or `control + z` on windows. 17 | 18 | To get out of insert mode and see the effect of our changes, press `shift + enter`. 19 | 20 | ### Adding and Deleting Cells 21 | 22 | We have already seen, to alter the contents of a cell we simply double click on that cell, bringing us into insert mode, and then change the contents. Now let's see how to add, and delete cells from escape mode. 23 | 24 | #### Adding cells 25 | 26 | If we wish to quickly add a new cell we can do so with the following steps: 27 | 28 | * Make sure we are not in insert mode, but in escape mode 29 | * *Remember we can tell we are in insert mode when we have a green border around our cell.* 30 | * To get out of insert mode and into escape mode, press shift + enter. Another option is to press the escape key. 31 | * You will no longer see a cell bordered in green. 32 | * Then press the letter `b` to create a new cell. 33 | 34 | #### Deleting cells 35 | 36 | To delete a cell we once again should be in escape mode, and then press the `x` key. 37 | 38 | Of course, we'll want a way to undo our deletion. From escape mode, you can press `z` to undo deletion of a cell. Note that this is different from `cmd + z`. Pressing `cmd + z` while in insert mode undoes any changes inside of a cell while, whether these changes be deletions or text insertions. Pressing `z` from escape mode undoes the deletion of a cell. 39 | 40 | Go to escape mode and press `x`. This cell disappears! 41 | 42 | Then bring it back with `z`. 43 | 44 | ### Types of Cells 45 | 46 | The current cell and every other cell in this lesson has been a markdown cell, meaning that it allows us to write text and stylize that text. For example, if you surround some text with stars on either side the text **becomes bold**. That's markdown. 47 | 48 | Cells can also have a type of code. If we are writing in a cell that is for Python code, everything in that cell must be valid Python or we will see an error. 49 | 50 | 51 | ```python 52 | This is a python cell without valid Python so we will see an error 53 | ``` 54 | 55 | So, a cell must either be of type markdown or of type code, in which case all of the contents must be valid Python. It cannot be both. We can quickly change a cell from markdown to code with some keyboard shortcuts. 56 | 57 | From escape mode, we change a cell to type code by pressing the letter `y`. 58 | 59 | Anytime we create a new cell, say with the shortcut key `b`, the new cell will default to code mode. We can switch to escape mode and press the letter `m` to change the cell from code to markdown. 60 | 61 | #### View All Shortcuts 62 | 63 | Press the key `h` while in escape mode to view the menu for all of Jupyter's shortcuts. 64 | 65 | ### Working with Python in Jupyter 66 | 67 | Ok, now that we know a little bit about adding and deleting cells, as well as changing cell types from markdown to code, let's focus on working with Python in Jupyter. We'll go into a large amount of detail about working with a Jupyter notebook in Python, but the main takeaway is this: if we see a Python cell, we should press `shift + enter` on that cell. 68 | 69 | The major gotcha in working with Python code is that we must execute the cells in order for Python to register the code in them. So for example, just seeing the cell where we define `name` to `'bob'` below does not write that cell to memory. 70 | 71 | 72 | ```python 73 | name = 'bob' 74 | ``` 75 | 76 | If we try to reference that variable later on without having executed the cell, Python will tell us that it is not defined. 77 | 78 | 79 | ```python 80 | name 81 | ``` 82 | 83 | 84 | --------------------------------------------------------------------------- 85 | 86 | NameError Traceback (most recent call last) 87 | 88 | in () 89 | ----> 1 name 90 | 91 | 92 | NameError: name 'name' is not defined 93 | 94 | 95 | To execute or run a cell, we must press `shift + enter` from escape mode on that cell. Upon running a cell, Python will show the the last line of the cell's return value underneath. Let's run the cell below to see this: 96 | 97 | 98 | ```python 99 | age = 14 100 | age 101 | ``` 102 | 103 | 104 | 105 | 106 | 14 107 | 108 | 109 | 110 | As you can see the variable `age` is set to 14, so when the cell is run `14` is displayed underneath. 111 | 112 | One tricky thing to note is that assignment, the action of assigning a variable, **does not** have a return a value. So, even though the cell is run, if the last line of cell is the assigning of a variable, nothing is displayed underneath. 113 | 114 | 115 | ```python 116 | hometown = 'NYC' 117 | ``` 118 | 119 | Notice, even after pressing shift + enter on the code above, nothing is displayed below. But if we reference the variable `hometown`, we see that the cell was run as the variable was defined. 120 | 121 | 122 | ```python 123 | hometown 124 | ``` 125 | 126 | 127 | 128 | 129 | 'NYC' 130 | 131 | 132 | 133 | > Yes, it's pretty confusing, but the important thing to take away is that we need to run our cells with Python code by pressing `shift + enter` if we want Python to read our variables and functions and remember them later on. Remember, in the case of assignment, the return value is `None`, which does not show an output. We can see this more concretely below by running the cell below: 134 | 135 | 136 | ```python 137 | None 138 | ``` 139 | 140 | ### Working through labs and readmes 141 | 142 | As you read through labs we encourage you to press shift + enter on each of the Python cells. Often in labs, we will assign variables to data early on. Then we will ask you to work with data stored in those variables. To avoid going to the top of the lab to press `shift + enter`, it's best just to press that while reading along. 143 | 144 | The same thing goes for working through a Readme. The Readmes will often assign data to variables, and it may be nice to work with data stored in those variables later on. So it's a good idea to press `shift + enter`, and now that you know how to work with Jupyter notebooks, you can always modify the notebooks with new code to test your understanding. 145 | 146 | ### Summary 147 | 148 | In this lesson, we learned about Jupyter notebooks. We saw that in Jupyter notebooks, we can either be in insert mode or escape mode. While in insert mode, we can edit the cells and undo changes within that cell with `cmd + z` on a mac or `ctl + z` on windows. In escape mode, we can add cells with `b`, delete a cell with `x`, and undo deletion of a cell with `z`. We can also change the type of a cell to markdown with `m` and to Python code with `y`. 149 | 150 | Then we saw how to work with Python code in Jupyter notebooks. We saw that to have our code in a cell executed, we need to press `shift + enter`. If we do not do this, then our variables that we assigned in Python are not going to be recognized by Python later on in our Jupyter notebook. 151 | -------------------------------------------------------------------------------- /index.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Jupyter Notebook CodeAlong" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### Introduction" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "So far, many of the readings and all of the labs you have been working with have been interactive. You are working in an environment that allows us to both display text and run Python code. In this lesson, we explore Jupyter, the software powering these interactive documents." 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "### Jupyter Background" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "Jupyter is a web application that allows someone to create and work with documents that have live code. It's a very popular tool among data scientists, as it allows for both explanation of thinking behind code as well as the code itself." 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "### Introduction to cells" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "The notebook itself consists of cells. Double click on this content to see what I mean. Once we double click on a cell, we are in insert mode. This means that we are able to edit the cells, just as you would if this were a word document. We can tell that we are in insert mode because of the green border around the cell. \n", 50 | "\n", 51 | "After entering insert mode for this cell, change some content. Don't worry about what you change as we can always undo it. We can revert our changes to a cell by making sure that we are still in insert mode and by pressing `command + z` on a mac or `control + z` on windows.\n", 52 | "\n", 53 | "To get out of insert mode and see the effect of our changes, press `shift + enter`." 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "### Adding and Deleting Cells" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "We have already seen, to alter the contents of a cell we simply double click on that cell, bringing us into insert mode, and then change the contents. Now let's see how to add, and delete cells from escape mode." 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "#### Adding cells" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "If we wish to quickly add a new cell we can do so with the following steps: \n", 82 | "\n", 83 | "* Make sure we are not in insert mode, but in escape mode\n", 84 | " * *Remember we can tell we are in insert mode when we have a green border around our cell.*\n", 85 | " * To get out of insert mode and into escape mode, press shift + enter. Another option is to press the escape key.\n", 86 | " * You will no longer see a cell bordered in green.\n", 87 | "* Then press the letter `b` to create a new cell." 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "#### Deleting cells" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "To delete a cell we once again should be in escape mode, and then press the `x` key.\n", 102 | "\n", 103 | "Of course, we'll want a way to undo our deletion. From escape mode, you can press `z` to undo deletion of a cell. Note that this is different from `cmd + z`. Pressing `cmd + z` while in insert mode undoes any changes inside of a cell while, whether these changes be deletions or text insertions. Pressing `z` from escape mode undoes the deletion of a cell.\n", 104 | "\n", 105 | "Go to escape mode and press `x`. This cell disappears!\n", 106 | "\n", 107 | "Then bring it back with `z`." 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "### Types of Cells\n", 115 | "\n", 116 | "The current cell and every other cell in this lesson has been a markdown cell, meaning that it allows us to write text and stylize that text. For example, if you surround some text with stars on either side the text **becomes bold**. That's markdown.\n", 117 | "\n", 118 | "Cells can also have a type of code. If we are writing in a cell that is for Python code, everything in that cell must be valid Python or we will see an error." 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": null, 124 | "metadata": { 125 | "collapsed": true 126 | }, 127 | "outputs": [], 128 | "source": [ 129 | "This is a python cell without valid Python so we will see an error" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": { 135 | "collapsed": true 136 | }, 137 | "source": [ 138 | "So, a cell must either be of type markdown or of type code, in which case all of the contents must be valid Python. It cannot be both. We can quickly change a cell from markdown to code with some keyboard shortcuts.\n", 139 | "\n", 140 | "From escape mode, we change a cell to type code by pressing the letter `y`.\n", 141 | "\n", 142 | "Anytime we create a new cell, say with the shortcut key `b`, the new cell will default to code mode. We can switch to escape mode and press the letter `m` to change the cell from code to markdown." 143 | ] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": {}, 148 | "source": [ 149 | "#### View All Shortcuts\n", 150 | "\n", 151 | "Press the key `h` while in escape mode to view the menu for all of Jupyter's shortcuts." 152 | ] 153 | }, 154 | { 155 | "cell_type": "markdown", 156 | "metadata": {}, 157 | "source": [ 158 | "### Working with Python in Jupyter" 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": {}, 164 | "source": [ 165 | "Ok, now that we know a little bit about adding and deleting cells, as well as changing cell types from markdown to code, let's focus on working with Python in Jupyter. We'll go into a large amount of detail about working with a Jupyter notebook in Python, but the main takeaway is this: if we see a Python cell, we should press `shift + enter` on that cell. " 166 | ] 167 | }, 168 | { 169 | "cell_type": "markdown", 170 | "metadata": {}, 171 | "source": [ 172 | "The major gotcha in working with Python code is that we must execute the cells in order for Python to register the code in them. So for example, just seeing the cell where we define `name` to `'bob'` below does not write that cell to memory." 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": null, 178 | "metadata": { 179 | "collapsed": true 180 | }, 181 | "outputs": [], 182 | "source": [ 183 | "name = 'bob'" 184 | ] 185 | }, 186 | { 187 | "cell_type": "markdown", 188 | "metadata": {}, 189 | "source": [ 190 | "If we try to reference that variable later on without having executed the cell, Python will tell us that it is not defined. " 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 4, 196 | "metadata": {}, 197 | "outputs": [ 198 | { 199 | "ename": "NameError", 200 | "evalue": "name 'name' is not defined", 201 | "output_type": "error", 202 | "traceback": [ 203 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 204 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 205 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mname\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 206 | "\u001b[0;31mNameError\u001b[0m: name 'name' is not defined" 207 | ] 208 | } 209 | ], 210 | "source": [ 211 | "name" 212 | ] 213 | }, 214 | { 215 | "cell_type": "markdown", 216 | "metadata": {}, 217 | "source": [ 218 | "To execute or run a cell, we must press `shift + enter` from escape mode on that cell. Upon running a cell, Python will show the the last line of the cell's return value underneath. Let's run the cell below to see this:" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 2, 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "data": { 228 | "text/plain": [ 229 | "14" 230 | ] 231 | }, 232 | "execution_count": 2, 233 | "metadata": {}, 234 | "output_type": "execute_result" 235 | } 236 | ], 237 | "source": [ 238 | "age = 14\n", 239 | "age" 240 | ] 241 | }, 242 | { 243 | "cell_type": "markdown", 244 | "metadata": {}, 245 | "source": [ 246 | "As you can see the variable `age` is set to 14, so when the cell is run `14` is displayed underneath.\n", 247 | "\n", 248 | "One tricky thing to note is that assignment, the action of assigning a variable, **does not** have a return a value. So, even though the cell is run, if the last line of cell is the assigning of a variable, nothing is displayed underneath. " 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": 3, 254 | "metadata": { 255 | "collapsed": true 256 | }, 257 | "outputs": [], 258 | "source": [ 259 | "hometown = 'NYC'" 260 | ] 261 | }, 262 | { 263 | "cell_type": "markdown", 264 | "metadata": {}, 265 | "source": [ 266 | "Notice, even after pressing shift + enter on the code above, nothing is displayed below. But if we reference the variable `hometown`, we see that the cell was run as the variable was defined." 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 4, 272 | "metadata": {}, 273 | "outputs": [ 274 | { 275 | "data": { 276 | "text/plain": [ 277 | "'NYC'" 278 | ] 279 | }, 280 | "execution_count": 4, 281 | "metadata": {}, 282 | "output_type": "execute_result" 283 | } 284 | ], 285 | "source": [ 286 | "hometown" 287 | ] 288 | }, 289 | { 290 | "cell_type": "markdown", 291 | "metadata": {}, 292 | "source": [ 293 | "> Yes, it's pretty confusing, but the important thing to take away is that we need to run our cells with Python code by pressing `shift + enter` if we want Python to read our variables and functions and remember them later on. Remember, in the case of assignment, the return value is `None`, which does not show an output. We can see this more concretely below by running the cell below:" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": 6, 299 | "metadata": { 300 | "collapsed": true 301 | }, 302 | "outputs": [], 303 | "source": [ 304 | "None" 305 | ] 306 | }, 307 | { 308 | "cell_type": "markdown", 309 | "metadata": {}, 310 | "source": [ 311 | "### Working through labs and readmes " 312 | ] 313 | }, 314 | { 315 | "cell_type": "markdown", 316 | "metadata": {}, 317 | "source": [ 318 | "As you read through labs we encourage you to press shift + enter on each of the Python cells. Often in labs, we will assign variables to data early on. Then we will ask you to work with data stored in those variables. To avoid going to the top of the lab to press `shift + enter`, it's best just to press that while reading along. \n", 319 | "\n", 320 | "The same thing goes for working through a Readme. The Readmes will often assign data to variables, and it may be nice to work with data stored in those variables later on. So it's a good idea to press `shift + enter`, and now that you know how to work with Jupyter notebooks, you can always modify the notebooks with new code to test your understanding." 321 | ] 322 | }, 323 | { 324 | "cell_type": "markdown", 325 | "metadata": {}, 326 | "source": [ 327 | "### Summary" 328 | ] 329 | }, 330 | { 331 | "cell_type": "markdown", 332 | "metadata": {}, 333 | "source": [ 334 | "In this lesson, we learned about Jupyter notebooks. We saw that in Jupyter notebooks, we can either be in insert mode or escape mode. While in insert mode, we can edit the cells and undo changes within that cell with `cmd + z` on a mac or `ctl + z` on windows. In escape mode, we can add cells with `b`, delete a cell with `x`, and undo deletion of a cell with `z`. We can also change the type of a cell to markdown with `m` and to Python code with `y`. \n", 335 | "\n", 336 | "Then we saw how to work with Python code in Jupyter notebooks. We saw that to have our code in a cell executed, we need to press `shift + enter`. If we do not do this, then our variables that we assigned in Python are not going to be recognized by Python later on in our Jupyter notebook." 337 | ] 338 | } 339 | ], 340 | "metadata": { 341 | "kernelspec": { 342 | "display_name": "Python 3", 343 | "language": "python", 344 | "name": "python3" 345 | }, 346 | "language_info": { 347 | "codemirror_mode": { 348 | "name": "ipython", 349 | "version": 3 350 | }, 351 | "file_extension": ".py", 352 | "mimetype": "text/x-python", 353 | "name": "python", 354 | "nbconvert_exporter": "python", 355 | "pygments_lexer": "ipython3", 356 | "version": "3.6.4" 357 | } 358 | }, 359 | "nbformat": 4, 360 | "nbformat_minor": 2 361 | } 362 | --------------------------------------------------------------------------------