{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 7 Reasons Why I <3 Python\n",
    "<img src=\"assets/8324418418_fd96aa2a4d_z.jpg\"/>\n",
    "<!-- https://www.flickr.com/photos/46990836@N03/8324418418/in/photolist-dFAPzu-84UDhW-5GLqRK-5GLmmH-5GQPgy-dHWLKu-nko93Q-ssYxi-iTzLxq-eFFN1v-9BiGdV-rnkoGd-nkoP4z-5MazPG-6xS62e-nvGUoC-kE4q4E-anVJs5-5GQFnq-nkowMh-qTy55d-8kFagv-nkonHf-puxpDp-5GLnzc-3LV5nd-pXGaMS-5GQDKL-Jvujy-netUoC-5GLoUZ-6TnLfc-dABayk-bW2VvT-23GScN-bGyDM-5WwnHr-5BBcoR-csNGwL-e8TK7k-5GQEJs-9pnCNb-nkohSJ-dFvpe6-9kiSMS-7jnd94-7iPM12-5pstA5-j8TwuU-5GLzbr -->\n",
    "\n",
    "## Keith Maull / Sep. 14, 2016 / Galvanize gSchool Boulder"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 1. It is beautiful, clean and precise\n",
    "\n",
    "<img src=\"assets/3717019943_7e6637b322_z.jpg\"/>\n",
    "<!-- https://www.flickr.com/photos/noodlefish/3717019943/in/photolist-fCfsFK-8skXjU-nAUYLu-7MVBXq-6poidm-8MMFdC-PMWrY-JMkZD-fCfsJP-2NKdk6-2Vft6H-af65YT-4SaCFj-5zx1T8-9W5SpL-JMm14-6h4SR1-qFsmnC-4R4PwH-5KvWdE-addunC-48RV8J-d2P53q-2gptSR-JMkZB-hDuYtP-8FsTtq-dWVKso-JMm12-f4iCPs-6EsGwK-32PBax-f6W9iS-6EsFHi-f6mxBd-fCfsFH-f67gX6-8QjEFY-6Hsb4o-6EwSXQ-JMiD6-5FtQ8z-2gucC1-2gpjV6-2gptUX-3cAS3L-f67gpg-5b4bYW-nRmMYh-nVbCtZ\n",
    "-->"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The gcd of 216 and 846 is 18.\n",
      "The gcd of 1071 and 462 is 21.\n",
      "The gcd of 1024 and 768 is 256.\n"
     ]
    }
   ],
   "source": [
    "def gcd(a,b):\n",
    "    while b != 0:\n",
    "        a, b = b, a%b\n",
    "    return a\n",
    "\n",
    "tests = [(216,846), (1071,462), (1024,768)]\n",
    "\n",
    "for a, b in tests:\n",
    "    print(\"The gcd of {} and {} is {}.\".format(a, b, gcd(a,b)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 and True is True\n",
      "0 or False is False\n"
     ]
    }
   ],
   "source": [
    "if 1 and True:\n",
    "    print(\"1 and True is True\")\n",
    "if not(0 or False):\n",
    "    print(\"0 or False is False\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Outer v1=1, Outer v2=2\n",
      "Outer iv1=3, Outer iv2=4\n"
     ]
    }
   ],
   "source": [
    "def outer(v1, v2):\n",
    "    # defining a nested function\n",
    "    def inner(iv1, iv2):\n",
    "        print(\"Outer v1={}, Outer v2={}\".format(v1, v2))\n",
    "        print(\"Outer iv1={}, Outer iv2={}\".format(iv1, iv2))\n",
    "    \n",
    "    inner(3,4)\n",
    "    \n",
    "outer(1,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 2. Text and Strings are a joy to work with!\n",
    "\n",
    "<img src=\"assets/157789902_ee3c6092b7_z.jpg\"/>\n",
    "<!-- https://www.flickr.com/photos/76283671@N00/157789902/in/photolist-eWHry-3p5xPR-9KvWoj-8EPNse-8hHfXJ-aDGD5X-74VGBk-5EA2Gw-8SUDFg-63S4Gn-8W6FV-8yXuVZ-8z1zM5-cQiWxu-8acKHx-avHUv6-FSeczR-aXRjnp-a66RN5-9wVi9p-Am2g-avLxeY-9o3Q7f-2YEhMQ-9fCbAN-4CkWu8-bXiwuY-92VoHu-bCbYXP-CaK6BB-mEpxvX-6Ctruz-axKSdr-3dBZ3f-52LLAo-52JqBR-s57ww-9WqcpG-2BM7qe-6mVdFd-bDKMrc-7eV4Lm-UMkAg-5ULJBL-ad2qsh-ayHw4M-a4weRB-aXTXwD-4Cqfzd-fBmx4 -->"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lorem\n",
      "ipsum\n",
      "dolor\n",
      "sit\n",
      "amet,\n",
      "consectetur\n",
      "adipiscing\n",
      "elit.\n",
      "In\n",
      "sed\n",
      "iaculis\n",
      "sapien,\n",
      "vitae\n",
      "vestibulum\n",
      "nunc.\n",
      "Vivamus\n",
      "pharetra\n",
      "risus\n",
      "neque,\n",
      "ut\n",
      "iaculis\n",
      "elit\n",
      "porta\n",
      "non.\n",
      "Vivamus\n",
      "ullamcorper\n",
      "dolor\n",
      "sit\n",
      "amet\n",
      "sollicitudin\n",
      "vehicula.\n",
      "Aliquam\n",
      "quis\n",
      "tristique\n",
      "nibh,\n",
      "eget\n",
      "malesuada\n",
      "purus.\n",
      "Proin\n",
      "varius\n",
      "sollicitudin\n",
      "turpis,\n",
      "fermentum\n",
      "mollis\n",
      "elit\n",
      "cursus\n",
      "vel.\n",
      "Suspendisse\n",
      "vehicula\n",
      "enim\n",
      "ac\n",
      "magna\n",
      "consequat\n",
      "ultricies.\n",
      "Pellentesque\n",
      "in\n",
      "leo\n",
      "et\n",
      "nulla\n",
      "porta\n",
      "faucibus.\n",
      "Quisque\n",
      "tincidunt\n",
      "in\n",
      "erat\n",
      "ut\n",
      "commodo.\n",
      "Quisque\n",
      "id\n",
      "augue\n",
      "a\n",
      "nisl\n",
      "mollis\n",
      "pulvinar\n",
      "id\n",
      "at\n",
      "nulla.\n",
      "Morbi\n",
      "a\n",
      "augue\n",
      "erat.\n",
      "Sed\n",
      "pharetra\n",
      "molestie\n",
      "mauris\n",
      "nec\n",
      "ultricies.\n",
      "Pellentesque\n",
      "efficitur\n",
      "blandit\n",
      "mauris,\n",
      "sed\n",
      "vulputate\n",
      "ante\n",
      "bibendum\n",
      "eu.\n",
      "Nulla\n",
      "facilisi.\n",
      "Sed\n",
      "porta\n",
      "massa\n",
      "eu\n",
      "ligula\n",
      "tristique,\n",
      "nec\n",
      "hendrerit\n",
      "elit\n",
      "euismod.\n",
      "Phasellus\n",
      "luctus\n",
      "placerat\n",
      "facilisis.\n"
     ]
    }
   ],
   "source": [
    "lorem_text = \"\"\"\n",
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sed iaculis sapien, vitae vestibulum nunc. Vivamus pharetra risus neque, ut iaculis elit porta non. Vivamus ullamcorper dolor sit amet sollicitudin vehicula. Aliquam quis tristique nibh, eget malesuada purus. Proin varius sollicitudin turpis, fermentum mollis elit cursus vel. Suspendisse vehicula enim ac magna consequat ultricies. Pellentesque in leo et nulla porta faucibus. Quisque tincidunt in erat ut commodo. Quisque id augue a nisl mollis pulvinar id at nulla. Morbi a augue erat. Sed pharetra molestie mauris nec ultricies. Pellentesque efficitur blandit mauris, sed vulputate ante bibendum eu. Nulla facilisi. Sed porta massa eu ligula tristique, nec hendrerit elit euismod. Phasellus luctus placerat facilisis.\n",
    "\"\"\"\n",
    "\n",
    "for t in lorem_text.split():\n",
    "    print t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lorem\n",
      "ipsum\n",
      "dolor\n",
      "sit\n",
      "amet\n",
      "consectetur\n",
      "adipiscing\n",
      "elit\n",
      "In\n",
      "sed\n",
      "iaculis\n",
      "sapien\n",
      "vitae\n",
      "vestibulum\n",
      "nunc\n",
      "Vivamus\n",
      "pharetra\n",
      "risus\n",
      "neque\n",
      "ut\n",
      "iaculis\n",
      "elit\n",
      "porta\n",
      "non\n",
      "Vivamus\n",
      "ullamcorper\n",
      "dolor\n",
      "sit\n",
      "amet\n",
      "sollicitudin\n",
      "vehicula\n",
      "Aliquam\n",
      "quis\n",
      "tristique\n",
      "nibh\n",
      "eget\n",
      "malesuada\n",
      "purus\n",
      "Proin\n",
      "varius\n",
      "sollicitudin\n",
      "turpis\n",
      "fermentum\n",
      "mollis\n",
      "elit\n",
      "cursus\n",
      "vel\n",
      "Suspendisse\n",
      "vehicula\n",
      "enim\n",
      "ac\n",
      "magna\n",
      "consequat\n",
      "ultricies\n",
      "Pellentesque\n",
      "in\n",
      "leo\n",
      "et\n",
      "nulla\n",
      "porta\n",
      "faucibus\n",
      "Quisque\n",
      "tincidunt\n",
      "in\n",
      "erat\n",
      "ut\n",
      "commodo\n",
      "Quisque\n",
      "id\n",
      "augue\n",
      "a\n",
      "nisl\n",
      "mollis\n",
      "pulvinar\n",
      "id\n",
      "at\n",
      "nulla\n",
      "Morbi\n",
      "a\n",
      "augue\n",
      "erat\n",
      "Sed\n",
      "pharetra\n",
      "molestie\n",
      "mauris\n",
      "nec\n",
      "ultricies\n",
      "Pellentesque\n",
      "efficitur\n",
      "blandit\n",
      "mauris\n",
      "sed\n",
      "vulputate\n",
      "ante\n",
      "bibendum\n",
      "eu\n",
      "Nulla\n",
      "facilisi\n",
      "Sed\n",
      "porta\n",
      "massa\n",
      "eu\n",
      "ligula\n",
      "tristique\n",
      "nec\n",
      "hendrerit\n",
      "elit\n",
      "euismod\n",
      "Phasellus\n",
      "luctus\n",
      "placerat\n",
      "facilisis\n"
     ]
    }
   ],
   "source": [
    "for w in lorem_text.split():\n",
    "    if w[-1] in [',', \".\"]:\n",
    "        print w[:-1]\n",
    "    else:\n",
    "        print w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lorem\n",
      "ipsum\n",
      "dolor\n",
      "sit\n",
      "amet\n"
     ]
    }
   ],
   "source": [
    "for w in lorem_text.split()[:5]:\n",
    "    if w[-1] in [',', \".\"]:\n",
    "        print w[:-1].lower()\n",
    "    else:\n",
    "        print w.lower()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "amet,\n",
      "sit\n",
      "dolor\n",
      "ipsum\n",
      "Lorem\n"
     ]
    }
   ],
   "source": [
    "for w in reversed(lorem_text.split()[:5]):\n",
    "    print w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "55\n"
     ]
    }
   ],
   "source": [
    "e_word_count = 0\n",
    "for w in lorem_text.split():\n",
    "    if 'e' in w:\n",
    "        e_word_count += 1\n",
    "\n",
    "print e_word_count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "def lorem_clean_sample(start, end):\n",
    "    sample = []\n",
    "    \n",
    "    for w in lorem_text.split()[start:end]:\n",
    "        if w[-1] in [',', \".\"]:\n",
    "            sample.append(w[:-1].lower())\n",
    "        else:\n",
    "            sample.append(w.lower())\n",
    "    \n",
    "    return sample"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 3. There are just enough data types to get started ...\n",
    "\n",
    "<img src=\"assets/5009171128_22a32d98a1_z.jpg\"/>\n",
    "<!-- https://www.flickr.com/photos/mfatic/5009171128/in/photolist-8CDj4s-sv2cN-cHqD9-H82d2J-ee998U-bEg1kD-9objQt-7VXmyG-cAJyP-7Y7fTE-jDQKGR-HD7PEu-bSZHYz-8Am1VT-oRXevU-kzA4Gf-8kQchz-3omu6m-agcavR-6sBGoq-pHA4Hi-6FxhWP-dwp8YN-aiLJvD-fB7Kkw-ncoPxB-dp25WU-6pVCtc-m7vJRb-HZRrD9-cxJDVf-bu3h8-bdMe2V-ktakv4-dipJP9-cGTzWC-naBhMm-eKtpXn-6L69ux-fkNEmY-2jiSJA-HMK37d-yZAzzm-8VUF5f-wNAJr-9o6Q1M-8wRXZ7-dipLfC-8AYxxx-aEPqLh -->"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "# dictionaries / maps\n",
    "d = {\"a\": 1}\n",
    "\n",
    "# tuples (immutable \"lists\")\n",
    "tup = (\"b\", 2) \n",
    "\n",
    "# lists [1, 2, 3]\n",
    "lis = [\"c\", 1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "# ... and they play well together"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['b', 2]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(tup)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('c', 1)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tuple(lis)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'c': 1}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict([tuple(lis)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'b': 2}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict([tup])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': None, 'b': None, 'c': None}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict.fromkeys([\"a\", \"b\", \"c\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': None, 'b': None, 'c': None}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict.fromkeys((\"a\", \"b\", \"c\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': None, 'b': None, 'c': None}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = dict.fromkeys([\"a\", \"b\", \"c\"])\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "d.update(dict([tuple(lis)]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': None, 'b': None, 'c': 1}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "d.update(dict([('a',1), tuple(lis), tup]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 1, 'b': 2, 'c': 1}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 4. And there are some nice short cuts ...\n",
    "\n",
    "<img src=\"assets/13563527335_7282ccd926_o.jpg\"/>\n",
    "<!-- https://www.flickr.com/photos/penguinandfish/13563527335/in/photolist-mEyC8z-xokZe-cvG6i-cvG6f-78pQcU-687d5B-J8JY5t-6PrzZ9-77x4pd-oYgqzQ-dNLFAw-8ZGhsA-5yAQGL-pfJj19-iqW4gY-6Dn5in-99oXCd-8WipvU-4gMDBd-9uYt1L-xokZj-ayxHqU-4S6ukZ-5B2cX4-7tUS2M-4pc9m2-sjqz2E-6MJPeL-xokfd-7tUSfT-az5QPT-7dy1no-f4wS42-xokZk-xokfa-5Re3Dz-6vGHki-9qBB8c-4gcyjN-xokeZ-f9EGpb-ah5B12-h4hW4Y-az5QUt-9mznbJ-sjqzfW-7dxZT1-s2ZpHS-FRcxry-3zGaoU -->"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# list comprehensions\n",
    "[i for i in xrange(1,16)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 5, 7, 9, 11, 13, 15]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[i for i in xrange(1,16) if i%2]  # say what???"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 4, 6, 8, 10, 12, 14]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[i for i in xrange(1,16) if not i%2]  # aha!!!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'adipiscing': 10,\n",
       " 'amet': 4,\n",
       " 'consectetur': 11,\n",
       " 'dolor': 5,\n",
       " 'elit': 4,\n",
       " 'in': 2,\n",
       " 'ipsum': 5,\n",
       " 'lorem': 5,\n",
       " 'sed': 3,\n",
       " 'sit': 3}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# dictionary comprehensions\n",
    "lorem_wlen = {key: len(key) for key in lorem_clean_sample(0, 10)}\n",
    "lorem_wlen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'adipiscing': 0,\n",
       " 'amet': 1,\n",
       " 'consectetur': 2,\n",
       " 'dolor': 0,\n",
       " 'elit': 1,\n",
       " 'in': 0,\n",
       " 'ipsum': 0,\n",
       " 'lorem': 1,\n",
       " 'sed': 1,\n",
       " 'sit': 0}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lorem_cnt_e = {key: key.count(\"e\") for key in lorem_clean_sample(0, 10)}\n",
    "lorem_cnt_e"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 5. Batteries included ....\n",
    "<img src=\"assets/13610718405_c247cc2925_z.jpg\" />\n",
    "<!-- https://www.flickr.com/photos/vintuitive/13610718405/in/photolist-mJJuoH-67y58t-pVuSW4-askYpe-nZfcyQ-2ZXjk4-9dh1gK-6QVJJT-9kixy9-91VEbQ-aBm9AH-arTTuE-r19JmA-37LN9W-cvvasy-aH15vp-5pgu3M-d77zoG-62epCR-4Js3Ki-BrNHkh-3VbqeB-7bzvtv-cKwNwE-3kGb35-9K4bGg-nMScWf-e9XLkL-nfRyBD-9DEb2v-5FkYhQ-qbYryC-5u18Ud-ovqtVt-oBnzvF-abfaWh-o54g8R-7yjvs1-5PHjgc-6J8QnF-p1bz4C-Jo61wp-9pGc6S-5FbjF8-cj1FNE-qpmBrn-pCQJAN-cRYEyC-hBpyoD-4X3LvS -->"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "r = requests.get(\"http://www2.ed.gov/data.json\")\n",
    "if r.status_code == 200:\n",
    "    payload_dict = r.json()  # loading the data into a dict via the json() just works!\n",
    "else: \n",
    "    print \"Could not load the data at this endpoint.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[u'conformsTo', u'dataset', u'describedBy', u'@context', u'@id', u'@type']"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "payload_dict.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": true,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "arra_data = {}\n",
    "for dataset in payload_dict['dataset']:\n",
    "    \n",
    "    if \"ARRA\" in dataset['description']:\n",
    "        \n",
    "        dsid = dataset['identifier']\n",
    "        desc = dataset['description']\n",
    "        title = dataset['title']\n",
    "        \n",
    "        arra_data[dsid] = {'description': desc, 'title': title}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{u'ED-2485': {'description': u'The Investing in Innovation (i3) Fund website depicts and allows downloading of general information on the i3 applicants received, grantees awarded and project locations. The Investing in Innovation Fund, established under section 14007 of the American Recovery and Reinvestment Act of 2009 (ARRA), provides funding to support (1) local educational agencies (LEAs), and (2) nonprofit organizations in partnership with (a) one or more LEAs or (b) a consortium of schools. The purpose of the i3 program is to provide competitive grants to applicants with a record of improving student achievement and attainment in order to expand the implementation of, and investment in, innovative practices that are demonstrated to have an impact on improving student achievement or student growth, closing achievement gaps, decreasing dropout rates, increasing high school graduation rates, or increasing college enrollment and completion rates. These grants will (1) allow eligible entities to expand and develop innovative practices that can serve as models of best practices, (2) allow eligible entities to work in partnership with the private sector and the philanthropic community, and (3) identify and document best practices that can be shared and taken to scale based on demonstrated success.',\n",
       "  'title': u'ED Grants: Investing in Innovation (i3) Fund 2010'},\n",
       " u'ED-5562': {'description': u'The Investing in Innovation (i3) Fund website depicts and allows downloading of general information on the i3 applicants received, grantees awarded and project locations. The Investing in Innovation Fund, established under section 14007 of the American Recovery and Reinvestment Act of 2009 (ARRA), provides funding to support (1) local educational agencies (LEAs), and (2) nonprofit organizations in partnership with (a) one or more LEAs or (b) a consortium of schools. The purpose of the i3 program is to provide competitive grants to applicants with a record of improving student achievement and attainment in order to expand the implementation of, and investment in, innovative practices that are demonstrated to have an impact on improving student achievement or student growth, closing achievement gaps, decreasing dropout rates, increasing high school graduation rates, or increasing college enrollment and completion rates. These grants will (1) allow eligible entities to expand and develop innovative practices that can serve as models of best practices, (2) allow eligible entities to work in partnership with the private sector and the philanthropic community, and (3) identify and document best practices that can be shared and taken to scale based on demonstrated success.',\n",
       "  'title': u'ED Grants: Investing in Innovation (i3) Fund 2011'}}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arra_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "arra_data_dc = { dataset['identifier'] : \n",
    "                {'description': dataset['description'], \n",
    "                 'title': dataset['title']}\n",
    "                for dataset in payload_dict['dataset'] \n",
    "                if \"ARRA\" in dataset['description'] }    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false,
    "scrolled": true,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{u'ED-2485': {'description': u'The Investing in Innovation (i3) Fund website depicts and allows downloading of general information on the i3 applicants received, grantees awarded and project locations. The Investing in Innovation Fund, established under section 14007 of the American Recovery and Reinvestment Act of 2009 (ARRA), provides funding to support (1) local educational agencies (LEAs), and (2) nonprofit organizations in partnership with (a) one or more LEAs or (b) a consortium of schools. The purpose of the i3 program is to provide competitive grants to applicants with a record of improving student achievement and attainment in order to expand the implementation of, and investment in, innovative practices that are demonstrated to have an impact on improving student achievement or student growth, closing achievement gaps, decreasing dropout rates, increasing high school graduation rates, or increasing college enrollment and completion rates. These grants will (1) allow eligible entities to expand and develop innovative practices that can serve as models of best practices, (2) allow eligible entities to work in partnership with the private sector and the philanthropic community, and (3) identify and document best practices that can be shared and taken to scale based on demonstrated success.',\n",
       "  'title': u'ED Grants: Investing in Innovation (i3) Fund 2010'},\n",
       " u'ED-5562': {'description': u'The Investing in Innovation (i3) Fund website depicts and allows downloading of general information on the i3 applicants received, grantees awarded and project locations. The Investing in Innovation Fund, established under section 14007 of the American Recovery and Reinvestment Act of 2009 (ARRA), provides funding to support (1) local educational agencies (LEAs), and (2) nonprofit organizations in partnership with (a) one or more LEAs or (b) a consortium of schools. The purpose of the i3 program is to provide competitive grants to applicants with a record of improving student achievement and attainment in order to expand the implementation of, and investment in, innovative practices that are demonstrated to have an impact on improving student achievement or student growth, closing achievement gaps, decreasing dropout rates, increasing high school graduation rates, or increasing college enrollment and completion rates. These grants will (1) allow eligible entities to expand and develop innovative practices that can serve as models of best practices, (2) allow eligible entities to work in partnership with the private sector and the philanthropic community, and (3) identify and document best practices that can be shared and taken to scale based on demonstrated success.',\n",
       "  'title': u'ED Grants: Investing in Innovation (i3) Fund 2011'}}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arra_data_dc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 6. Juptyer Notebooks\n",
    "\n",
    "<img src=\"assets/jupyter_logo.jpg\" /> \n",
    "<!-- http://jupyter.org -->"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Caledonia Farmers Market Association - Danville -72.140305 44.411013\n"
     ]
    }
   ],
   "source": [
    "import csv\n",
    "\n",
    "with open(\"Export.csv\") as fi:\n",
    "    csv_fi = csv.DictReader(fi)\n",
    "    for row in csv_fi:\n",
    "        print row['MarketName'], row['x'], row['y']\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Count not convert row for FMID: 2000001\n",
      "Count not convert row for FMID: 1011689\n",
      "Count not convert row for FMID: 2000002\n",
      "Count not convert row for FMID: 1002854\n",
      "Count not convert row for FMID: 2000004\n",
      "Count not convert row for FMID: 2000005\n",
      "Count not convert row for FMID: 2000006\n",
      "Count not convert row for FMID: 2000007\n",
      "Count not convert row for FMID: 2000008\n",
      "Count not convert row for FMID: 2000009\n",
      "Count not convert row for FMID: 2000010\n",
      "Count not convert row for FMID: 2000011\n",
      "Count not convert row for FMID: 2000012\n",
      "Count not convert row for FMID: 2000013\n",
      "Count not convert row for FMID: 2000014\n",
      "Count not convert row for FMID: 2000016\n",
      "Count not convert row for FMID: 2000017\n",
      "Count not convert row for FMID: 2000019\n",
      "Count not convert row for FMID: 2000020\n",
      "Count not convert row for FMID: 2000021\n",
      "Count not convert row for FMID: 2000022\n",
      "Count not convert row for FMID: 2000026\n",
      "Count not convert row for FMID: 2000028\n",
      "Count not convert row for FMID: 2000030\n",
      "Count not convert row for FMID: 2000032\n",
      "Count not convert row for FMID: 2000033\n",
      "Count not convert row for FMID: 2000034\n",
      "Count not convert row for FMID: 2000035\n",
      "Count not convert row for FMID: 2000036\n"
     ]
    }
   ],
   "source": [
    "map_data_objects = {}\n",
    "with open(\"Export.csv\") as fi:\n",
    "    csv_fi = csv.DictReader(fi)\n",
    "    for row in csv_fi:\n",
    "        try:\n",
    "            map_data_objects[row['FMID']] = (row['MarketName'], float(row['x'].strip()), float(row['y'].strip()))\n",
    "        except ValueError:\n",
    "            print(\"Count not convert row for FMID: {}\".format(row['FMID']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "import folium\n",
    "\n",
    "us_fm_map = folium.Map(location=[39.232253, -97.910156],\n",
    "                   zoom_start=4,\n",
    "                   tiles='Stamen Terrain')\n",
    "\n",
    "for fmid, v in map_data_objects.iteritems():\n",
    "    name, lon, lat = v\n",
    "    \n",
    "    if (lon < -104 and lon > -106) \\\n",
    "        and \\\n",
    "        (lat < 43 and lat > 30):\n",
    "            folium.Marker([lat, lon],\n",
    "                       popup=unicode(name.decode('utf-8'))\n",
    "                      ).add_to(us_fm_map) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><iframe src=\"data:text/html;base64,[encoded data, 187620 characters]\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;\"></iframe></div></div>"
      ],
      "text/plain": [
       "<folium.folium.Map at 0x4ac12e8>"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "us_fm_map"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 7. The community ...\n",
    "\n",
    "<img src=\"assets/pycommunity.png\"/>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Now go play ...\n",
    "<img src=\"assets/8569018622_93efb5cd7e_z.jpg\"/>\n",
    "<!-- https://www.flickr.com/photos/46990836@N03/8569018622/in/photolist-e4dsGo-e4drbm-ag5WKD-4caGzy-7eTkzp-e4drTN-6kZ1AA-67Fq7C-4qvUeL-dFvk86-dFvkLz-p8N5aV-dx8z1d-62VbU4-dFALqN-4u1EWE-dFvmhk-dFALQS-dFALm5-hUL8ph-7cLTsy-9dGKeF-dFvkuP-dFALJ9-avVtdr-dFvjAv-dtR8k8-5QLb27-dFvkTe-8fpxyQ-4hTwhD-pMuBbQ-9qVf2B-9hgixi-peV9SM-ikEv1x-j1bLt-7u5Hi4-6mPrUi-aq5Hkg-dFAK8y-bSyrsR-dFAKML-7cLTsC-54ruRw-o2iGon-4jQBiE-5W7z2-ooTY-b9sTqZ -->\n"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
