{ "metadata": { "name": "ipython-intro" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ " Introduction to the IPython notebook" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Basics\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* The notebook consists of individual cells\n", "* Each cell may contain code or text (amongst other options)\n", "* Text format can be [markdown](http://daringfireball.net/projects/markdown), HTML or LaTeX\n", "* Code cells are run using:\n", " * **shift + enter** will move to next cell\n", " * **ctrl + enter** will stay in the current cell\n", "* Markdown cells can be modified by double-clicking on them" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"Hello IPython notebook!\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Only the last statement will be shown\n", "L = ['red', 'blue', 'green', 'black', 'white']\n", "type(L)\n", "#sorted(L) # uncomment this line to show the sorted list instead of the type" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# An example code cell using stuff from pylab\n", "\n", "# numpy's linspace and pi\n", "x = linspace(0, 3*pi, 500)\n", "\n", "# matplotlib's plot and title\n", "plot(x, sin(x**2))\n", "title('A simple chirp');" ], "language": "python", "metadata": { "slideshow": { "slide_start": false } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Errors lead to detailed tracebacks\n", "x = \"Hello IPython!\"\n", "\n", "y = 42\n", "\n", "z = x + y" ], "language": "python", "metadata": { "slideshow": { "slide_start": false } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Echo the version of IPython\n", "import IPython\n", "IPython.__version__" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Exercise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Download the IPython notebook file\n", "* Execute `ipython notebook --pylab=inline` to launch the notebook\n", "* Execute the cells in the above section using both **shift + enter** and **control + enter**\n", "* Make a tiny modification to one of the cells and execute it again" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Navigation and cell manipulation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* You can:\n", " * Select/add/delete/move/insert cells\n", "\n", "* Using:\n", " * Menu\n", " * Toolbar\n", " * Keyboard shortcuts (via the Help menu)" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Exercise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Create a markdown cell, a header cell and a code cell\n", "* Move the cells around using Edit-> Move Cell\n", "* Cut and paste a cell to a different location\n", "* Use the menu, the toolbar and look at the keyboard shortcuts\n", "* Try **shift + enter** when on the last cell in the notebook. What happens?" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Markdown Cells" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Markdown basics: lists, markup and code\n", "\n", "* list item \n", "* list item\n", " * nested list item\n", "\n", "\n", "* *italics*\n", "* **bold**\n", "* `fixed_font`\n", "\n", "Code examples:\n", "\n", " def hello_ipython():\n", " print \"Hello IPython!\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Markdown cells can also contain HTML\n", "\n", "

Markdown basics: lists, markup and code

\n", "\n", "\n", "\n", "

Code examples:

\n", "\n", "
def hello_ipython():\n",
      "    print \"Hello IPython!\"\n",
      "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using math mode (anything between two `$`s or two `$$`s) is interpreted as $LaTeX$ math:\n", "\n", "$$ D_{KL}(P||Q) = \\sum\\limits_{i}ln (\\frac{P(i)}{Q(i)}) P(i)$$" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Modify or create a markdown cell and use:\n", " * Markdown\n", " * HTML\n", " * LateX math\n", "\n", "* Use either `$` or ``$$`` for the math expressions. What is the difference?\n", "\n", "* Look at the [markdown syntax page](http://daringfireball.net/projects/markdown/syntax#span) (or this cell) to find out how to create hyperlinks" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Help" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Help on python objects using the '?'\n", "import IPython\n", "IPython?" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Using the built-in help\n", "help(IPython)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Double click on the divider to close the help console\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Tab completion is available\n", "IPython. # press TAB after the period" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The *Help* menu item has links to IPython and the scientific packages" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Loading and Saving" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Dashboard shows all notebooks in the directory where it was started from\n", "* Use the dashboard to create new notebooks\n", "* Files saved as JSON with the ``.ipynb`` extension\n", "* Not meant for human consumption\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Use File -> Print View for a version without the controls\n", "* Execute the cells who's output you want to show\n", "* Save to HTML or print to PDF" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* When running from someone else's server, use File -> Download\n", "* Either as ``*.ipynb`` file\n", "* Or as ``*.py`` file with the markdown as comments\n", "* [``*.py`` file can also be imported](http://ipython.org/ipython-doc/dev/interactive/htmlnotebook.html#exporting-a-notebook-and-importing-existing-scripts)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* There is also the online [nbviewer](http://nbviewer.ipython.org/)\n", "* This can render a static version of a publicly available notebook." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Create a new notebook from the dashboard\n", "* Look at a saved ``*.ipynb`` file. Can you decipher it at least somewhat.\n", "* Save the notebook as ``*.py`` file. What does this look like in the editor?\n", "* Find an IPython notebook online and try it with the nbviewer site." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Images and Video" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import Image\n", "\n", "Image(filename='images/logo.png')" ], "language": "python", "metadata": { "slideshow": { "slide_start": false } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import YouTubeVideo\n", "\n", "YouTubeVideo('F4rFuIb1Ie4')" ], "language": "python", "metadata": { "slideshow": { "slide_start": false } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Alternatively, load an image from a URL\n", "Image(url='http://octodex.github.com/images/pythocat.png')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Things I haven't covered" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Hiding output\n", "* Debugging by attaching a QTConsole\n", "* Restarting the kernel in case of crashes\n", "* Cython magic\n", "\n", "* [In-depth IPython tutorial this afternoon](https://conference.scipy.org/scipy2013/tutorial_detail.php?id=104)" ] } ], "metadata": {} } ] }