{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "<span style=\"font-size: 200%\">Introduction to Python (2)</span>\n",
    "==="
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Sequence types\n",
    "===\n",
    "\n",
    "Lists\n",
    "---\n",
    "\n",
    "Mutable sequences of values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = [2, 5, 2, 3, 7]\n",
    "type(l)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lists can be heterogeneous, but we typically don't use that."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 'abc', 1.3e+20, ['spezi', 'spezi', 2]]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 'spezi'\n",
    "[3, 'abc', 1.3e20, [a, a, 2]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Sequence types\n",
    "===\n",
    "\n",
    "Tuples\n",
    "---\n",
    "\n",
    "Immutable sequences of values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = 'white', 77, 1.5\n",
    "type(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "77"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "color, width, scale = t\n",
    "width"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Sequence types\n",
    "===\n",
    "\n",
    "Strings (1/2)\n",
    "---\n",
    "\n",
    "Immutable sequences of characters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a string can be written in single quotes'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'a string can be written in single quotes'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Strings can also be written with double quotes, or over multiple lines with triple-quotes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"this makes it easier to use the ' character\""
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"this makes it easier to use the ' character\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'This is a multiline string.\\n\\nYou see? I continued after a blank line.'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"This is a multiline string.\n",
    "\n",
    "You see? I continued after a blank line.\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Sequence types\n",
    "===\n",
    "\n",
    "Strings (2/2)\n",
    "---\n",
    "\n",
    "A common operation is formatting strings using argument substitutions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'pi times 2 equals 6.28'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'{} times {} equals {:.2f}'.format('pi', 2, 6.283185307179586)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Accessing arguments by position or name is more readable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2 times pi equals 6.28'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'{1} times {0} equals {2:.2f}'.format('pi', 2, 6.283185307179586)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'pi times 2 equals 6.28'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'{number} times {amount} equals {result:.2f}'.format(number='pi', amount=2,\n",
    "                                                     result=6.283185307179586)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Sequence types\n",
    "===\n",
    "\n",
    "Common operations (1/2)\n",
    "---\n",
    "\n",
    "All sequence types support concatenation, membership/substring tests, indexing, and slicing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[1, 2, 3] + [4, 5, 6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'bier' in 'we drinken bier vanaf half 5'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'f'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'abcdefghijkl'[5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Sequence types\n",
    "===\n",
    "\n",
    "Slicing\n",
    "---\n",
    "\n",
    "Slice `s` from `i` to `j` with `s[i:j]`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'efgh'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'abcdefghijkl'[4:8]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'abc'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'abcdefghijkl'[:3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also define the step `k` with `s[i:j:k]`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hgfe'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'abcdefghijkl'[7:3:-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Sequence types\n",
    "===\n",
    "\n",
    "Common operations (2/2)\n",
    "---\n",
    "\n",
    "Contrary to strings and tuples, lists are mutable. We can also get their length, smallest/largest item, and number/position of certain items."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len('attacgataggcatccgt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "86"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max([17, 86, 34, 51])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "('atg', 22, True, 'atg').count('atg')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Sequence types\n",
    "===\n",
    "\n",
    "Additional operations with lists\n",
    "---\n",
    "\n",
    "We can replace, add, remove, reverse and sort items in-place."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "l = [1, 2, 3, 4]\n",
    "l[3] = 7\n",
    "l.append(1)\n",
    "l[1:3] = [3, 2]\n",
    "l.sort()\n",
    "l.reverse()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[7, 3, 2, 1, 1]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Dictionaries\n",
    "===\n",
    "\n",
    "Dictionaries map *hashable* values to arbitrary objects\n",
    "---\n",
    "\n",
    "* All built-in immutable objects are hashable.\n",
    "* No built-in mutable objects are hashable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {'a': 27, 'b': 18, 'c': 12}\n",
    "type(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d['e'] = 17\n",
    "'e' in d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 18, 'b': 18, 'c': 12, 'e': 17, 'f': 2}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.update({'a': 18, 'f': 2})\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Dictionaries\n",
    "===\n",
    "\n",
    "Accessing dictionary content\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d['b']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'c', 'b', 'e', 'f']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 12, 18, 17, 2]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('a', 18), ('c', 12), ('b', 18), ('e', 17), ('f', 2)]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.items()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Sets\n",
    "===\n",
    "\n",
    "Mutable unordered collections of hashable values without duplication\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = {12, 28, 21, 17}\n",
    "type(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{12, 17, 21, 28}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.add(12)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{12, 17, 28}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.discard(21)\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Sets\n",
    "===\n",
    "\n",
    "Operations with sets\n",
    "---\n",
    "\n",
    "We can test for membership and apply many common set operations such as union and intersect."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "17 in {12, 28, 21, 17}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{11, 12, 17, 18, 21, 28}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{12, 28, 21, 17} | {12, 18, 11}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{12}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{12, 28, 21, 17} & {12, 18, 11}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Booleans\n",
    "===\n",
    "\n",
    "Boolean values and operations\n",
    "---\n",
    "\n",
    "The two boolean values are written `False` and `True`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True or False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True and False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "not False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Booleans\n",
    "===\n",
    "\n",
    "Comparisons\n",
    "---\n",
    "\n",
    "Comparisons can be done on all objects and return a boolean value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "22 * 3 > 66"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We have two equivalence relations: value equality (`==`) and object identity (`is`)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a, b = [1, 2, 3], [1, 2, 3]\n",
    "a == b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a is b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Booleans\n",
    "===\n",
    "\n",
    "`if` statements\n",
    "---\n",
    "\n",
    "(The `print` statement writes a string representation of the given value.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Did we need the ` == True` part here?\n"
     ]
    }
   ],
   "source": [
    "if 26 <= 17:\n",
    "    print 'Fact: 26 is less than or equal to 17'\n",
    "elif (26 + 8 > 14) == True:\n",
    "    print 'Did we need the ` == True` part here?'\n",
    "else:\n",
    "    print 'Nothing seems true'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Booleans\n",
    "===\n",
    "\n",
    "`while` statements\n",
    "---\n",
    "\n",
    "Our first looping control structure just repeats until the given expression evaluates to `False`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "i = 0\n",
    "while i < 5:\n",
    "    print i\n",
    "    i += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Hands on!\n",
    "===\n",
    "\n",
    "Try to guess the outcome of the following statements:\n",
    "\n",
    "    2 * 3 > 4\n",
    "    2 * (3 > 4)\n",
    "    2 * (4 > 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Notes about syntax\n",
    "===\n",
    "\n",
    "Indentation\n",
    "---\n",
    "\n",
    "Python uses indentation to delimit blocks\n",
    "\n",
    "* Instead of `begin ... end` or `{ ... }` in other languages.\n",
    "* Always increase indentation by *4 spaces*, never use tabs.\n",
    "* In any case, be consistent."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "if False:\n",
    "    if False:\n",
    "        print 'Why am I here?'\n",
    "    else:\n",
    "        while True:\n",
    "            print 'When will it stop?'\n",
    "    print \"And we're back to the first indentation level\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Some editors can be configured to behave just like that."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Notes about syntax\n",
    "===\n",
    "\n",
    "Comments\n",
    "---\n",
    "\n",
    "Comments are prepended by `#` and completely ignored."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Add 42 to this list.\n",
    "l.append(42)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`pass` statements\n",
    "---\n",
    "\n",
    "If you ever need a statement syntactically but don't want to do anything, use `pass`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "while False:\n",
    "    # This is never executed anyway.\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Useful built-ins\n",
    "===\n",
    "\n",
    "Getting help\n",
    "---\n",
    "\n",
    "You can get help on almost any object with `help`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function range in module __builtin__:\n",
      "\n",
      "range(...)\n",
      "    range([start,] stop[, step]) -> list of integers\n",
      "    \n",
      "    Return a list containing an arithmetic progression of integers.\n",
      "    range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n",
      "    When step is given, it specifies the increment (or decrement).\n",
      "    For example, range(4) returns [0, 1, 2, 3].  The end point is omitted!\n",
      "    These are exactly the valid indices for a list of 4 elements.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(range)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In IPython you can do it faster by typing:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "range?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Useful built-ins\n",
    "===\n",
    "\n",
    "We'll shortly use the following built-in functions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "range(5, 16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('red', 0), ('white', 1), ('blue', 2)]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zip(['red', 'white', 'blue'], range(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list('abcdefghijk')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Iteration\n",
    "===\n",
    "\n",
    "Iterating over a sequence\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "colors = ['red', 'white', 'blue', 'orange']\n",
    "cities = ['leiden', 'utrecht', 'warmond', 'san francisco']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `for` statement can iterate over sequence items."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "red\n",
      "white\n",
      "blue\n",
      "orange\n"
     ]
    }
   ],
   "source": [
    "for color in colors:\n",
    "    print color"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b\n",
      "l\n",
      "u\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "for character in 'blue':\n",
    "    print character"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Iteration\n",
    "===\n",
    "\n",
    "Python anti-patterns\n",
    "---\n",
    "\n",
    "These are common for programmers coming from other languages."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "red\n",
      "white\n",
      "blue\n",
      "orange\n"
     ]
    }
   ],
   "source": [
    "i = 0\n",
    "while i < len(colors):\n",
    "    print colors[i]\n",
    "    i += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "red\n",
      "white\n",
      "blue\n",
      "orange\n"
     ]
    }
   ],
   "source": [
    "for i in range(len(colors)):\n",
    "    print colors[i]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We call them *unpythonic*."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Iteration\n",
    "===\n",
    "\n",
    "Using values *and* indices\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 -> red\n",
      "1 -> white\n",
      "2 -> blue\n",
      "3 -> orange\n"
     ]
    }
   ],
   "source": [
    "for i, color in enumerate(colors):\n",
    "    print i, '->', color"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Taking two sequences together\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "leiden -> red\n",
      "utrecht -> white\n",
      "warmond -> blue\n",
      "san francisco -> orange\n"
     ]
    }
   ],
   "source": [
    "for city, color in zip(cities, colors):\n",
    "    print city, '->', color"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Iteration\n",
    "===\n",
    "\n",
    "Other iterables\n",
    "---\n",
    "\n",
    "Iterating over a dictionary yields keys."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "c\n",
      "b\n"
     ]
    }
   ],
   "source": [
    "for key in {'a': 33, 'b': 17, 'c': 18}:\n",
    "    print key"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Iterating over a file yields lines."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this short file has two lines\n",
      "\n",
      "it is used in the example code\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for line in open('data/short_file.txt'):\n",
    "    print line"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are many more useful iterables in Python."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "<div class=\"alert alert-success\">\n",
    "<h1>Hands on!</h1>\n",
    "\n",
    "<ol>\n",
    "  <li>Make a list with 10 integer elements. Sum all the items in the list.</li>\n",
    "  <li>Make a new list from the above one that does not include the 0th, 4th and 5th elements.\n",
    "  <li>Sum only the elements from the first list which are between the 2nd and 6th elements.\n",
    "  <li>Make a new list that includes only the elements that are greater than 10 from the first list.\n",
    "  <li>Food.\n",
    "    <ul>\n",
    "      <li>Create a dictionary for food products called \"prices\" and put some values in it, e.g., \"apples\": 2, \"oranges\": 1.5, \"pears\": 3, ...</li>\n",
    "      <li>Create a corresponding dictionary called \"stocks\" and put the stock values in it, e.g., \"apples\": 0, \"oranges\": 1, \"pears\": 10, ...</li>\n",
    "      <li>Print stock and price information for each food item.</li>\n",
    "      <li>Determine and print how much money you would make if you sold all of your food products.\n",
    "    </ul>\n",
    "  </li>\n",
    "</ol>\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Homework assignment\n",
    "===\n",
    "\n",
    "https://classroom.github.com/a/QU2iPYKn"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "$\\S$ Exercise: Iterate over a list\n",
    "===\n",
    "\n",
    "First we are going to make a list and fill it with a simple sequence. Then we are going to use this list to print something.\n",
    "\n",
    "* Make a list containing the numbers 0, 1, ... 9.\n",
    "* Print the last 10 lines of the song ''99 bottles of beer'' using this list."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "$\\S$ Exercise: Analyse a repeat structure\n",
    "===\n",
    "\n",
    "We are going to make a repeating DNA sequence and extract some subsequences from it.\n",
    "\n",
    "* Make a short tandem repeat that consists of three \"ACGT\" units and five \"TTATT\" units.\n",
    "* Print all suffixes of the repeat structure.\n",
    "\n",
    "**Note:** A suffix is an ending. For example, the word \"spam\" has five suffixes: \"spam\", \"pam\", \"am\", \"m\" and \"\".\n",
    "\n",
    "* Print all substrings of length 3.\n",
    "* Print all unique substrings of length 3.\n",
    "\n",
    "**Hint:** All elements in a set are unique."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "$\\S$ Exercise: Combining lists\n",
    "===\n",
    "\n",
    "Calculate all coordinates of the line x=y with x < 100.\n",
    "\n",
    "**Note:** This is the sequence (0, 0), (1, 1), ... (99, 99)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "$\\S$ Exercise: Dictionaries\n",
    "===\n",
    "We are going to store the output of a function ($f(x) = x^2$) together with its input in a dictionary.\n",
    "\n",
    "* Make a dictionary containing all squares smaller than 100.\n",
    "* Print the content of this dictionary in english, e.g., \"4 is the square of 2\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>/* Remove the vertical scrollbar added by nbconvert. */\n",
       ".reveal {\n",
       "  overflow: hidden;\n",
       "}\n",
       "\n",
       "/* Workaround some highlight.js bugs in language autodetection. */\n",
       "code.objectivec *,\n",
       "code.perl *,\n",
       "code.cs *,\n",
       "code.javascript *,\n",
       "code.http * {\n",
       "  color: black ! important;\n",
       "  font-weight: normal ! important;\n",
       "}\n",
       "span.title {\n",
       "  color: black ! important;\n",
       "}\n",
       "span.tag {\n",
       "  color: black ! important;\n",
       "}\n",
       "span.attribute {\n",
       "  color: black ! important;\n",
       "}\n",
       "</style>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML at 0x1873a50>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.display import HTML\n",
    "def css_styling():\n",
    "    styles = open('styles/custom.css', 'r').read()\n",
    "    return HTML('<style>' + styles + '</style>')\n",
    "css_styling()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Acknowledgements\n",
    "========\n",
    "\n",
    "Martijn Vermaat\n",
    "\n",
    "[Jeroen Laros](mailto:j.f.j.laros@lumc.nl)\n",
    "\n",
    "Based on\n",
    "---------\n",
    "[Python Scientific Lecture Notes](http://scipy-lectures.github.io/)\n",
    "\n",
    "License\n",
    "--------\n",
    "[Creative Commons Attribution 3.0 License (CC-by)](http://creativecommons.org/licenses/by/3.0)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}