{ "metadata": { "celltoolbar": "Slideshow", "name": "", "signature": "sha256:7b4c4307ccc127e749bcc6fae78706177f4e98973022d13424e5a50a8325f24e" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "<span style=\"font-size: 200%\">Introduction to Python (2)</span>\n", "===\n", "\n", "<br>\n", "\n", "[Martijn Vermaat](mailto:m.vermaat.hg@lumc.nl), [Department of Human Genetics, Leiden University Medical Center](http://humgen.nl)\n", "\n", "[Jeroen Laros](mailto:j.f.j.laros@lumc.nl), [Department of Human Genetics, Leiden University Medical Center](http://humgen.nl)\n", "\n", "Based on: [Python Scientific Lecture Notes](http://scipy-lectures.github.io/)\n", "\n", "License: [Creative Commons Attribution 3.0 License (CC-by)](http://creativecommons.org/licenses/by/3.0)" ] }, { "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", "collapsed": false, "input": [ "l = [2, 5, 2, 3, 7]\n", "type(l)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "list" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists can be heterogeneous, but we typically don't use that." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 'spezi'\n", "[3, 'abc', 1.3e20, [a, a, 2]]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "[3, 'abc', 1.3e+20, ['spezi', 'spezi', 2]]" ] } ], "prompt_number": 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", "collapsed": false, "input": [ "t = 'white', 77, 1.5\n", "type(t)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "tuple" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "color, width, scale = t\n", "width" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "77" ] } ], "prompt_number": 4 }, { "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", "collapsed": false, "input": [ "'a string can be written in single quotes'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "'a string can be written in single quotes'" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings can also be written with double quotes, or over multiple lines with triple-quotes." ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"this makes it easier to use the ' character\"" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "\"this makes it easier to use the ' character\"" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"This is a multiline string.\n", "\n", "You see? I continued after a blank line.\"\"\"" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "'This is a multiline string.\\n\\nYou see? I continued after a blank line.'" ] } ], "prompt_number": 7 }, { "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", "collapsed": false, "input": [ "'{} times {} equals {:.2f}'.format('pi', 2, 6.283185307179586)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "'pi times 2 equals 6.28'" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Accessing arguments by position or name is more readable." ] }, { "cell_type": "code", "collapsed": false, "input": [ "'{1} times {0} equals {2:.2f}'.format('pi', 2, 6.283185307179586)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "'2 times pi equals 6.28'" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "'{number} times {amount} equals {result:.2f}'.format(number='pi', amount=2,\n", " result=6.283185307179586)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "'pi times 2 equals 6.28'" ] } ], "prompt_number": 10 }, { "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", "collapsed": false, "input": [ "[1, 2, 3] + [4, 5, 6]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "[1, 2, 3, 4, 5, 6]" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "'bier' in 'we drinken bier vanaf half 5'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ "True" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "'abcdefghijkl'[5]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 13, "text": [ "'f'" ] } ], "prompt_number": 13 }, { "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", "collapsed": false, "input": [ "'abcdefghijkl'[4:8]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 14, "text": [ "'efgh'" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "'abcdefghijkl'[:3]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": [ "'abc'" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also define the step `k` with `s[i:j:k]`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "'abcdefghijkl'[7:3:-1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 16, "text": [ "'hgfe'" ] } ], "prompt_number": 16 }, { "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", "collapsed": false, "input": [ "len('attacgataggcatccgt')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 17, "text": [ "18" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "max([17, 86, 34, 51])" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 18, "text": [ "86" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "('atg', 22, True, 'atg').count('atg')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 19, "text": [ "2" ] } ], "prompt_number": 19 }, { "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", "collapsed": false, "input": [ "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()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "l" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 21, "text": [ "[7, 3, 2, 1, 1]" ] } ], "prompt_number": 21 }, { "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", "collapsed": false, "input": [ "d = {'a': 27, 'b': 18, 'c': 12}\n", "type(d)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 22, "text": [ "dict" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "d['e'] = 17\n", "'e' in d" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 23, "text": [ "True" ] } ], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "d.update({'a': 18, 'f': 2})\n", "d" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 24, "text": [ "{'a': 18, 'b': 18, 'c': 12, 'e': 17, 'f': 2}" ] } ], "prompt_number": 24 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Dictionaries\n", "===\n", "\n", "Accessing dictionary content\n", "---" ] }, { "cell_type": "code", "collapsed": false, "input": [ "d['b']" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 25, "text": [ "18" ] } ], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "d.keys()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 26, "text": [ "['a', 'c', 'b', 'e', 'f']" ] } ], "prompt_number": 26 }, { "cell_type": "code", "collapsed": false, "input": [ "d.values()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 27, "text": [ "[18, 12, 18, 17, 2]" ] } ], "prompt_number": 27 }, { "cell_type": "code", "collapsed": false, "input": [ "d.items()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 28, "text": [ "[('a', 18), ('c', 12), ('b', 18), ('e', 17), ('f', 2)]" ] } ], "prompt_number": 28 }, { "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", "collapsed": false, "input": [ "x = {12, 28, 21, 17}\n", "type(x)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 29, "text": [ "set" ] } ], "prompt_number": 29 }, { "cell_type": "code", "collapsed": false, "input": [ "x.add(12)\n", "x" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 30, "text": [ "{12, 17, 21, 28}" ] } ], "prompt_number": 30 }, { "cell_type": "code", "collapsed": false, "input": [ "x.discard(21)\n", "x" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 31, "text": [ "{12, 17, 28}" ] } ], "prompt_number": 31 }, { "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", "collapsed": false, "input": [ "17 in {12, 28, 21, 17}" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 32, "text": [ "True" ] } ], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "{12, 28, 21, 17} | {12, 18, 11}" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 33, "text": [ "{11, 12, 17, 18, 21, 28}" ] } ], "prompt_number": 33 }, { "cell_type": "code", "collapsed": false, "input": [ "{12, 28, 21, 17} & {12, 18, 11}" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 34, "text": [ "{12}" ] } ], "prompt_number": 34 }, { "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", "collapsed": false, "input": [ "True or False" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 35, "text": [ "True" ] } ], "prompt_number": 35 }, { "cell_type": "code", "collapsed": false, "input": [ "True and False" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 36, "text": [ "False" ] } ], "prompt_number": 36 }, { "cell_type": "code", "collapsed": false, "input": [ "not False" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 37, "text": [ "True" ] } ], "prompt_number": 37 }, { "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", "collapsed": false, "input": [ "22 * 3 > 66" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 38, "text": [ "False" ] } ], "prompt_number": 38 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have two equivalence relations: value equality (`==`) and object identity (`is`)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a, b = [1, 2, 3], [1, 2, 3]\n", "a == b" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 39, "text": [ "True" ] } ], "prompt_number": 39 }, { "cell_type": "code", "collapsed": false, "input": [ "a is b" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 40, "text": [ "False" ] } ], "prompt_number": 40 }, { "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", "collapsed": false, "input": [ "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'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Did we need the ` == True` part here?\n" ] } ], "prompt_number": 41 }, { "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", "collapsed": false, "input": [ "i = 0\n", "while i < 5:\n", " print i\n", " i += 1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "prompt_number": 42 }, { "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", "collapsed": false, "input": [ "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\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 43 }, { "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", "collapsed": false, "input": [ "# Add 42 to this list.\n", "l.append(42)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 44 }, { "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", "collapsed": false, "input": [ "while False:\n", " # This is never executed anyway.\n", " pass" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 45 }, { "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", "collapsed": false, "input": [ "help(range)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "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" ] } ], "prompt_number": 46 }, { "cell_type": "markdown", "metadata": {}, "source": [ "In IPython you can do it faster by typing:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "range?" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 47 }, { "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", "collapsed": false, "input": [ "range(5, 16)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 48, "text": [ "[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]" ] } ], "prompt_number": 48 }, { "cell_type": "code", "collapsed": false, "input": [ "zip(['red', 'white', 'blue'], range(3))" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 49, "text": [ "[('red', 0), ('white', 1), ('blue', 2)]" ] } ], "prompt_number": 49 }, { "cell_type": "code", "collapsed": false, "input": [ "list('abcdefghijk')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 50, "text": [ "['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']" ] } ], "prompt_number": 50 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Iteration\n", "===\n", "\n", "Iterating over a sequence\n", "---" ] }, { "cell_type": "code", "collapsed": false, "input": [ "colors = ['red', 'white', 'blue', 'orange']\n", "cities = ['leiden', 'utrecht', 'warmond', 'san francisco']" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 51 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `for` statement can iterate over sequence items." ] }, { "cell_type": "code", "collapsed": false, "input": [ "for color in colors:\n", " print color" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "red\n", "white\n", "blue\n", "orange\n" ] } ], "prompt_number": 52 }, { "cell_type": "code", "collapsed": false, "input": [ "for character in 'blue':\n", " print character" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "b\n", "l\n", "u\n", "e\n" ] } ], "prompt_number": 53 }, { "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", "collapsed": false, "input": [ "i = 0\n", "while i < len(colors):\n", " print colors[i]\n", " i += 1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "red\n", "white\n", "blue\n", "orange\n" ] } ], "prompt_number": 54 }, { "cell_type": "code", "collapsed": false, "input": [ "for i in range(len(colors)):\n", " print colors[i]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "red\n", "white\n", "blue\n", "orange\n" ] } ], "prompt_number": 55 }, { "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", "collapsed": false, "input": [ "for i, color in enumerate(colors):\n", " print i, '->', color" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 -> red\n", "1 -> white\n", "2 -> blue\n", "3 -> orange\n" ] } ], "prompt_number": 56 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Taking two sequences together\n", "---" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for city, color in zip(cities, colors):\n", " print city, '->', color" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "leiden -> red\n", "utrecht -> white\n", "warmond -> blue\n", "san francisco -> orange\n" ] } ], "prompt_number": 57 }, { "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", "collapsed": false, "input": [ "for key in {'a': 33, 'b': 17, 'c': 18}:\n", " print key" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a\n", "c\n", "b\n" ] } ], "prompt_number": 58 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Iterating over a file yields lines." ] }, { "cell_type": "code", "collapsed": false, "input": [ "for line in open('data/short_file.txt'):\n", " print line" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "this short file has two lines\n", "\n", "it is used in the example code\n", "\n" ] } ], "prompt_number": 59 }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are many more useful iterables in Python." ] }, { "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: Boolean comparison\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": [ "$\\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", "collapsed": false, "input": [ "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()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [ { "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>" ], "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "<IPython.core.display.HTML at 0x1873a50>" ] } ], "prompt_number": 1 } ], "metadata": {} } ] }