{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Getting started with Jupyter notebooks\n", "\n", "This is a [Jupyter](https://jupyter.org/) notebook. You can learn more about Jupyter notebooks [here](https://jupyter.readthedocs.io), but you only need to know a few things to get started.\n", "\n", "1. Jupyter notebooks are made up of _cells_ that can contain prose, executable code, or interactive UI elements. This cell is a prose cell.\n", "2. Cells can be edited and executed. To edit a cell, double-click until the frame around it turns green. To execute a cell (whether you've edited it or not), select it and press Shift+Enter. This will execute the cell, record its output, and advance to the next cell. \n", "3. While a cell is executing, it will have an asterisk in the margin, like `In: [*]`. After a cell completes executing, the asterisk will be replaced with a sequence number.\n", "\n", "\n", "Let's try it out now! Do you know what the next snippet of Python code will evaluate to? Execute it to find out." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d = {0: 'f', 1: 'is', 2: 'a', 3: 'n', 4: 're', 5: 's', 6: 'th', 7: 'u'}\n", "ls = [\"\".join([d[i] for i in ls]) for ls in [[6,1],[5,7,4],[1],[0,7,3]]]\n", "ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you've used Python before, you might know about the `dir` and `help` built-in functions. These are a great way to learn about the functions and classes you'll be using without leaving the notebook environment.\n", "\n", "`dir(x)` will tell you all of the names of the members of `x`. For example, \n", "\n", " import string\n", " dir(string)\n", " \n", "will list all of the members of the `string` module. Try it out now!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import string\n", "dir(string)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also run `dir` on objects that aren't modules, like strings or other class instances. \n", "\n", "✅ Edit the next cell to try it out." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "notebooks = \"this sure is fun\"\n", "\n", "# add a line here to see what members a string \n", "# object has and execute this cell\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sidebar: interactivity\n", "\n", "In the notebooks we'll be working on today, there will be many opportunities to change cells and see how small code changes influence model and pipeline performance. We'll call out several places where we've made it especially easy to try new things with a checkmark emoji, like this: ✅\n", "\n", "You just did that, so you'll notice it next time! In most of the other notebooks, the changes you'll want to make will involve assigning different values to constants, like changing\n", "\n", "`GREETING=\"hello\"`\n", "\n", "to \n", "\n", "`GREETING=\"bonjour\"`\n", "\n", "# Getting help\n", "\n", "The `help` built-in function will give you documentation for a member. Sometimes you'll know the name of the member that you're interested in, but sometimes you're looking for the right function to do something in particular. Often you can find what you're looking for by using `dir` and `help` together.\n", "\n", "Run the next cell to read documentation for the `zfill` function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "help(notebooks.zfill)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now edit the previous cell to get help for another function that you learned about by listing the members of the `notebooks` string.\n", "\n", "# Editing and executing: some pitfalls\n", "\n", "As you've just seen, you don't _have_ to execute cells only once, or even in order, but most of the notebooks you'll interact with will assume you do (and you should develop notebooks that work properly when cells are only executed once and in order). Let's see how this might be confusing. Run the next cell once." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ls.reverse()\n", "ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run the previous cell again. Why might running cells more than once be confusing if you're sharing a notebook? (Hint: think about sharing the outputs as well as the inputs.)\n", "\n", "# Starting over fresh\n", "\n", "Next up, we'll see how to start over when we need to. Say you make a mistake in your notebook and overwrite a variable declaration that you need. Run the next cell." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def foo(x):\n", " return \"sorry, I don't know anything about %r\" % x\n", "\n", "help = foo\n", "\n", "help(\"foo\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Oh, no! We've overwritten the definition of the `help` built-in function. Fortunately, there are ways to get it back (e.g., try `__builtins__.help(\"foo\")`), but we'll use this as an opportunity to learn how to clean up after our mistake.\n", "\n", "First, edit the previous cell, deleting the line `help = foo`. Then, go to the drop-down menu and select `Kernel -> Restart & clear all output`. This will restart the Python interpreter, meaning that any variables we've defined will be reset and any modules we've imported will no longer be available.\n", "\n", "It's a good idea to run `Kernel -> Restart & clear all output` before committing a notebook to source control -- it'll be cleaner and the diffs will be smaller. It's also a good idea to run `Kernel -> Restart & run all` before publishing a notebook or sharing it with a friend, so that you can be more sure that your notebook will produce the results you expect when the cells are run in order." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }