{ "metadata": { "name": "", "signature": "sha256:a48cac8e0a3785e2996458336d8116d7c4073b9b6b995db28561ddc9ef21b09a" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "**Packing and Unpacking**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def func1(x, y, z):\n", " print x\n", " print y \n", " print z \n", "\n", "def func2(*args):\n", " # Convert args tuple to a list so we can modify it\n", " args = list(args)\n", " args[0] = 'Hello'\n", " args[1] = 'awesome'\n", " func1(*args)\n", "\n", "func2('Goodbye', 'cruel', 'world!')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello\n", "awesome\n", "world!\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "range(3, 6) # normal call with separate arguments" ], "language": "python", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "[3, 4, 5]" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "args = [3, 6]\n", "range(*args) # call with arguments unpacked from a list" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "[3, 4, 5]" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "def parrot(voltage, state='a stiff', action='voom'):\n", " print \"-- This parrot wouldn't\", action,\n", " print \"if you put\", voltage, \"volts through it.\",\n", " print \"E's\", state, \"!\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "d = {\"state\": \"bleedin' demised\", \"action\": \"VOOM\", \"voltage\": \"four million\"}\n", "parrot(**d)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "(2 * i + 1 for i in range(3))" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ " at 0x108e1db40>" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "a, b, c = (2 * i + 1 for i in range(3))\n", "a, b,c" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "(1, 3, 5)" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "a, (b, c), d = [1, (2, 3), 4]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "a" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "1" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "b" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "2" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "c" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "3" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "d" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ "4" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Swapping variables: an inside view**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def bar(a, b, c, d):\n", " d, c, b, a = a, b, c, d" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "import dis\n", "dis.dis(bar)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " 2 0 LOAD_FAST 0 (a)\n", " 3 LOAD_FAST 1 (b)\n", " 6 LOAD_FAST 2 (c)\n", " 9 LOAD_FAST 3 (d)\n", " 12 BUILD_TUPLE 4\n", " 15 UNPACK_SEQUENCE 4\n", " 18 STORE_FAST 3 (d)\n", " 21 STORE_FAST 2 (c)\n", " 24 STORE_FAST 1 (b)\n", " 27 STORE_FAST 0 (a)\n", " 30 LOAD_CONST 0 (None)\n", " 33 RETURN_VALUE \n" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "def foo(a, b):\n", " a, b = b, a\n", "dis.dis(foo)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " 2 0 LOAD_FAST 1 (b)\n", " 3 LOAD_FAST 0 (a)\n", " 6 ROT_TWO \n", " 7 STORE_FAST 0 (a)\n", " 10 STORE_FAST 1 (b)\n", " 13 LOAD_CONST 0 (None)\n", " 16 RETURN_VALUE \n" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "#https://hg.python.org/cpython/file/3696b9ae6b17/Python/peephole.c#l426\n", "#/* Try to fold tuples of constants (includes a case for lists\n", "#which are only used for \"in\" and \"not in\" tests).\n", "#Skip over BUILD_SEQN 1 UNPACK_SEQN 1.\n", "#Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.\n", "#Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "a, b = 1, 2 # simple sequence assignment\n", "print(a, b)\n", "a, b = ['green', 'blue'] # list asqignment\n", "print (a, b)\n", "a, b = 'XY' # string assignment\n", "print (a, b)\n", "a, b = range(1,5,2) # any iterable will do\n", "print (a, b)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(1, 2)\n", "('green', 'blue')\n", "('X', 'Y')\n", "(1, 3)\n" ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Nested sequence assignment**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "(a,b), c = \"XY\", \"Z\" # a = 'X', b = 'Y', c = 'Z'\n", "print(a, b, c)\n", "\n", "(a,b), c, = [1,2],'this' # a = '1', b = '2', c = 'this'\n", "print(a, b, c)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "('X', 'Y', 'Z')\n", "(1, 2, 'this')\n" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "(a,b), c = \"XYZ\" # ERROR -- too many values to unpack\n", "(a,b), c = \"XY\" # ERROR -- need more than 1 value to unpack\n", "(a,b), (c,) = [1,2],'this' # ERROR -- too many values to unpack" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "(a,b), c = \"XY\", \"Z\" # ERROR -- need more than 1 value to unpack\n", "print(a, b, c)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "('X', 'Y', 'Z')\n" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "a, b, c = \"XY\", \"Z\"" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "need more than 2 values to unpack", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"XY\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"Z\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: need more than 2 values to unpack" ] } ], "prompt_number": 22 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Extended sequence unpacking (Python 3)** (switch to the other notebook)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "*(a,*b), *c = 1,2,3,3,4,5,6,7" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m *(a,*b), *c = 1,2,3,3,4,5,6,7\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "*(a,*b), c, d = 1,2,3,3,4,5,6,7" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m *(a,*b), c, d = 1,2,3,3,4,5,6,7\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "*(a,*b), (c, d) = 1,2,3,3,4,5,6,7" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m *(a,*b), (c, d) = 1,2,3,3,4,5,6,7\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "prompt_number": 25 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Negative indexing**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "a[-1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 26, "text": [ "10" ] } ], "prompt_number": 26 }, { "cell_type": "code", "collapsed": false, "input": [ "a[-3]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 27, "text": [ "8" ] } ], "prompt_number": 27 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**List slices (a[start:end])**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "a[2:8]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 28, "text": [ "[2, 3, 4, 5, 6, 7]" ] } ], "prompt_number": 28 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**List slices with negative indexing**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "a[-4:-2]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 29, "text": [ "[7, 8]" ] } ], "prompt_number": 29 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**List slices with step (a[start:end:step])**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "a[::2]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 30, "text": [ "[0, 2, 4, 6, 8, 10]" ] } ], "prompt_number": 30 }, { "cell_type": "code", "collapsed": false, "input": [ "a[::3]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 31, "text": [ "[0, 3, 6, 9]" ] } ], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [ "a[2:8:2]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 32, "text": [ "[2, 4, 6]" ] } ], "prompt_number": 32 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**List slices with negative step**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "a[::-1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 33, "text": [ "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]" ] } ], "prompt_number": 33 }, { "cell_type": "code", "collapsed": false, "input": [ "a[-2:]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 34, "text": [ "[9, 10]" ] } ], "prompt_number": 34 }, { "cell_type": "code", "collapsed": false, "input": [ "a[:-2]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 35, "text": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8]" ] } ], "prompt_number": 35 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**List slice assignment**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [1, 2, 3, 4, 5]\n", "a[2:3] = [0, 0]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 36 }, { "cell_type": "code", "collapsed": false, "input": [ "a" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 37, "text": [ "[1, 2, 0, 0, 4, 5]" ] } ], "prompt_number": 37 }, { "cell_type": "code", "collapsed": false, "input": [ "a[1:1] = [8, 9]\n", "a" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 38, "text": [ "[1, 8, 9, 2, 0, 0, 4, 5]" ] } ], "prompt_number": 38 }, { "cell_type": "code", "collapsed": false, "input": [ "a[1:-1] = []\n", "a" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 39, "text": [ "[1, 5]" ] } ], "prompt_number": 39 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Naming slices (slice(start, end, step))**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [0, 1, 2, 3, 4, 5]\n", "LASTTHREE = slice(-3, None)\n", "LASTTHREE" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 40, "text": [ "slice(-3, None, None)" ] } ], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ "a[LASTTHREE]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 41, "text": [ "[3, 4, 5]" ] } ], "prompt_number": 41 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Iterating over list index and value pairs (enumerate)**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = ['Hello', 'world', '!']\n", "for i, x in enumerate(a):\n", " print '{}: {}'.format(i, x)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0: Hello\n", "1: world\n", "2: !\n" ] } ], "prompt_number": 42 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Iterating over dictionary key and value pairs (dict.iteritems) in Python 2**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "m = {'a': 1, 'b': 2, 'c': 3, 'd': 4}\n", "for k, v in m.iteritems():\n", " print '{}: {}'.format(k, v)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a: 1\n", "c: 3\n", "b: 2\n", "d: 4\n" ] } ], "prompt_number": 43 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Zipping and unzipping lists and iterables**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [1, 2, 3]\n", "b = ['a', 'b', 'c']\n", "z = zip(a, b)\n", "z" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 44, "text": [ "[(1, 'a'), (2, 'b'), (3, 'c')]" ] } ], "prompt_number": 44 }, { "cell_type": "code", "collapsed": false, "input": [ "zip(*z)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 45, "text": [ "[(1, 2, 3), ('a', 'b', 'c')]" ] } ], "prompt_number": 45 }, { "cell_type": "markdown", "metadata": {}, "source": [ "_zip([iterable, ...])\n", "This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.\n", "The returned list is truncated in length to the length of the shortest argument sequence.\n", "When there are multiple arguments which are all of the same length, zip() is similar to map() with an initial argument of None.\n", "With a single sequence argument, it returns a list of 1-tuples. With no arguments, it returns an empty list._" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [1, 2, 3, 4] #Please note the 4 at the end\n", "b = ['a', 'b', 'c']\n", "z = zip(a, b)\n", "z" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 46, "text": [ "[(1, 'a'), (2, 'b'), (3, 'c')]" ] } ], "prompt_number": 46 }, { "cell_type": "code", "collapsed": false, "input": [ "items = [1, 2, 3, 4, 5]\n", "def sqr(x): return x ** 2\n", "map(sqr, items)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 47, "text": [ "[1, 4, 9, 16, 25]" ] } ], "prompt_number": 47 }, { "cell_type": "code", "collapsed": false, "input": [ "map(None, items)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 48, "text": [ "[1, 2, 3, 4, 5]" ] } ], "prompt_number": 48 }, { "cell_type": "code", "collapsed": false, "input": [ "a = [1, 2, 3]\n", "b = ['a', 'b', 'c']\n", "map(None, a, b)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 49, "text": [ "[(1, 'a'), (2, 'b'), (3, 'c')]" ] } ], "prompt_number": 49 }, { "cell_type": "code", "collapsed": false, "input": [ "map(None, a, b) == zip(a, b)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 50, "text": [ "True" ] } ], "prompt_number": 50 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Grouping adjacent list items using zip**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [1, 2, 3, 4, 5, 6]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 51 }, { "cell_type": "code", "collapsed": false, "input": [ "# Using iterators\n", "group_adjacent = lambda a, k: zip(*([iter(a)] * k))\n", "group_adjacent(a, 3)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 52, "text": [ "[(1, 2, 3), (4, 5, 6)]" ] } ], "prompt_number": 52 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }