{ "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.4.3" }, "name": "", "signature": "sha256:50b1b1afa6a23f8a4b348ac7cc36927e99531320b1197acbdc8ce6c64a01fd54" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Using the IPython notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The IPython *notebook* is a new feature that has been implemented into IPython, and allows you to write notebooks similar to e.g. Mathematica. The advantage of doing this is that you can include text, code, and plots in the same document. This makes it ideal for example to write up a report about a project that uses mostly Python code, in order to share with others. In fact, the notes for this course are written using the IPython notebook!" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Starting up" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "CIP Pool" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you are using a computer in the CIP pool, you will need to add the following line:\n", "\n", " export PATH=/local/py4sci/anaconda3/bin:$PATH\n", "\n", "to a file called ``.bashrc`` in your home directory. This ensures that the Anaconda Python Distribution is used by default. Then start the notebook with:\n", "\n", " start-ipython-notebook\n", " \n", "This is a special script that I wrote that will ensure that you all run different notebooks and don't accidentally access other people's notebooks." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Laptops/Other computers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The normal way to start up the IPython notebook is to open a shell, go to the directory containing your notebooks and say:\n", "\n", " ipython notebook\n", " \n", "Once you do this, your web browser should open and go to a page showing a list of folders and notebooks." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "First steps" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Click on ``New Notebook`` on the right, which will start a new document. You can change the name of the document by clicking on the **Untitled** name at the top and entering a new name. Make sure you then save the document (make sure that you save regularly as you might lose content if you close the browser window!).\n", "\n", "At first glance, a notebook looks like a fairly typical application - it has a menubar (File, Edit, View, etc.) and a tool bar with icons. Below this, you will see an empty cell, in which you can type any Python code. You can write several lines of code, and once it is ready to run, you can press shift-enter and it will get executed:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 1\n", "print(a)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can then click on that cell, change the Python code, and press shift-enter again to re-execute the code. Once you have executed a cell once, a new cell will appear below. You can again enter some code, then press shift-enter to execute it." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Plotting" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To make plots, enter any Matplotlib commands (see later lectures), and just press shift-enter - note that all commands for a plot should be entered in one cell, you cannot split it up over multiple cells:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "plt.plot([1,2,3],[4,5,6])\n", "plt.xlabel(\"x\")\n", "plt.ylabel(\"y\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "" ] }, { "metadata": {}, "output_type": "display_data", "png": "iVBORw0KGgoAAAANSUhEUgAAAYYAAAEPCAYAAABGP2P1AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAEGRJREFUeJzt3W+oZHd9x/H3ZxPbmAQNomytm5IWDdQHxWhdU7cxF2nF\nLJoHJQ98UEPyQBeLGigKpX/IylpF4mIqpRrqWoK1DWJoTNstTdJ6JVAMCfnT1E2K1lgS0Vjqf1OK\nq98+uJPtzOz9M3funDPnnHm/YMjMnLMzP07O3t99nzNnNlWFJEnP2rfsAUiSusWJQZI0wYlBkjTB\niUGSNMGJQZI0wYlBkjSh8YkhyUVJPpvksSSnkly+yTofTfL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d8likhbEYpDkluRP4K+CXgBdX1buWPCRpITrztdtSnyS5Fvjfqrpt9HXH/7LF\nv5ol9Y7FIEma4DkGSdIEJwZJ0gQnBknSBCcGSdIEJwZJ0gQnBknSBCcGSdIEJwZJ0oT/A31S+uEg\nTQsGAAAAAElFTkSuQmCC\n", "text": [ "" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "As before, you can always go back and edit the cell to re-make the plot. If you want to save it, make sure you include ``plt.savefig(filename)`` as the last command, where ``filename`` is the name of the plot, such as ``my_plot.png``." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is likely that you will want to enter actual text (non-code) in the notebook. To do this, click on a cell, and in the drop-down menu in the toolbar, select 'Markdown'. This is a specific type of syntax for writing text. You can just write text normally and press shift-enter to *render* it:\n", "\n", " This is some plain text\n", "\n", "To edit it, double click on the cell. You can also enter section headings using the following syntax:\n", "\n", " This is a title\n", " ===============\n", "\n", " This is a sub-title\n", " -------------------\n", "\n", "which will look like:\n", "\n", "This is a title\n", "===============\n", "\n", "This is a sub-title\n", "-------------------\n", "\n", "Finally, if you are familiar with LaTeX, you can enter equations using:\n", "\n", " $$E = m c^2$$\n", "\n", "on a separate line, or:\n", "\n", " The equation $p=h/\\lambda$ is very important\n", "\n", "to include it in a sentence. This will look like:\n", "\n", "$$E = m c^2$$\n", "\n", "The equation $p=h/\\lambda$ is very important\n", "\n", "For more information about using LaTeX for equations, see [this guide](http://en.wikibooks.org/wiki/LaTeX/Mathematics)." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Splitting/deleting/moving cells" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can split, delete, and move cells by going to 'Edit' and selecting the appropriate command. Some of the commands are also available in the toolbar - put your mouse over the icon and wait for a second, and it will tell you what it does." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Sharing your notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "IPython notebooks are plain text files \u2013 you can view them with a text editor, and other people can dump them into their own folders to re-run them.\n", "\n", "Find the files under the name you gave the notebook, with an ipynb extension. When you hand in solutions later, please send these files as attachments (whitespace matters, so they don't work terribly well in mail bodies).\n", "\n", "Of course, if you know version control, the notebooks are diff- and merge-friendly. If you are already using, say, github: try it with your notebooks." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Important notes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A few important notes about using the notebook:\n", "\n", "* Save often! There is an auto-save in the notebook, but better to also save explicitly from time to time.\n", "\n", "* Code *can* be executed in an order different from top to bottom, but note that if you do this variables will not be reset. So for example if you type:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 1" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "then go higher up and type:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(a)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "it will give the value you previously set. To make sure that your code works from top to bottom, go to the 'Cell' menu item and go to **All Output** -> **Clear** then in the **Cell** menu, select **Run All**.\n", "\n", "In addition, even if you remove a cell, then variables set in that cell still exist unless you restart the notebook. If you want to restart a notebook, you can select **Kernel** -> **Restart**. This removes any variables from memory, and you have to start running the notebook from the start." ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Another word of warning" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ipython notebooks contain \u2013 suprise \u2013 code. Executing a notebook of unknown origin is dangerous. From within such a notebook, people can do\n", "anything you can do, e.g., delete files, create backdoors, etc.\n", "\n", "The notebook runner will not execute untrusted code *without your command*. If you see code you don't understand (in particular if it contains calls into modules like os, subprocess, sys, and such): Keep your\n", "hands off shift-Enter and friends." ] } ], "metadata": {} } ] }