{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Running Code in the IPython Notebook" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "Based on [this notebook](http://nbviewer.ipython.org/url/github.com/ipython/ipython/raw/master/examples/notebooks/Part%201%20-%20Running%20Code.ipynb) with very minor changes.\n", "\n", "---------\n", "\n", "First and foremost, the IPython Notebook is an interactive environment for writing and running Python code." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "## Code cells allow you to enter and run Python code" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "Run a code cell using `Shift-Enter` or pressing the \"Play\" button in the toolbar above:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "a = 10" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## All of the goodness of IPython works" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here are two system aliases:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'/Users/fangohr/hg/teaching-python/notebook'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pwd" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "IPython-beyond-plain-Python-Fernando-Perez.ipynb\r\n", "IPythonNotebookIntroduction.html\r\n", "IPythonNotebookIntroduction.ipynb\r\n", "Makefile\r\n", "Matplotlib.ipynb\r\n", "Testing-intro.ipynb\r\n", "helloworld.py\r\n", "inverse-function-through-rootfinding.ipynb\r\n", "lab1.ipynb\r\n", "lab1.pdf\r\n", "lab2.ipynb\r\n", "lab3.ipynb\r\n", "lab4.ipynb\r\n", "lab5.ipynb\r\n", "lab6.ipynb\r\n", "lab7.ipynb\r\n", "lab8.ipynb\r\n", "same-or-differents-object.ipynb\r\n", "speed-map-vs-for-loop.ipynb\r\n", "update-web.sh\r\n" ] } ], "source": [ "ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Any command line program can be run using `!` with string interpolation from Python variables:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The IPython notebook is great!\r\n" ] } ], "source": [ "message = 'The IPython notebook is great!'\n", "# note: the echo command does not run on Windows, it's a unix command.\n", "!echo $message" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tab completion works:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "numpy." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Shift-Tab completion after `(` brings up a tooltip with the docstring:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "numpy.random.rand(" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Adding `?` opens the docstring in the pager below:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [], "source": [ "magic?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Exceptions are formatted nicely:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "division by zero", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mz\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" ] } ], "source": [ "x = 1\n", "y = 4\n", "z = y/(1-x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Working with external code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are a number of ways of getting external code into code cells." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pasting code with `>>>` prompts works as expected:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Be careful not to fall off!\n" ] } ], "source": [ ">>> the_world_is_flat = True\n", ">>> if the_world_is_flat:\n", "... print(\"Be careful not to fall off!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `%load` magic lets you load code from URLs or local files:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%load?" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%load http://matplotlib.org/mpl_examples/statistics/histogram_demo_features.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating a file on the fly\n", "\n", "Using the ``%%file`` magic, we can directly create a file" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing helloworld.py\n" ] } ], "source": [ "%%file helloworld.py\n", "print(\"Hello World\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and can see the file on the file system:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-rw-r--r-- 1 fangohr staff 20 16 Sep 14:43 helloworld.py\r\n" ] } ], "source": [ "!ls -l helloworld.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and execute the file using the python interpreter:\n" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\r\n" ] } ], "source": [ "!python helloworld.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Type markdown for text and latex\n", "\n", "For example, if $x=\\alpha$ then $x^2 = \\alpha^2$. Furthermore \n", "$$ \\int\\limits_a^b f(x) \\mathrm{d} x = \\Gamma.$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Further reading\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "https://github.com/ipython/ipython/blob/1.x/examples/notebooks/README.md" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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 }