{ "metadata": { "name": "", "signature": "sha256:a4d2405f8d0b45b0668d82682e58621872ca83be2595e1e5c57ed2d73b5921c1" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# IPython Notebook\n", "\n", "The IPython Notebook provides an incredible way to work with Python. For scientific projects, it has become my primary tool. \n", "\n", "IPython brings together\n", "\n", "* Code development\n", "* Code execution\n", "* Documentation\n", "* Visualization\n", "* Rich Display Environment\n", "* Reporting\n", "\n", "all in one simple interface.\n", "\n", "# Key Features\n", "\n", "IPython comes with some features that make it extremely powerful, especially when compared to the standard Pyhton shell.\n", "\n", "* **Interactive code editing**\n", " * Syntax Highlighting\n", " * Tab Completion/Introspection\n", " * Easy code help\n", "* **Code Execution**\n", " * Results attached to the code that was executed\n", " * Input/Output stored and saved with notebook\n", " * \"Magic\" functions for performing common tasks (timing, profiling, etc.)\n", " * Easy command line execution with results stored in python list\n", "* **In-line Structured Documentation**\n", " * Markdown processor (that's what this is)\n", "* **Rich Displays and Media**\n", " * Built in support for multiple types (HTML, LaTeX, PNG, SVG, etc.)\n", " * Custom representations for common Python Objects (e.g. Pandas Dataframe)\n", "* **And more!**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Code Editing and Execution\n", "\n", "Let's look at some python code! (and then execute it)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(\"Hello world!\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello world!\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Functions work as well!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def hello(what):\n", " '''\n", " Prints Hello to something!\n", " '''\n", " print(\"Hello \" + what + \"!\")\n", "\n", "hello(\"hsv.py\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello hsv.py!\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "It also persists through cells." ] }, { "cell_type": "code", "collapsed": false, "input": [ "hello(\"hsv.py... again\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello hsv.py... again!\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can get help as well by using the `?` symbol." ] }, { "cell_type": "code", "collapsed": false, "input": [ "hello?" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**The output is also asyncronous.**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "fib = lambda n:reduce(lambda x,n:[x[1],x[0]+x[1]], range(n),[0,1])[0]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "import time\n", "for i in range(10):\n", " print(fib(i))\n", " time.sleep(.5)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "1" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "2" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "3" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "5" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "8" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "13" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "21" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "34" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "And pretty smart" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for i in range(200):\n", " print(fib(i))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "1\n", "2\n", "3\n", "5\n", "8\n", "13\n", "21\n", "34\n", "55\n", "89\n", "144\n", "233\n", "377\n", "610\n", "987\n", "1597\n", "2584\n", "4181\n", "6765\n", "10946\n", "17711\n", "28657\n", "46368\n", "75025\n", "121393\n", "196418\n", "317811\n", "514229\n", "832040\n", "1346269\n", "2178309\n", "3524578\n", "5702887\n", "9227465\n", "14930352\n", "24157817\n", "39088169\n", "63245986\n", "102334155\n", "165580141\n", "267914296\n", "433494437\n", "701408733\n", "1134903170\n", "1836311903\n", "2971215073\n", "4807526976\n", "7778742049\n", "12586269025\n", "20365011074\n", "32951280099\n", "53316291173\n", "86267571272\n", "139583862445\n", "225851433717\n", "365435296162\n", "591286729879\n", "956722026041\n", "1548008755920\n", "2504730781961\n", "4052739537881\n", "6557470319842\n", "10610209857723\n", "17167680177565\n", "27777890035288\n", "44945570212853\n", "72723460248141\n", "117669030460994\n", "190392490709135\n", "308061521170129\n", "498454011879264\n", "806515533049393\n", "1304969544928657\n", "2111485077978050\n", "3416454622906707\n", "5527939700884757\n", "8944394323791464\n", "14472334024676221\n", "23416728348467685\n", "37889062373143906\n", "61305790721611591\n", "99194853094755497\n", "160500643816367088\n", "259695496911122585\n", "420196140727489673\n", "679891637638612258\n", "1100087778366101931\n", "1779979416004714189\n", "2880067194370816120\n", "4660046610375530309\n", "7540113804746346429\n", "12200160415121876738\n", "19740274219868223167\n", "31940434634990099905\n", "51680708854858323072\n", "83621143489848422977\n", "135301852344706746049\n", "218922995834555169026\n", "354224848179261915075\n", "573147844013817084101\n", "927372692193078999176\n", "1500520536206896083277\n", "2427893228399975082453\n", "3928413764606871165730\n", "6356306993006846248183\n", "10284720757613717413913\n", "16641027750620563662096\n", "26925748508234281076009\n", "43566776258854844738105\n", "70492524767089125814114\n", "114059301025943970552219\n", "184551825793033096366333\n", "298611126818977066918552\n", "483162952612010163284885\n", "781774079430987230203437\n", "1264937032042997393488322\n", "2046711111473984623691759\n", "3311648143516982017180081\n", "5358359254990966640871840\n", "8670007398507948658051921\n", "14028366653498915298923761\n", "22698374052006863956975682\n", "36726740705505779255899443\n", "59425114757512643212875125\n", "96151855463018422468774568\n", "155576970220531065681649693\n", "251728825683549488150424261\n", "407305795904080553832073954\n", "659034621587630041982498215\n", "1066340417491710595814572169\n", "1725375039079340637797070384\n", "2791715456571051233611642553\n", "4517090495650391871408712937\n", "7308805952221443105020355490\n", "11825896447871834976429068427\n", "19134702400093278081449423917\n", "30960598847965113057878492344\n", "50095301248058391139327916261\n", "81055900096023504197206408605\n", "131151201344081895336534324866\n", "212207101440105399533740733471\n", "343358302784187294870275058337\n", "555565404224292694404015791808\n", "898923707008479989274290850145\n", "1454489111232772683678306641953\n", "2353412818241252672952597492098\n", "3807901929474025356630904134051\n", "6161314747715278029583501626149\n", "9969216677189303386214405760200\n", "16130531424904581415797907386349\n", "26099748102093884802012313146549\n", "42230279526998466217810220532898\n", "68330027629092351019822533679447\n", "110560307156090817237632754212345\n", "178890334785183168257455287891792\n", "289450641941273985495088042104137\n", "468340976726457153752543329995929\n", "757791618667731139247631372100066\n", "1226132595394188293000174702095995\n", "1983924214061919432247806074196061\n", "3210056809456107725247980776292056\n", "5193981023518027157495786850488117\n", "8404037832974134882743767626780173\n", "13598018856492162040239554477268290\n", "22002056689466296922983322104048463\n", "35600075545958458963222876581316753\n", "57602132235424755886206198685365216\n", "93202207781383214849429075266681969\n", "150804340016807970735635273952047185\n", "244006547798191185585064349218729154\n", "394810887814999156320699623170776339\n", "638817435613190341905763972389505493\n", "1033628323428189498226463595560281832\n", "1672445759041379840132227567949787325\n", "2706074082469569338358691163510069157\n", "4378519841510949178490918731459856482\n", "7084593923980518516849609894969925639\n", "11463113765491467695340528626429782121\n", "18547707689471986212190138521399707760\n", "30010821454963453907530667147829489881\n", "48558529144435440119720805669229197641\n", "78569350599398894027251472817058687522\n", "127127879743834334146972278486287885163\n", "205697230343233228174223751303346572685\n", "332825110087067562321196029789634457848\n", "538522340430300790495419781092981030533\n", "871347450517368352816615810882615488381\n", "1409869790947669143312035591975596518914\n", "2281217241465037496128651402858212007295\n", "3691087032412706639440686994833808526209\n", "5972304273877744135569338397692020533504\n", "9663391306290450775010025392525829059713\n", "15635695580168194910579363790217849593217\n", "25299086886458645685589389182743678652930\n", "40934782466626840596168752972961528246147\n", "66233869353085486281758142155705206899077\n", "107168651819712326877926895128666735145224\n", "173402521172797813159685037284371942044301\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Let's look at some variables" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 1\n", "y = 2\n", "\n", "x + y" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "3" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "_ * 3" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "9" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "Out" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "{8: 3, 9: 9}" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "In" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "['',\n", " u'print(\"Hello world!\")',\n", " u'def hello(what):\\n \\'\\'\\'\\n Prints Hello to something!\\n \\'\\'\\'\\n print(\"Hello \" + what + \"!\")\\n\\nhello(\"hsv.py\")',\n", " u'hello(\"hsv.py... again\")',\n", " u\"get_ipython().magic(u'pinfo hello')\",\n", " u'fib = lambda n:reduce(lambda x,n:[x[1],x[0]+x[1]], range(n),[0,1])[0]',\n", " u'import time\\nfor i in range(10):\\n print(fib(i))\\n time.sleep(.5)',\n", " u'for i in range(200):\\n print(fib(i))',\n", " u'x = 1\\ny = 2\\n\\nx + y',\n", " u'_ * 3',\n", " u'Out',\n", " u'In']" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Don't forget about magics" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%lsmagic" ], "language": "python", "metadata": {}, "outputs": [ { "json": [ "{\"cell\": {\"prun\": \"ExecutionMagics\", \"file\": \"Other\", \"!\": \"OSMagics\", \"capture\": \"ExecutionMagics\", \"timeit\": \"ExecutionMagics\", \"script\": \"ScriptMagics\", \"pypy\": \"Other\", \"system\": \"OSMagics\", \"perl\": \"Other\", \"HTML\": \"Other\", \"python3\": \"Other\", \"python\": \"Other\", \"SVG\": \"Other\", \"javascript\": \"DisplayMagics\", \"writefile\": \"OSMagics\", \"ruby\": \"Other\", \"bash\": \"Other\", \"latex\": \"DisplayMagics\", \"sx\": \"OSMagics\", \"svg\": \"DisplayMagics\", \"html\": \"DisplayMagics\", \"sh\": \"Other\", \"time\": \"ExecutionMagics\", \"debug\": \"ExecutionMagics\"}, \"line\": {\"psource\": \"NamespaceMagics\", \"logstart\": \"LoggingMagics\", \"popd\": \"OSMagics\", \"loadpy\": \"CodeMagics\", \"install_ext\": \"ExtensionMagics\", \"colors\": \"BasicMagics\", \"who_ls\": \"NamespaceMagics\", \"lf\": \"Other\", \"install_profiles\": \"DeprecatedMagics\", \"ll\": \"Other\", \"pprint\": \"BasicMagics\", \"lk\": \"Other\", \"ls\": \"Other\", \"save\": \"CodeMagics\", \"tb\": \"ExecutionMagics\", \"lx\": \"Other\", \"pylab\": \"PylabMagics\", \"killbgscripts\": \"ScriptMagics\", \"quickref\": \"BasicMagics\", \"magic\": \"BasicMagics\", \"dhist\": \"OSMagics\", \"edit\": \"KernelMagics\", \"logstop\": \"LoggingMagics\", \"gui\": \"BasicMagics\", \"alias_magic\": \"BasicMagics\", \"debug\": \"ExecutionMagics\", \"page\": \"BasicMagics\", \"logstate\": \"LoggingMagics\", \"ed\": \"Other\", \"pushd\": \"OSMagics\", \"timeit\": \"ExecutionMagics\", \"rehashx\": \"OSMagics\", \"hist\": \"Other\", \"qtconsole\": \"KernelMagics\", \"rm\": \"Other\", \"dirs\": \"OSMagics\", \"run\": \"ExecutionMagics\", \"reset_selective\": \"NamespaceMagics\", \"rep\": \"Other\", \"pinfo2\": \"NamespaceMagics\", \"matplotlib\": \"PylabMagics\", \"unload_ext\": \"ExtensionMagics\", \"doctest_mode\": \"KernelMagics\", \"logoff\": \"LoggingMagics\", \"reload_ext\": \"ExtensionMagics\", \"pdb\": \"ExecutionMagics\", \"load\": \"CodeMagics\", \"lsmagic\": \"BasicMagics\", \"autosave\": \"KernelMagics\", \"cd\": \"OSMagics\", \"pastebin\": \"CodeMagics\", \"prun\": \"ExecutionMagics\", \"cp\": \"Other\", \"autocall\": \"AutoMagics\", \"bookmark\": \"OSMagics\", \"connect_info\": \"KernelMagics\", \"mkdir\": \"Other\", \"system\": \"OSMagics\", \"whos\": \"NamespaceMagics\", \"rmdir\": \"Other\", \"automagic\": \"AutoMagics\", \"store\": \"StoreMagics\", \"more\": \"KernelMagics\", \"pdef\": \"NamespaceMagics\", \"precision\": \"BasicMagics\", \"pinfo\": \"NamespaceMagics\", \"pwd\": \"OSMagics\", \"psearch\": \"NamespaceMagics\", \"reset\": \"NamespaceMagics\", \"recall\": \"HistoryMagics\", \"xdel\": \"NamespaceMagics\", \"xmode\": \"BasicMagics\", \"cat\": \"Other\", \"mv\": \"Other\", \"rerun\": \"HistoryMagics\", \"logon\": \"LoggingMagics\", \"history\": \"HistoryMagics\", \"pycat\": \"OSMagics\", \"unalias\": \"OSMagics\", \"install_default_config\": \"DeprecatedMagics\", \"env\": \"OSMagics\", \"load_ext\": \"ExtensionMagics\", \"config\": \"ConfigMagics\", \"profile\": \"BasicMagics\", \"pfile\": \"NamespaceMagics\", \"less\": \"KernelMagics\", \"who\": \"NamespaceMagics\", \"notebook\": \"BasicMagics\", \"man\": \"KernelMagics\", \"sx\": \"OSMagics\", \"macro\": \"ExecutionMagics\", \"clear\": \"KernelMagics\", \"alias\": \"OSMagics\", \"time\": \"ExecutionMagics\", \"sc\": \"OSMagics\", \"ldir\": \"Other\", \"pdoc\": \"NamespaceMagics\"}}" ], "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ "Available line magics:\n", "%alias %alias_magic %autocall %automagic %autosave %bookmark %cat %cd %clear %colors %config %connect_info %cp %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %install_default_config %install_ext %install_profiles %killbgscripts %ldir %less %lf %lk %ll %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %lx %macro %magic %man %matplotlib %mkdir %more %mv %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %rm %rmdir %run %save %sc %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode\n", "\n", "Available cell magics:\n", "%%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%latex %%perl %%prun %%pypy %%python %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile\n", "\n", "Automagic is ON, % prefix IS NOT needed for line magics." ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "%ls" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 - Intro.ipynb ExpandInlineMagic.ipynb \u001b[34mimages\u001b[m\u001b[m/\r\n", "1 - Quick Into to IPython Notebook.ipynb README.md \u001b[31mmeasurements.csv\u001b[m\u001b[m*\r\n", "2 - GPXPy Overview.ipynb \u001b[31mcardioActivities.csv\u001b[m\u001b[m* requirements.txt\r\n", "3 - Analyzing Run Data.ipynb environment.conda \u001b[34mrundata\u001b[m\u001b[m/\r\n", "4 - Making Cool Things With Run Data.ipynb \u001b[34mhsvrundata\u001b[m\u001b[m/\r\n" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "%whos" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Variable Type Data/Info\n", "--------------------------------\n", "fib function at 0x1036661b8>\n", "hello function \n", "i int 199\n", "time module 2.7/lib-dynload/time.so'>\n", "x int 1\n", "y int 2\n" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "%timeit fib(100)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10000 loops, best of 3: 39.8 \u00b5s per loop\n" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "%run some_python_script.py" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stderr", "text": [ "ERROR: File `u'some_python_script.py'` not found.\n" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Structured Documentation\n", "Of course you have already seen some Markdown.\n", "\n", "## You can\n", "### have multiple\n", "#### header styles\n", "\n", "* And\n", "* Lists\n", "\n", "Block quotes also look nice:\n", "\n", "> Why Thank you!\n", "\n", "More IPython Markdown examples can be found [here](http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/notebooks/Part%204%20-%20Markdown%20Cells.ipynb) and Markdown syntax can be found [here](http://daringfireball.net/projects/markdown/basics)\n", "\n", "# Rich Display\n", "\n", "Instead of demonstrating the Rich Display capabilities of IPython, I'll use IPythons's Rich Display capabilities to show a [website](http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/notebooks/Part%205%20-%20Rich%20Display%20System.ipynb) highlighing IPython's Rish Display capabilities! #meta" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import IFrame\n", "IFrame('http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/notebooks/Part%205%20-%20Rich%20Display%20System.ipynb', \n", " width=800, height=350)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", " \n", " " ], "metadata": {}, "output_type": "pyout", "prompt_number": 17, "text": [ "" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 17 } ], "metadata": {} } ] }