Last active
December 23, 2015 23:39
-
-
Save anandology/6711800 to your computer and use it in GitHub Desktop.
Python Training - September 26-28, 2013
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "metadata": { | |
| "name": "" | |
| }, | |
| "nbformat": 3, | |
| "nbformat_minor": 0, | |
| "worksheets": [ | |
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Python Training - Day 2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**Functions in more detail**" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Passing arguments by name\n", | |
| "def difference(x, y):\n", | |
| " return x-y\n", | |
| "\n", | |
| "print difference(5, 3)\n", | |
| "print difference(x=5, y=3)\n", | |
| "print difference(y=3, x=5)\n", | |
| "print difference(5, y=3)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "2\n", | |
| "2\n", | |
| "2\n", | |
| "2\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 5 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# default arguments\n", | |
| "def incr(x, amount=1):\n", | |
| " return x + amount\n", | |
| "\n", | |
| "print incr(5)\n", | |
| "print incr(5, 3)\n", | |
| "print incr(5, amount=3)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "6\n", | |
| "8\n", | |
| "8\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 9 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "def find_person(name=None, email=None, company=None, year=None):\n", | |
| " pass\n", | |
| "\n", | |
| "find_person(\"joe\", None, \"Foo\", None)\n", | |
| "find_person(name=\"joe\", company=\"Foo\")" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 12 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "%%file circle.py\n", | |
| "\n", | |
| "pi = 3.14\n", | |
| "\n", | |
| "def area(r):\n", | |
| " return pi * square(r)\n", | |
| "\n", | |
| "def square(x):\n", | |
| " return x * x \n", | |
| "\n", | |
| "print area(10)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "Writing circle.py\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 15 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "!python circle.py" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "314.0\r\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 16 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "%%file square2.py\n", | |
| "\n", | |
| "\n", | |
| "def square(x):\n", | |
| " y = x * x\n", | |
| " return y\n", | |
| "\n", | |
| "y = 4\n", | |
| "y2 = square(y)\n", | |
| "print y, y2\n" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "Overwriting square2.py\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 25 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "!python square2.py" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "4 16\r\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 26 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**Tuples**" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "(1, 2)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 27, | |
| "text": [ | |
| "(1, 2)" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 27 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "x = (1, 2, 3, 4, 5)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 28 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "len(x)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 29, | |
| "text": [ | |
| "5" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 29 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "x[1]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 30, | |
| "text": [ | |
| "2" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 30 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "x[1:4]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 31, | |
| "text": [ | |
| "(2, 3, 4)" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 31 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "x[0] = 3" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "ename": "TypeError", | |
| "evalue": "'tuple' object does not support item assignment", | |
| "output_type": "pyerr", | |
| "traceback": [ | |
| "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", | |
| "\u001b[0;32m<ipython-input-32-4e9f9c7cce1d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
| "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 32 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "tuple(range(10))" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 33, | |
| "text": [ | |
| "(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 33 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "a, b = 1, 2" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 34 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "1, 2" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 35, | |
| "text": [ | |
| "(1, 2)" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 35 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "x = 1, 2, 3" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 36 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "x" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 37, | |
| "text": [ | |
| "(1, 2, 3)" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 37 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "a, b, c = x" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 38 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "print a, b, c" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "1 2 3\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 39 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "a, b = b, a" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 40 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "print a, b" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "2 1\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 41 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "x[1:]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 42, | |
| "text": [ | |
| "(2, 3)" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 42 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# list of tuples\n", | |
| "\n", | |
| "x = [(1, 1), (2, 4), (3, 9)]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 43 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "for a in x:\n", | |
| " print a, a[0], a[1]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "(1, 1) 1 1\n", | |
| "(2, 4) 2 4\n", | |
| "(3, 9) 3 9\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 47 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "for a, b in x:\n", | |
| " print a, b" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "1 1\n", | |
| "2 4\n", | |
| "3 9\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 45 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "a, b = x[0]\n", | |
| "print a, b" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "1 1\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 46 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Sorting Lists" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "x = [2, 10, 8, 4, 98]\n", | |
| "x.sort() # modifies list in-place\n", | |
| "print x" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "[2, 4, 8, 10, 98]\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 51 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "y = [2, 10, 8, 4, 98]\n", | |
| "print sorted(y) # returns a new list in sorted order\n", | |
| "print y" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "[2, 4, 8, 10, 98]\n", | |
| "[2, 10, 8, 4, 98]\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 53 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Lets try to find if python sort is fast enough to sort a million values.\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "import random\n", | |
| "import string\n", | |
| "import time\n", | |
| "def random_string(n):\n", | |
| " return \"\".join([random.choice(string.letters) for i in range(n)])\n", | |
| "\n", | |
| "print \"generating strings\"\n", | |
| "names = [random_string(100) for i in range(10000)] * 100\n", | |
| "\n", | |
| "print \"sorting...\"\n", | |
| "t0 = time.time()\n", | |
| "names.sort()\n", | |
| "t1 = time.time()\n", | |
| "print \"took\", t1-t0, \"seconds\"" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "generating strings\n", | |
| "sorting..." | |
| ] | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "\n", | |
| "took" | |
| ] | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| " 0.669486045837 seconds\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 63 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "names = ['c', 'Java', 'Python', 'haskell', \n", | |
| " 'perl', 'go']\n", | |
| "print sorted(names)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "['Java', 'Python', 'c', 'go', 'haskell', 'perl']\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 70 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# How to sort these names by length?\n", | |
| "# How to sort these ignorng the case?" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 68 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "print sorted(names, key=len)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "['c', 'go', 'Java', 'perl', 'Python', 'haskell']\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 71 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "def lower(s): return s.lower()\n", | |
| "\n", | |
| "print sorted(names, key=lower)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "['c', 'go', 'haskell', 'Java', 'perl', 'Python']\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 73 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "scores = [(\"A\", 20), \n", | |
| " (\"C\", 43),\n", | |
| " (\"B\", 53), \n", | |
| " (\"D\", 12)]\n", | |
| "\n", | |
| "def f(x):\n", | |
| " # fix this\n", | |
| " return x[1]\n", | |
| "\n", | |
| "# print them in the order of scores\n", | |
| "for name, score in sorted(scores, key=f):\n", | |
| " print score, name" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "12 D\n", | |
| "20 A\n", | |
| "43 C\n", | |
| "53 B\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 79 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# lambda functions\n", | |
| "\n", | |
| "print sorted(scores, key=lambda x: x[1])" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "[('D', 12), ('A', 20), ('C', 43), ('B', 53)]\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 81 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "square = lambda x:x*x\n", | |
| "print square(4)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "16\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 82 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "g = lambda x: x[1]\n", | |
| "print g((\"a\", 3, 5))" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "3\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 84 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Working with Files" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "%%file a.txt\n", | |
| "first line \n", | |
| "second line\n", | |
| "third line\n", | |
| "fourth line\n", | |
| "fifth line" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "Writing a.txt\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 86 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "print open(\"a.txt\").read()" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "first line \n", | |
| "second line\n", | |
| "third line\n", | |
| "fourth line\n", | |
| "fifth line\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 87 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "open(\"a.txt\").read()" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 88, | |
| "text": [ | |
| "'first line \\nsecond line\\nthird line\\nfourth line\\nfifth line'" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 88 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "open(\"a.txt\").readlines()" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 89, | |
| "text": [ | |
| "['first line \\n',\n", | |
| " 'second line\\n',\n", | |
| " 'third line\\n',\n", | |
| " 'fourth line\\n',\n", | |
| " 'fifth line']" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 89 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# creating a file with 100 numbers\n", | |
| "!seq 100 > numbers.txt" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 90 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "%%file head.py\n", | |
| "# Program to print first 10 lines of a file.\n", | |
| "# The filename will be passed as command line \n", | |
| "# argument.\n", | |
| "import sys\n", | |
| "filename = sys.argv[1]\n", | |
| "for line in open(filename).readlines()[:10]:\n", | |
| " print line.strip(\"\\n\")\n" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "Overwriting head.py\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 97 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "!python head.py numbers.txt" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "1\r\n", | |
| "2\r\n", | |
| "3\r\n", | |
| "4\r\n", | |
| "5\r\n", | |
| "6\r\n", | |
| "7\r\n", | |
| "8\r\n", | |
| "9\r\n", | |
| "10\r\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 98 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**Problem** Write a program `tail.py` to print the last 10 lines of a file. The program should take the filename as command line argument.\n", | |
| "\n", | |
| " $ python tail.py numbers.txt" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**Problem** Write a program `grep.py` to print all the lines containing given pattern. The program should take the pattern and filename as arguments.\n", | |
| "\n", | |
| " $ python grep.py 0 numbers.txt\n", | |
| " 10\n", | |
| " 20\n", | |
| " ...\n", | |
| " \n", | |
| " $ python grep.py def square.py\n", | |
| " def square(x):" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**Writing files**" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# open file in write mode\n", | |
| "f = open(\"b.txt\", \"w\")\n", | |
| "f.write(\"hello world\\n\")\n", | |
| "f.close()" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 99 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# reading the file that we just wrote\n", | |
| "open(\"b.txt\").read()" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 101, | |
| "text": [ | |
| "'hello world\\n'" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 101 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**Problem** Wrire a program `copyfile.py` that takes 2 filenames as arguments and copies the first file into the second.\n", | |
| "\n", | |
| " $ python copyfile.py b.txt b2.txt" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "%%file copyfile.py\n", | |
| "\n", | |
| "import sys\n", | |
| "\n", | |
| "filename1 = sys.argv[1]\n", | |
| "filename2 = sys.argv[2]\n", | |
| "\n", | |
| "# reading the contents of the first file\n", | |
| "contents = open(filename1).read()\n", | |
| "\n", | |
| "# writing the contents to the second file\n", | |
| "f = open(filename2, \"w\")\n", | |
| "f.write(contents)\n", | |
| "f.close()\n" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "Writing copyfile.py\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 102 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## List Comprehensions" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "[x*x for x in range(10)]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 103, | |
| "text": [ | |
| "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 103 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "def squares(numbers):\n", | |
| " return [x*x for x in numbers]\n", | |
| " \n", | |
| "print squares(range(10))\n", | |
| "print sum(squares(range(100)))" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n", | |
| "328350\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 107 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "numbers = range(10)\n", | |
| "[x*x for x in numbers]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 108, | |
| "text": [ | |
| "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 108 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Compute squares of all even numbes below 10\n", | |
| "[x*x for x in numbers if x % 2 == 0]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 109, | |
| "text": [ | |
| "[0, 4, 16, 36, 64]" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 109 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# sum of squares of all even numbers below 100\n", | |
| "print sum([x*x for x in range(100) if x % 2 == 0])" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "161700\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 110 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**Problem** How do you find the length of longest string in a given list?\n", | |
| "\n", | |
| " names = ['c', 'Java', 'Python', 'haskell', \n", | |
| " 'perl', 'go']\n", | |
| " # ??" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Example: Parsing CSV file" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "%%file a.csv\n", | |
| "1,2,3\n", | |
| "1,4,9\n", | |
| "1,8,27" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "Writing a.csv\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 111 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "rows = [line.strip().split(\",\") \n", | |
| " for line in open(\"a.csv\").readlines()]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 119 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "rows" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 120, | |
| "text": [ | |
| "[['1', '2', '3'], ['1', '4', '9'], ['1', '8', '27']]" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 120 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "print rows[0]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "['1', '2', '3']\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 121 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "print rows[0][2]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "3\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 123 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "%%file b.csv\n", | |
| "# this is csv file with comments\n", | |
| "# every line starting with # is a comment\n", | |
| "1,2,3\n", | |
| "1,4,9\n", | |
| "1,8,27\n", | |
| "# end of file" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "Writing b.csv\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 124 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Can you improve the previously written CSV parser to ignore comments?" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "rows = [line.strip().split(\",\") \n", | |
| " for line in open(\"b.csv\").readlines()\n", | |
| " if not line.startswith(\"#\")]\n", | |
| "print rows" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "[['1', '2', '3'], ['1', '4', '9'], ['1', '8', '27']]\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 126 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "How do you find sum of the last column?" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# first row\n", | |
| "rows[0]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 128, | |
| "text": [ | |
| "['1', '2', '3']" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 128 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# last element of first row\n", | |
| "rows[0][-1]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 129, | |
| "text": [ | |
| "'3'" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 129 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# last element of all rows (or the last column)\n", | |
| "[row[-1] for row in rows]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 130, | |
| "text": [ | |
| "['3', '9', '27']" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 130 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# last column as ints\n", | |
| "[int(row[-1]) for row in rows]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 132, | |
| "text": [ | |
| "[3, 9, 27]" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 132 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# sum of last column\n", | |
| "sum([int(row[-1]) for row in rows])" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 133, | |
| "text": [ | |
| "39" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 133 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**Example: wordcount**" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Write a program `wc.py` to compute number of lines, number of words and number of characters in a file. " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# It should work like unix command wc\n", | |
| "!wc square.py" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| " 8 18 126 square.py\r\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 142 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "%%file wc.py\n", | |
| "import sys\n", | |
| "\n", | |
| "def linecount(filename):\n", | |
| " return len(open(filename).readlines())\n", | |
| "\n", | |
| "def wordcount(filename):\n", | |
| " return len(open(filename).read().split())\n", | |
| "\n", | |
| "def charcount(filename):\n", | |
| " return len(open(filename).read())\n", | |
| "\n", | |
| "f = sys.argv[1]\n", | |
| "print linecount(f), wordcount(f), charcount(f), f\n", | |
| " \n" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "Overwriting wc.py\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 147 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "!python wc.py square.py" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "8 18 126 square.py\r\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 148 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**The `os` module**" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "import os" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 149 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# get the current working directory\n", | |
| "os.getcwd()" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 150, | |
| "text": [ | |
| "'/Users/anand/Dropbox/Trainings/2013/python-sept2013/notebook'" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 150 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# list all files in testdir/\n", | |
| "os.listdir(\"testdir\")" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 152, | |
| "text": [ | |
| "['a.csv', 'b.csv', 'numbers.txt']" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 152 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**Problem** Write a program `listdir.py`, that takes name of a directory as command line argument and prints names of all the files in that directory.\n", | |
| "\n", | |
| "<pre>\n", | |
| " $ python listdir.py /tmp\n", | |
| " ...\n", | |
| " $ python listdir.py c:\\Python27\n", | |
| " ... \n", | |
| "</pre>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "%%file listdir.py\n", | |
| "import sys\n", | |
| "import os\n", | |
| "\n", | |
| "dirname = sys.argv[1]\n", | |
| "files = os.listdir(dirname)\n", | |
| "for f in files:\n", | |
| " print f" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "Writing listdir.py\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 153 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "!python listdir.py testdir" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "a.csv\r\n", | |
| "b.csv\r\n", | |
| "numbers.txt\r\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 154 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**Problem** Find the total number of lines in all the python files in the current directory." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "%%file countcode.py\n", | |
| "import os\n", | |
| "\n", | |
| "def linecount(filename):\n", | |
| " return len(open(filename).readlines())\n", | |
| "\n", | |
| "# find files in the current dir\n", | |
| "files = os.listdir(\".\") \n", | |
| "\n", | |
| "# find python files from files\n", | |
| "pyfiles = [f for f in files if f.endswith(\".py\")]\n", | |
| "\n", | |
| "# find number of lines in each file\n", | |
| "counts = [linecount(f) for f in pyfiles]\n", | |
| "\n", | |
| "# and fine the total\n", | |
| "print sum(counts)\n" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "Overwriting countcode.py\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 159 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "!python countcode.py" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "93\r\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 160 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "def linecount(filename):\n", | |
| " return len(open(filename).readlines())\n", | |
| "\n", | |
| "print sum([linecount(f) for f in os.listdir(\".\") \n", | |
| " if f.endswith(\".py\")])" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "93\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 162 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Dictionaries" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "d = {\"x\": 1, \"y\": 20}" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 163 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "print d['x']" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "1\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 164 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "person = {\n", | |
| " \"name\": \"Anand\", \n", | |
| " \"email\": \"[email protected]\",\n", | |
| " \"phone\": \"00000000\"\n", | |
| "}" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 172 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "person['name']" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 167, | |
| "text": [ | |
| "'Anand'" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 167 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "d" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 168, | |
| "text": [ | |
| "{'x': 1, 'y': 20}" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 168 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "d.keys()" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 169, | |
| "text": [ | |
| "['y', 'x']" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 169 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "d.values()" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 170, | |
| "text": [ | |
| "[20, 1]" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 170 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "d.items()" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 171, | |
| "text": [ | |
| "[('y', 20), ('x', 1)]" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 171 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "print person" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "{'phone': '00000000', 'name': 'Anand', 'email': '[email protected]'}\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 173 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "print 'phone' in person" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "True\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 174 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "d" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 175, | |
| "text": [ | |
| "{'x': 1, 'y': 20}" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 175 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "d['x'] = 5" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 176 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "d" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 177, | |
| "text": [ | |
| "{'x': 5, 'y': 20}" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 177 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# since 'x' is there in d, it gives d['x']\n", | |
| "print d.get('x', 0)\n", | |
| "\n", | |
| "# since z is not there in d, it gives the default\n", | |
| "print d.get('z', 0)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "5\n", | |
| "0\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 179 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Example: Word Freqency" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "%%file wf.py\n", | |
| "import sys\n", | |
| "\n", | |
| "def read_words(filename):\n", | |
| " return open(filename).read().split()\n", | |
| "\n", | |
| "def wordfreq(words):\n", | |
| " counts = {}\n", | |
| " print \"begin\", counts\n", | |
| " for w in words:\n", | |
| " counts[w] = counts.get(w, 0) + 1\n", | |
| " print w, counts\n", | |
| " return counts\n", | |
| "\n", | |
| "def print_freq(freq):\n", | |
| " print freq\n", | |
| "\n", | |
| "def main():\n", | |
| " filename = sys.argv[1]\n", | |
| " words = read_words(filename)\n", | |
| " freq = wordfreq(words)\n", | |
| " print_freq(freq)\n", | |
| "\n", | |
| "main()" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "Overwriting wf.py\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 192 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "%%file words.txt\n", | |
| "one\n", | |
| "one two\n", | |
| "one two three\n", | |
| "one two three four\n", | |
| "one two three four five" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "Overwriting words.txt\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 193 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "!python wf.py words.txt" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "begin {}\r\n", | |
| "one {'one': 1}\r\n", | |
| "one {'one': 2}\r\n", | |
| "two {'two': 1, 'one': 2}\r\n", | |
| "one {'two': 1, 'one': 3}\r\n", | |
| "two {'two': 2, 'one': 3}\r\n", | |
| "three {'three': 1, 'two': 2, 'one': 3}\r\n", | |
| "one {'three': 1, 'two': 2, 'one': 4}\r\n", | |
| "two {'three': 1, 'two': 3, 'one': 4}\r\n", | |
| "three {'three': 2, 'two': 3, 'one': 4}\r\n", | |
| "four {'four': 1, 'three': 2, 'two': 3, 'one': 4}\r\n", | |
| "one {'four': 1, 'three': 2, 'two': 3, 'one': 5}\r\n", | |
| "two {'four': 1, 'three': 2, 'two': 4, 'one': 5}\r\n", | |
| "three {'four': 1, 'three': 3, 'two': 4, 'one': 5}\r\n", | |
| "four {'four': 2, 'three': 3, 'two': 4, 'one': 5}\r\n", | |
| "five {'four': 2, 'three': 3, 'five': 1, 'two': 4, 'one': 5}\r\n", | |
| "{'four': 2, 'three': 3, 'five': 1, 'two': 4, 'one': 5}\r\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 194 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### String Formatting" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "\"Hello %s\" % \"Foo\"" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 195, | |
| "text": [ | |
| "'Hello Foo'" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 195 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "\"Chapter %d: %s\" % (1, \"Getting started\")" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 196, | |
| "text": [ | |
| "'Chapter 1: Getting started'" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 196 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "'<a href=\"%s\">%s</a>' % (\"http://google.com\", \"http://google.com/\")" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 197, | |
| "text": [ | |
| "'<a href=\"http://google.com\">http://google.com/</a>'" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 197 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "'<a href=\"%(url)s\">%(url)s</a>' % {\"url\": \"http://google.com\"}" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 198, | |
| "text": [ | |
| "'<a href=\"http://google.com\">http://google.com</a>'" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 198 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**Problem** Given a set of photographs, generate one html file for each photo to navigate through those photos. Each html file should have next and prev links.\n", | |
| "\n", | |
| "Sample photos and sample html file are available at <http://anandology.com/tmp/album.zip>." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "%%file album.py\n", | |
| "import sys\n", | |
| "import os\n", | |
| "\n", | |
| "html = \"\"\"\n", | |
| "<a href=\"#\">Prev</a> | <a href=\"#\">Next</a>\n", | |
| "<br/>\n", | |
| "<img src=\"%s\"/>\n", | |
| "\"\"\"\n", | |
| "\n", | |
| "def find_jpg_files(dirname):\n", | |
| " # fix this\n", | |
| " return os.listdir(dirname)\n", | |
| "\n", | |
| "def generate_html(dirname, filename):\n", | |
| " htmlfile = filename.replace(\".jpg\", \".html\")\n", | |
| " path = os.path.join(dirname, htmlfile)\n", | |
| " print \"generating\", htmlfile, path\n", | |
| "\n", | |
| "def main():\n", | |
| " dirname = sys.argv[1]\n", | |
| " files = find_jpg_files(dirname)\n", | |
| " for f in files:\n", | |
| " generate_html(dirname, f)\n", | |
| " \n", | |
| "main()" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "Overwriting album.py\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 211 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "!python album.py /tmp/album" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "generating photo1.html /tmp/album/photo1.html\r\n", | |
| "generating photo2.html /tmp/album/photo2.html\r\n", | |
| "generating photo3.html /tmp/album/photo3.html\r\n", | |
| "generating photo4.html /tmp/album/photo4.html\r\n", | |
| "generating sample-photo2.html /tmp/album/sample-photo2.html\r\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 212 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [] | |
| } | |
| ], | |
| "metadata": {} | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment