{ "metadata": { "name": "ipython-tutor" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "from IPython.lib.display import YouTubeVideo\n", "YouTubeVideo(\"4vuW6tQ0218\")" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", " \n", " " ], "output_type": "pyout", "prompt_number": 1, "text": [ "" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "IPython Tutorial\n", "=================\n", "\n", "Presented by [Jeremy Perkins](http://science.gsfc.nasa.gov/sed/index.cfm?fuseAction=people.jumpBio&iphonebookid=25320)\n", "\n", "Originally by [Paul Ivanov](http://pirsquared.org)\n", "\n" ] }, { "cell_type": "code", "collapsed": true, "input": [ "parrot = \"dead\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "parrot" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 3, "text": [ "'dead'" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tab completion: `obj.`" ] }, { "cell_type": "code", "collapsed": true, "input": [ "parrot." ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": " " }, { "cell_type": "markdown", "metadata": {}, "source": [ "Introspection:" ] }, { "cell_type": "code", "collapsed": true, "input": [ "parrot.upper?" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "parrot.upper()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 5, "text": [ "'DEAD'" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": true, "input": [ "import os.path" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Code-level introspection:" ] }, { "cell_type": "code", "collapsed": true, "input": [ "os.path.join??" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Knowing a partial name, find it" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%pylab inline" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline].\n", "For more information, type 'help(pylab)'.\n" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": true, "input": [ "# ends with \"py\"\n", "*py?" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 9 }, { "cell_type": "code", "collapsed": true, "input": [ "# has \"py\" in it\n", "*py*?" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bring out your dead!" ] }, { "cell_type": "code", "collapsed": true, "input": [ "peasant = \"I'm not dead, yet\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "peasant" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 12, "text": [ "\"I'm not dead, yet\"" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "u'' is just a unicode string, which is what IPython uses internally , don't worry about that, you'll be stone dead\n", "\n", "In-n-Out\n", "--------\n", "You can access previous 3 results using `_`, `__`, and `___` (single-, double-, and triple-underscore).\n", "\n", "For all others, you can use either `Out[42]` or `_42`.\n", "\n", "Similarly, for the input strings, access the previous 3 using `_i`, `_ii`, `_iii`.\n", "\n", "For all others, you can use either `In[42]` or `_i42`." ] }, { "cell_type": "code", "collapsed": true, "input": [ "peasant = Out[5]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "peasant" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 14, "text": [ "'DEAD'" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": true, "input": [ "peasant = peasant + _i" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "print peasant" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "DEADpeasant\n" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "%whos" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Variable Type Data/Info\n", "-----------------------------------\n", "YouTubeVideo type \n", "os module .7/lib/python2.7/os.pyc'>\n", "parrot str dead\n", "peasant unicode DEADpeasant\n" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": true, "input": [ "%magic" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 18 }, { "cell_type": "code", "collapsed": true, "input": [ "%reset -f" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "%whos" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Interactive namespace is empty.\n" ] } ], "prompt_number": 20 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Running shell commands" ] }, { "cell_type": "code", "collapsed": false, "input": [ "!ipython --version" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0.13.1\r\n" ] } ], "prompt_number": 21 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Getting shell output back in python" ] }, { "cell_type": "code", "collapsed": false, "input": [ "v = !ipython --version\n", "v" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 22, "text": [ "['0.13.1']" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "!ls" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "00_notebook_tour.ipynb breakout.ipynb\r\n", "01_notebook_introduction.ipynb ipython-tutor.ipynb\r\n", "animation.m4v posterity.py\r\n" ] } ], "prompt_number": 23 }, { "cell_type": "code", "collapsed": true, "input": [ "files = !ls" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 24 }, { "cell_type": "code", "collapsed": true, "input": [ "files?" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "files.l" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 26, "text": [ "['00_notebook_tour.ipynb',\n", " '01_notebook_introduction.ipynb',\n", " 'animation.m4v',\n", " 'breakout.ipynb',\n", " 'ipython-tutor.ipynb',\n", " 'posterity.py']" ] } ], "prompt_number": 26 }, { "cell_type": "code", "collapsed": false, "input": [ "files.s" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 27, "text": [ "'00_notebook_tour.ipynb 01_notebook_introduction.ipynb animation.m4v breakout.ipynb ipython-tutor.ipynb posterity.py'" ] } ], "prompt_number": 27 }, { "cell_type": "code", "collapsed": false, "input": [ "files.n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 28, "text": [ "'00_notebook_tour.ipynb\\n01_notebook_introduction.ipynb\\nanimation.m4v\\nbreakout.ipynb\\nipython-tutor.ipynb\\nposterity.py'" ] } ], "prompt_number": 28 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using python variables back in the shell" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for x in v[0].split('.'):\n", " print x\n", " # the touch command is unix specific, just creates empty files\n", " !touch $x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "13\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] } ], "prompt_number": 29 }, { "cell_type": "code", "collapsed": false, "input": [ "ls" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 animation.m4v\r\n", "00_notebook_tour.ipynb breakout.ipynb\r\n", "01_notebook_introduction.ipynb ipython-tutor.ipynb\r\n", "1 posterity.py\r\n", "13\r\n" ] } ], "prompt_number": 30 }, { "cell_type": "code", "collapsed": true, "input": [ "#cleanup the files we just made\n", "for x in v[0].split('.'):\n", " !rm $x" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [ "ls" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "00_notebook_tour.ipynb breakout.ipynb\r\n", "01_notebook_introduction.ipynb ipython-tutor.ipynb\r\n", "animation.m4v posterity.py\r\n" ] } ], "prompt_number": 32 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Most of the things we've covered can be found in:" ] }, { "cell_type": "code", "collapsed": true, "input": [ "%quickref" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 33 }, { "cell_type": "code", "collapsed": false, "input": [ "%history?" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 34 }, { "cell_type": "code", "collapsed": true, "input": [ "#XXX: Ipython bug?\n", "%history -f posterity.py" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Overwriting file." ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 35 }, { "cell_type": "code", "collapsed": false, "input": [ "!ls" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "00_notebook_tour.ipynb breakout.ipynb\r\n", "01_notebook_introduction.ipynb ipython-tutor.ipynb\r\n", "animation.m4v posterity.py\r\n" ] } ], "prompt_number": 36 }, { "cell_type": "markdown", "metadata": {}, "source": [ "And finally, welcome to python!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import this" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The Zen of Python, by Tim Peters\n", "\n", "Beautiful is better than ugly.\n", "Explicit is better than implicit.\n", "Simple is better than complex.\n", "Complex is better than complicated.\n", "Flat is better than nested.\n", "Sparse is better than dense.\n", "Readability counts.\n", "Special cases aren't special enough to break the rules.\n", "Although practicality beats purity.\n", "Errors should never pass silently.\n", "Unless explicitly silenced.\n", "In the face of ambiguity, refuse the temptation to guess.\n", "There should be one-- and preferably only one --obvious way to do it.\n", "Although that way may not be obvious at first unless you're Dutch.\n", "Now is better than never.\n", "Although never is often better than *right* now.\n", "If the implementation is hard to explain, it's a bad idea.\n", "If the implementation is easy to explain, it may be a good idea.\n", "Namespaces are one honking great idea -- let's do more of those!\n" ] } ], "prompt_number": 37 }, { "cell_type": "code", "collapsed": true, "input": [ "!rm posterity.py" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 38 }, { "cell_type": "code", "collapsed": true, "input": [ "%connect_info" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{\n", " \"stdin_port\": 58657, \n", " \"ip\": \"127.0.0.1\", \n", " \"hb_port\": 58658, \n", " \"key\": \"7bfef4cb-adec-45ec-a070-e572e27c565a\", \n", " \"shell_port\": 58655, \n", " \"iopub_port\": 58656\n", "}\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Paste the above JSON into a file, and connect with:\n", " $> ipython --existing \n", "or, if you are local, you can connect with just:\n", " $> ipython --existing kernel-038504aa-a057-43e8-af01-23e64150e807.json \n", "or even just:\n", " $> ipython --existing \n", "if this is the most recent IPython session you have started.\n" ] } ], "prompt_number": 39 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }