This is a Jupyter notebook. You can learn more about Jupyter notebooks here, but you only need to know a few things to get started.
In: [*]
. After a cell completes executing, the asterisk will be replaced with a sequence number.Let's try it out now! Do you know what the next snippet of Python code will evaluate to? Execute it to find out.
d = {0: 'f', 1: 'is', 2: 'a', 3: 'n', 4: 're', 5: 's', 6: 'th', 7: 'u'}
ls = ["".join([d[i] for i in ls]) for ls in [[6,1],[5,7,4],[1],[0,7,3]]]
ls
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.
dir(x)
will tell you all of the names of the members of x
. For example,
import string
dir(string)
will list all of the members of the string
module. Try it out now!
import string
dir(string)
You can also run dir
on objects that aren't modules, like strings or other class instances.
✅ Edit the next cell to try it out.
notebooks = "this sure is fun"
# add a line here to see what members a string
# object has and execute this cell
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: ✅
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
GREETING="hello"
to
GREETING="bonjour"
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.
Run the next cell to read documentation for the zfill
function.
help(notebooks.zfill)
Now edit the previous cell to get help for another function that you learned about by listing the members of the notebooks
string.
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.
ls.reverse()
ls
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.)
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.
def foo(x):
return "sorry, I don't know anything about %r" % x
help = foo
help("foo")
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.
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.
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.