{ "metadata": { "name": "ipython-tutor" }, "nbformat": 2, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "from IPython.lib.display import YouTubeVideo", "YouTubeVideo(\"4vuW6tQ0218\")" ], "language": "python", "outputs": [ { "html": [ "", " ", " " ], "output_type": "pyout", "prompt_number": 1, "text": [ "" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "source": [ "IPython Tutorial", "=================", "", "By [Paul Ivanov](http://pirsquared.org)", "", "" ] }, { "cell_type": "code", "collapsed": true, "input": [ "parrot = \"dead\"" ], "language": "python", "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "parrot" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 2, "text": [ "'dead'" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "source": [ "Tab completion: `obj.`" ] }, { "cell_type": "code", "collapsed": true, "input": [ "parrot." ], "language": "python", "outputs": [], "prompt_number": " " }, { "cell_type": "markdown", "source": [ "Introspection:" ] }, { "cell_type": "code", "collapsed": true, "input": [ "parrot.upper?" ], "language": "python", "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "parrot.upper()" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 4, "text": [ "'DEAD'" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": true, "input": [ "import os.path" ], "language": "python", "outputs": [], "prompt_number": 5 }, { "cell_type": "markdown", "source": [ "Code-level introspection:" ] }, { "cell_type": "code", "collapsed": true, "input": [ "os.path.join??" ], "language": "python", "outputs": [], "prompt_number": 6 }, { "cell_type": "markdown", "source": [ "Knowing a partial name, find it" ] }, { "cell_type": "code", "collapsed": true, "input": [ "# ends with \"py\"", "*py?" ], "language": "python", "outputs": [], "prompt_number": 7 }, { "cell_type": "code", "collapsed": true, "input": [ "# has \"py\" in it", "*py*?" ], "language": "python", "outputs": [], "prompt_number": 8 }, { "cell_type": "markdown", "source": [ "Bring out your dead!" ] }, { "cell_type": "code", "collapsed": true, "input": [ "peasant = \"I'm not dead, yet\"" ], "language": "python", "outputs": [], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "peasant" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 10, "text": [ "\"I'm not dead, yet\"" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "source": [ "u'' is just a unicode string, which is what IPython uses internally , don't worry about that, you'll be stone dead", "", "In-n-Out", "--------", "You can access previous 3 results using `_`, `__`, and `___` (single-, double-, and triple-underscore).", "", "For all others, you can use either `Out[42]` or `_42`.", "", "Similarly, for the input strings, access the previous 3 using `_i`, `_ii`, `_iii`.", "", "For all others, you can use either `In[42]` or `_i42`." ] }, { "cell_type": "code", "collapsed": true, "input": [ "peasant = Out[4]" ], "language": "python", "outputs": [], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "peasant" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 12, "text": [ "'DEAD'" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": true, "input": [ "peasant = peasant + _i" ], "language": "python", "outputs": [], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "peasant" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 14, "text": [ "u'DEADpeasant'" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "%whos" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Variable Type Data/Info", "-------------------------------", "os module ", "parrot str dead", "peasant unicode DEADpeasant" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": true, "input": [ "%magic" ], "language": "python", "outputs": [], "prompt_number": 16 }, { "cell_type": "code", "collapsed": true, "input": [ "%reset -f" ], "language": "python", "outputs": [], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "%whos" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Interactive namespace is empty." ] } ], "prompt_number": 18 }, { "cell_type": "markdown", "source": [ "Running shell commands" ] }, { "cell_type": "code", "collapsed": false, "input": [ "!ipython --version" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0.12.1" ] } ], "prompt_number": 19 }, { "cell_type": "markdown", "source": [ "Getting shell output back in python" ] }, { "cell_type": "code", "collapsed": false, "input": [ "v = !ipython --version", "v" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 20, "text": [ "['0.12.1']" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "!ls" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ipython-tutor.ipynb" ] } ], "prompt_number": 21 }, { "cell_type": "code", "collapsed": true, "input": [ "files = !ls" ], "language": "python", "outputs": [], "prompt_number": 22 }, { "cell_type": "code", "collapsed": true, "input": [ "files?" ], "language": "python", "outputs": [], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "files.l" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 24, "text": [ "['ipython-tutor.ipynb']" ] } ], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "files.s" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 25, "text": [ "'ipython-tutor.ipynb'" ] } ], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "files.n" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 26, "text": [ "'ipython-tutor.ipynb'" ] } ], "prompt_number": 26 }, { "cell_type": "markdown", "source": [ "Using python variables back in the shell" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for x in v[0].split('.'):", " print x", " # the touch command is unix specific, just creates empty files", " !touch $x" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0" ] }, { "output_type": "stream", "stream": "stdout", "text": [] }, { "output_type": "stream", "stream": "stdout", "text": [ "12" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "1" ] } ], "prompt_number": 27 }, { "cell_type": "code", "collapsed": false, "input": [ "ls" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\u001b[0m\u001b[00m0\u001b[0m \u001b[00m1\u001b[0m \u001b[00m12\u001b[0m \u001b[00mipython-tutor.ipynb\u001b[0m" ] } ], "prompt_number": 28 }, { "cell_type": "code", "collapsed": true, "input": [ "#cleanup the files we just made", "for x in v[0].split('.'):", " !rm $x" ], "language": "python", "outputs": [], "prompt_number": 29 }, { "cell_type": "code", "collapsed": false, "input": [ "ls" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\u001b[0m\u001b[00mipython-tutor.ipynb\u001b[0m" ] } ], "prompt_number": 30 }, { "cell_type": "markdown", "source": [ "Most of the things we've covered can be found in:" ] }, { "cell_type": "code", "collapsed": true, "input": [ "%quickref" ], "language": "python", "outputs": [], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [ "%history" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "parrot = \"dead\"" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "", "parrot", "parrot.upper?", "parrot.upper()", "import os.path", "os.path.join??", "# ends with \"py\"", "*py?", "# has \"py\" in it", "*py*?", "peasant = \"I'm not dead, yet\"", "peasant", "peasant = Out[4]", "peasant", "peasant = peasant + _i", "peasant", "%whos", "%magic", "%reset -f", "%whos", "!ipython --version", "v = !ipython --version", "v", "!ls", "files = !ls", "files?", "files.l", "files.s", "files.n", "for x in v[0].split('.'):", " print x", " # the touch command is unix specific, just creates empty files", " !touch $x", "ls", "#cleanup the files we just made", "for x in v[0].split('.'):", " !rm $x", "ls", "%quickref", "%history" ] } ], "prompt_number": 32 }, { "cell_type": "code", "collapsed": true, "input": [ "# XXX: Ipython bug?", "#%history -f posterity.py" ], "language": "python", "outputs": [], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ "!ls" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ipython-tutor.ipynb posterity.py" ] } ], "prompt_number": 37 }, { "cell_type": "markdown", "source": [ "And finally, welcome to python!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import this" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The Zen of Python, by Tim Peters", "", "Beautiful is better than ugly.", "Explicit is better than implicit.", "Simple is better than complex.", "Complex is better than complicated.", "Flat is better than nested.", "Sparse is better than dense.", "Readability counts.", "Special cases aren't special enough to break the rules.", "Although practicality beats purity.", "Errors should never pass silently.", "Unless explicitly silenced.", "In the face of ambiguity, refuse the temptation to guess.", "There should be one-- and preferably only one --obvious way to do it.", "Although that way may not be obvious at first unless you're Dutch.", "Now is better than never.", "Although never is often better than *right* now.", "If the implementation is hard to explain, it's a bad idea.", "If the implementation is easy to explain, it may be a good idea.", "Namespaces are one honking great idea -- let's do more of those!" ] } ], "prompt_number": 38 }, { "cell_type": "code", "collapsed": true, "input": [ "!rm posterity.py" ], "language": "python", "outputs": [], "prompt_number": 39 }, { "cell_type": "code", "collapsed": true, "input": [], "language": "python", "outputs": [], "prompt_number": " " } ] } ] }