{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# A brief introduction to the Jupyter Notebook\n", "-------------------------------------------------------------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "

Learning Objectives

\n", "
\n", "
\n", "\n", "> - become familiar with the notebook interface\n", "> - know how to **run code** and **get help** in the notebook\n", "> - learn how to **format text**, **display images** in the notebook\n", "> - learn how to present **equations** in the notebook\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Notebook Basics\n", "\n", "> **Note: if you are using Google Colab, skip straight to \"The Notebook UI** \n", "\n", "## The notebook dashboard\n", "\n", "*Note: the notebook dashboard is only used on managed desktop machines, or your own computer. In Sage Math Cloud, you create notebooks as you would any other file.*\n", "\n", "We've already seen how to start the notebook server. When you first start the notebook server, your browser will open to the notebook dashboard. The dashboard serves as a home page for the notebook. Its main purpose is to display the notebooks and files in the **current directory**. When you first start the server, it will show the directory in which you started the notebook server.\n", "\n", "\n", "\n", "To create a new notebook, click on the \"New\" button at the top of the list and select a notebook type from the dropdown (as seen below). Which types are listed depend on what's installed on the server. Some of the notebook types in the screenshot below may not exist as an option to you. \n", "\n", "\n", "\n", "Notebooks and files can be uploaded to the current directory by dragging a notebook file onto the notebook list or by the \"upload\" text above the list.\n", "\n", "The notebook list shows green \"Running\" text and a green notebook icon next to running notebooks (as seen below). **Notebooks remain running until you explicitly shut them down; closing the notebook's page is not sufficient.** To see all of your running notebooks along with their directories, click on the \"Running\" tab: \n", "\n", "\n", "\n", "You can shutdown notebooks from here. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "

Exercise

\n", "
\n", "
\n", "\n", "> Start the notebook server, and create and open a new notebook so you can play along with this tutorial.\n", "\n", "---\n", "# Notebooks and Kernels\n", "---\n", "\n", "Any code you type in a notebook gets run by a *kernel*. You can actually run much more than Python code in a notebook, just by changing the kernel. The \"Kernel\" menu in the notebook allows you to set the kernel." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "

Exercise

\n", "
\n", "
\n", "\n", "> Make sure your kernel is set to Python3 by selecting \"Kernel -> Change kernel -> Python3\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "# The notebook UI\n", "---\n", "\n", "## Cell types\n", "The notebook is composed of cells, which you can add, delete, edit and move around. Each cell can be a particular **type**. The two you really need are *code* cells and *markdown* cells. Code cells should be self-explanatory, but markdown cells are used to create formatted text - like the text you're reading now.\n", "\n", "The cell type is controlled by the drop-down box in the toolbar.\n", "\n", "## Run code\n", "\n", "This is the most important bit! In the notebook, to run a cell of code, hit Shift-Enter or press the button in the toolbar above. This executes the cell and puts the cursor in the next cell below, or makes a new one if you are at the end. Alternately, you can use:\n", "\n", "* Alt-Enter to always add a new cell below\n", "* Ctrl-Enter to run the cell and keep the cursor in the cell." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n" ] } ], "source": [ "print(\"hello\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "

Exercise

\n", "
\n", "
\n", "\n", "> Enter some Python code to print out your name in the notebook, and run it." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get Help\n", "\n", "Typing ```some_thing?``` will print out detailed help about that thing, and is a really nice way of getting help on how to use libraries you may import, e.g." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import math\n", "math.sin?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also get help by typing ```help(some_thing)```. Unlike the example above, this will display the help in the notebook itself, e.g." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function sin in module math:\n", "\n", "sin(...)\n", " sin(x)\n", " \n", " Return the sine of x (measured in radians).\n", "\n" ] } ], "source": [ "help(math.sin)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tab completion\n", "\n", "A very nice feature of the notebook is that if you begin to type the name of something and hit Tab, it will complete the name of that object. This also works for members of a library, or for file and directory names.\n", "\n", "
\n", "
\n", "
\n", "

Exercise

\n", "
\n", "
\n", "\n", "> Use the cell below, and tab completion to see what the ```math``` library contains. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "math." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", ">Notice how when you run cells a little number pops up next to them? That keeps track of which cell was executed when. Behind the notebook is an instance of the IPython interpreter, which runs your code and keeps track of what you've done. Thus, the following works:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = 3\n", "y = 2" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "print(x+y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> In the notebook you can run cells *out of order*, or run cells again!. This can produce very confusing results. If your notebook gets into such a confused state, it can be useful to stop the IPython interpreter (hit the button in the toolbar) and run the cells again in order.\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Other Stuff\n", "---\n", "\n", " - There are useful commands in the ```Edit``` menu for cutting, pasting and moving cells around.\n", " - Extensive help, for the notebook and Python more generally, is in the ```Help``` menu, unsuprisingly." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "
\n", "
\n", "

Conclusions

\n", "
\n", "
\n", "\n", "> - we've learned how to run the notebook server and create new notebooks\n", "> - we've learned how to move and edit cells\n", "> - we've learned how to run code cells and get help\n", "\n", "> Next, we'll look at using markdown cells to produce formatted text, equations and display images." ] } ], "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.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }