{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Getting Started with Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Live Demo of Jupyter Features\n", "\n", "* Administration interface\n", " * Files\n", " * Running\n", " * Uploading notebooks\n", " * New notebook\n", "* Notebook interface\n", " * Menu\n", " * Cells\n", " * Keyboard shortcuts\n", " * Getting help" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using Markdown in Jupyter for Literate Programming\n", "\n", "[Markdown Syntax](https://help.github.com/articles/basic-writing-and-formatting-syntax)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Elements of Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Code and comments" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[3, 6, 9]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# The code below makes a list of numbers\n", "list(range(3, 10, 3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### None" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Logical" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(True, False)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True, False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Numeric" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3, 3.14, 2.78)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1, 2, 3, 3.14, 2.78" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Strings" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "('a', 'hello', 'spaces are OK', 'double quotes are OK too')" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'a', 'hello', 'spaces are OK', \"double quotes are OK too\"" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'triple quoted strings\\ncan span\\nmultiple lines'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'''triple quoted strings\n", "can span\n", "multiple lines'''" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Tab and newline characters" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a b\tc\n", "d\te f\n" ] } ], "source": [ "print('a b\\tc\\nd\\te f')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### String interpolation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Old style" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'There are 8 planets in our solar system'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'There are %d planets in our %s system' % (8, 'solar')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "New style" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'There are 3.14 planets in our lunar system'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'There are {} planets in our {} system'.format(3.14, 'lunar')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Operators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Arithmetic" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(-1, 5, 1, 3.5, 3, 16)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "-1, 2+3, 7%3, 7/2, 7//2, 2**4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Logical" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(True, False, True, True, False, True, False)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and True, True & False, True | False, 3 <= 4, 3 == 4, 3 != 4, 3 > 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Variables and Assignment" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = 3\n", "b = 4\n", "c = a + b" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(3, 4, 7)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b, c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Containers (Collections)" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a_tuple = (1, 2, 3, 4)\n", "a_list = ['a', 'b', 'c', 'd']\n", "a_set = {1, 2, 2, 3, 3, 3}\n", "a_dict = {'c': 1, 'b': 2, 'a': 3}\n", "a_string = \"a string is a container of characters\"" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3, 4)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_tuple" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c', 'd']" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_list" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3}" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_set" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'a': 3, 'b': 2, 'c': 1}" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_dict" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'a string is a container of characters'" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_string" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Indexing a container" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_tuple[0]" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['b', 'c', 'd']" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_list[1:4]" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_dict['b']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Built-in functions" ] }, { "cell_type": "code", "execution_count": 82, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(\"hello world\")" ] }, { "cell_type": "code", "execution_count": 81, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[5, 6, 7, 8, 9]" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(5, 10))" ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n", "world\n" ] } ], "source": [ "print(\"hello\\nworld\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conversion between types" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(123, int)" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 123\n", "x, type(x)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "('123', str)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = str(x)\n", "x, type(x)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(123.0, float)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = float(x)\n", "x, type(x)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = {'a': 1, 'b': 2}\n", "type(d)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['a', 'b']" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(d)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[('a', 1), ('b', 2)]" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(d.items())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Controlling program flow" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C\n" ] } ], "source": [ "score = np.random.uniform(60, 100)\n", "\n", "if score > 90:\n", " print('A')\n", "elif score > 80:\n", " print('B')\n", "else:\n", " print('C')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Looping" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[10, 12, 14, 16, 18]" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(10, 20, 2))" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 100\n", "12 144\n", "14 196\n", "16 256\n", "18 324\n" ] } ], "source": [ "for i in range(10, 20, 2):\n", " print(i, i**2)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "max_count = 5\n", "count = 0\n", "while (count < max_count):\n", " print(count)\n", " count += 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating lists" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[0, 4, 16]" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[x**2 for x in range(5) if x % 2 == 0]" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[0, 1, 4, 9, 16]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xs = []\n", "for x in range(5):\n", " xs.append(x**2)\n", "xs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### User-defined functions" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def f(x):\n", " \"\"\"Say something about the function here.\"\"\"\n", " return x" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3.14" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(3.14)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def g(a, b):\n", " \"\"\"Calculate the sum of a and b.\"\"\"\n", " return a + b" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g(3, 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Default arguments" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def h(a= 0, b = 1, c = 2):\n", " \"\"\"Cacluates some complicated mathematical function.\"\"\"\n", " return a + 2*b + 3*c" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h()" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h(a = 1, b = 2)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h(c = 1, b = 2, a = 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using Libraries" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3.141592653589793" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "\n", "math.pi" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "np.linspace(0, 1, 11)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from numpy.random import rand" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0.27340751, 0.22682114, 0.51015736, 0.81841486])" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand(4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Built-in functions\n", "\n", "Many functions are automatically imported into the main namespace. That is why we can use functions such as `range` or `list` without importing them first." ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": true }, "outputs": [], "source": [ "range?" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on class zip in module builtins:\n", "\n", "class zip(object)\n", " | zip(iter1 [,iter2 [...]]) --> zip object\n", " | \n", " | Return a zip object whose .__next__() method returns a tuple where\n", " | the i-th element comes from the i-th iterable argument. The .__next__()\n", " | method continues until the shortest iterable in the argument sequence\n", " | is exhausted and then it raises StopIteration.\n", " | \n", " | Methods defined here:\n", " | \n", " | __getattribute__(self, name, /)\n", " | Return getattr(self, name).\n", " | \n", " | __iter__(self, /)\n", " | Implement iter(self).\n", " | \n", " | __new__(*args, **kwargs) from builtins.type\n", " | Create and return a new object. See help(type) for accurate signature.\n", " | \n", " | __next__(self, /)\n", " | Implement next(self).\n", " | \n", " | __reduce__(...)\n", " | Return state information for pickling.\n", "\n" ] } ], "source": [ "help(zip)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[('a', 0), ('b', 1), ('c', 2)]" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(zip(['a', 'b', 'c'], range(10)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Working with vectors and arrays" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.02088975, 0.28425656, 0.96299894, 0.08568538],\n", " [ 0.77909358, 0.08869851, 0.46405472, 0.09380882],\n", " [ 0.46485234, 0.41541956, 0.81755275, 0.87000665]])" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = np.random.random((3,4))\n", "A" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Indexing a matrix" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.020889751435612114" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[0,0]" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.87000665416964384" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[2,3]" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0.77909358, 0.08869851, 0.46405472, 0.09380882])" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[1]" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0.77909358, 0.08869851, 0.46405472, 0.09380882])" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[1, :]" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0.96299894, 0.46405472, 0.81755275])" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[:, 2]" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.28425656, 0.96299894, 0.08568538],\n", " [ 0.08869851, 0.46405472, 0.09380882]])" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[:2, 1:]" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.08869851, 0.46405472],\n", " [ 0.41541956, 0.81755275]])" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[1:3, 1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Vectorized functions" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.20889751, 2.84256561, 9.6299894 , 0.85685384],\n", " [ 7.79093577, 0.88698511, 4.64054718, 0.93808819],\n", " [ 4.64852338, 4.15419561, 8.17552747, 8.70006654]])" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A * 10" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "5.3473175620669089" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.sum()" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1.26483567, 0.78837463, 2.24460641, 1.04950086])" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.sum(axis = 0)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1.35383064, 1.42565563, 2.5678313 ])" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.sum(axis = 1)" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0.77909358, 0.41541956, 0.96299894, 0.87000665])" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.max(axis = 0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Input and output\n", "\n", "We will mostly be using the `pandas` library to read in tabular data files, so this section is just for completeness." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### This uses a Jupyter magic to create a file" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting test1.csv\n" ] } ], "source": [ "%%file test1.csv\n", "1,2,3\n", "4,5,6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Opening a file in `read` mode\n", "\n", "The `open` function returns a `generator`, allowing us to loop through each line of potentially massive files without using much memory." ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1,2,3\n", "4,5,6" ] } ], "source": [ "with open('test1.csv', 'r') as f:\n", " for line in f:\n", " print(line, end='')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Opening a file in `write` mode" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "collapsed": false }, "outputs": [], "source": [ "s = ['to be', 'or not to be']\n", "with open('test2.txt', 'w') as f:\n", " f.write('\\n'.join(s))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Opening a file in `append` mode" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": true }, "outputs": [], "source": [ "with open('test2.txt', 'a') as f:\n", " f.write('\\nthat is the question')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Check file contents " ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "to be\n", "or not to be\n", "that is the question\n" ] } ], "source": [ "with open('test2.txt', 'r') as f:\n", " s = f.read()\n", "print(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reading *entire* content of file into list of strings\n", "\n", "Warning: This may use a large amount of memory. The line by line approach shown above is recommended." ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['to be\\n', 'or not to be\\n', 'that is the question']" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open('test2.txt', 'r') as f:\n", " s = f.readlines()\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting comfortable with error messages" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "collapsed": false }, "outputs": [ { "ename": "NameError", "evalue": "name 'foo' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfoo\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'foo' is not defined" ] } ], "source": [ "foo" ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "collapsed": false }, "outputs": [ { "ename": "NameError", "evalue": "name 'Sort' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mSort\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'Sort' is not defined" ] } ], "source": [ "Sort([2,3,1])" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "collapsed": false }, "outputs": [ { "ename": "IndentationError", "evalue": "expected an indented block (, line 2)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m print(i)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n" ] } ], "source": [ "for i in range(3):\n", "print(i)" ] }, { "cell_type": "code", "execution_count": 72, "metadata": { "collapsed": false }, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for +: 'int' and 'str'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m3\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'1'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" ] } ], "source": [ "3 + '1'" ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "collapsed": false }, "outputs": [ { "ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mnumbers\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mnumbers\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } ], "source": [ "numbers = [1,2,3]\n", "numbers[3]" ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "collapsed": false }, "outputs": [ { "ename": "KeyError", "evalue": "'homer'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mcontacts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m'bart'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'ann@fox.cartoons.org'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'bob'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'bob@pinapple.under.thesea'\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mcontacts\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'homer'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mKeyError\u001b[0m: 'homer'" ] } ], "source": [ "contacts = {'bart': 'ann@fox.cartoons.org', 'bob': 'bob@pinapple.under.thesea'}\n", "contacts['homer']" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "collapsed": false }, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "integer division or modulo by zero", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m \u001b[0;34m//\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m3\u001b[0m \u001b[0;34m//\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m: integer division or modulo by zero" ] } ], "source": [ "x = 1 // 3\n", "y = 3 // x" ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "collapsed": false }, "outputs": [ { "ename": "TypeError", "evalue": "range expected at most 3 arguments, got 4", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: range expected at most 3 arguments, got 4" ] } ], "source": [ "range(1,2,3,4)" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "collapsed": false }, "outputs": [ { "ename": "FileNotFoundError", "evalue": "[Errno 2] No such file or directory: 'spongebob.txt'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'spongebob.txt'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'spongebob.txt'" ] } ], "source": [ "open('spongebob.txt')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**1**. Create a variable and save your name in it. Then print out `\"Hello \"` where `` comes from the saved variable." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello Cliburn\n" ] } ], "source": [ "name = 'Cliburn'\n", "print('Hello {}'.format(name))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**2**. Create a dictionary with day of week as key and number of letters as value. For example, one entry would have 'monday' as key and 6 as the value." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']\n", "d = dict(zip(days, [len(day) for day in days]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**3**. Loop through the dictionary above, and print the day and number of letters for each entry on a separate line." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "wednesday 9\n", "sunday 6\n", "thursday 8\n", "tuesday 7\n", "friday 6\n", "monday 6\n", "saturday 8\n" ] } ], "source": [ "for day in d:\n", " print(day, d[day])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**4**. Repeat 3, but now save th lines to a file called \"days.txt\". Then read in the contents of the file and print to screen." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "with open('days.txt', 'w') as f:\n", " for day in d:\n", " f.write('%s %s\\n' % (day, d[day]))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "wednesday 9\n", "sunday 6\n", "thursday 8\n", "tuesday 7\n", "friday 6\n", "monday 6\n", "saturday 8\n" ] } ], "source": [ "with open('days.txt') as f:\n", " for line in f:\n", " print(line.strip())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**5**. Loop through the dictionary above, and print the day and number of letters for each entry on a separate line, but only for days with more than 6 letters." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "wednesday 9\n", "thursday 8\n", "tuesday 7\n", "saturday 8\n" ] } ], "source": [ "for day in d:\n", " if d[day] > 6:\n", " print(day, d[day])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**6**. Create a list with the values 1,1,2,3,5,8, and 13. Use indexing to\n", "\n", "- print the 3rd value\n", "- print the last 2 values\n", "- print only the odd values" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "[8, 13]\n", "[1, 1, 3, 5, 13]\n" ] } ], "source": [ "xs = [1,1,2,3,5,8,13]\n", "print(xs[2])\n", "print(xs[-2:])\n", "print([x for x in xs if x%2 == 1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**7.** Write a function that accepts a string argument and returns the number of characters in the string." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def strlen(s):\n", " \"\"\"Return length of string s.\"\"\"\n", " return len(s)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "strlen(\"hello world\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More challenging exercises" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "**1a**. Create a list of the integers from 1 to 9 using `range`. The solution is \n", "\n", "```python\n", "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n", "```\n", "\n", "**1b**. Create a list of the **odd** integers from 1 to 9. The solution is \n", "\n", "```python\n", "[1, 3, 5, 7, 9]\n", "```\n", "\n", "**1c**. Create a list of the cubes of the **odd** integers from 1 to 9. The solution is \n", "\n", "```python\n", "[1, 27, 125, 343, 729]\n", "```" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(1, 10))" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 3, 5, 7, 9]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[x for x in range(1, 10, 2)]" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 27, 125, 343, 729]" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[x**3 for x in range(1, 10, 2)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**2** Strings have many useful methods. Here we will learn how to read the standard Python documentation for [strings](https://docs.python.org/3/library/stdtypes.html#string-methods) to understand what some of these string methods do. We will work with the following DNA sequence (gi|568815592:31575567-31578336 Homo sapiens chromosome 6, GRCh38.p7 Primary Assembly)\n", "\n", "```\n", "CAGACGCTCCCTCAGCAAGGACAGCAGAGGACCAGCTAAGAGGGAGAGAAGCAACTACAGACCCCCCCTG\n", "AAAACAACCCTCAGACGCCACATCCCCTGACAAGCTGCCAGGCAGGTTCTCTTCCTCTCACATACTGACC\n", "CACGGCTCCACCCTCTCTCCCCTGGAAAGGACACCATGAGCACTGAAAGCATGATCCGGGACGTGGAGCT\n", "GGCCGAGGAGGCGCTCCCCAAGAAGACAGGGGGGCCCCAGGGCTCCAGGCGGTGCTTGTTCCTCAGCCTC\n", "TTCTCCTTCCTGATCGTGGCAGGCGCCACCACGCTCTTCTGCCTGCTGCACTTTGGAGTGATCGGCCCCC\n", "AGAGGGAAGAGGTGAGTGCCTGGCCAGCCTTCATCCACTCTCCCACCCAAGGGGAAATGGAGACGCAAGA\n", "GAGGGAGAGAGATGGGATGGGTGAAAGATGTGCGCTGATAGGGAGGGATGGAGAGAAAAAAACGTGGAGA\n", "AAGACGGGGATGCAGAAAGAGATGTGGCAAGAGATGGGGAAGAGAGAGAGAGAAAGATGGAGAGACAGGA\n", "TGTCTGGCACATGGAAGGTGCTCACTAAGTGTGTATGGAGTGAATGAATGAATGAATGAATGAACAAGCA\n", "GATATATAAATAAGATATGGAGACAGATGTGGGGTGTGAGAAGAGAGATGGGGGAAGAAACAAGTGATAT\n", "GAATAAAGATGGTGAGACAGAAAGAGCGGGAAATATGACAGCTAAGGAGAGAGATGGGGGAGATAAGGAG\n", "AGAAGAAGATAGGGTGTCTGGCACACAGAAGACACTCAGGGAAAGAGCTGTTGAATGCCTGGAAGGTGAA\n", "TACACAGATGAATGGAGAGAGAAAACCAGACACCTCAGGGCTAAGAGCGCAGGCCAGACAGGCAGCCAGC\n", "TGTTCCTCCTTTAAGGGTGACTCCCTCGATGTTAACCATTCTCCTTCTCCCCAACAGTTCCCCAGGGACC\n", "TCTCTCTAATCAGCCCTCTGGCCCAGGCAGTCAGTAAGTGTCTCCAAACCTCTTTCCTAATTCTGGGTTT\n", "GGGTTTGGGGGTAGGGTTAGTACCGGTATGGAAGCAGTGGGGGAAATTTAAAGTTTTGGTCTTGGGGGAG\n", "GATGGATGGAGGTGAAAGTAGGGGGGTATTTTCTAGGAAGTTTAAGGGTCTCAGCTTTTTCTTTTCTCTC\n", "TCCTCTTCAGGATCATCTTCTCGAACCCCGAGTGACAAGCCTGTAGCCCATGTTGTAGGTAAGAGCTCTG\n", "AGGATGTGTCTTGGAACTTGGAGGGCTAGGATTTGGGGATTGAAGCCCGGCTGATGGTAGGCAGAACTTG\n", "GAGACAATGTGAGAAGGACTCGCTGAGCTCAAGGGAAGGGTGGAGGAACAGCACAGGCCTTAGTGGGATA\n", "CTCAGAACGTCATGGCCAGGTGGGATGTGGGATGACAGACAGAGAGGACAGGAACCGGATGTGGGGTGGG\n", "CAGAGCTCGAGGGCCAGGATGTGGAGAGTGAACCGACATGGCCACACTGACTCTCCTCTCCCTCTCTCCC\n", "TCCCTCCAGCAAACCCTCAAGCTGAGGGGCAGCTCCAGTGGCTGAACCGCCGGGCCAATGCCCTCCTGGC\n", "CAATGGCGTGGAGCTGAGAGATAACCAGCTGGTGGTGCCATCAGAGGGCCTGTACCTCATCTACTCCCAG\n", "GTCCTCTTCAAGGGCCAAGGCTGCCCCTCCACCCATGTGCTCCTCACCCACACCATCAGCCGCATCGCCG\n", "TCTCCTACCAGACCAAGGTCAACCTCCTCTCTGCCATCAAGAGCCCCTGCCAGAGGGAGACCCCAGAGGG\n", "GGCTGAGGCCAAGCCCTGGTATGAGCCCATCTATCTGGGAGGGGTCTTCCAGCTGGAGAAGGGTGACCGA\n", "CTCAGCGCTGAGATCAATCGGCCCGACTATCTCGACTTTGCCGAGTCTGGGCAGGTCTACTTTGGGATCA\n", "TTGCCCTGTGAGGAGGACGAACATCCAACCTTCCCAAACGCCTCCCCTGCCCCAATCCCTTTATTACCCC\n", "CTCCTTCAGACACCCTCAACCTCTTCTGGCTCAAAAAGAGAATTGGGGGCTTAGGGTCGGAACCCAAGCT\n", "TAGAACTTTAAGCAACAAGACCACCACTTCGAAACCTGGGATTCAGGAATGTGTGGCCTGCACAGTGAAG\n", "TGCTGGCAACCACTAAGAATTCAAACTGGGGCCTCCAGAACTCACTGGGGCCTACAGCTTTGATCCCTGA\n", "CATCTGGAATCTGGAGACCAGGGAGCCTTTGGTTCTGGCCAGAATGCTGCAGGACTTGAGAAGACCTCAC\n", "CTAGAAATTGACACAAGTGGACCTTAGGCCTTCCTCTCTCCAGATGTTTCCAGACTTCCTTGAGACACGG\n", "AGCCCAGCCCTCCCCATGGAGCCAGCTCCCTCTATTTATGTTTGCACTTGTGATTATTTATTATTTATTT\n", "ATTATTTATTTATTTACAGATGAATGTATTTATTTGGGAGACCGGGGTATCCTGGGGGACCCAATGTAGG\n", "AGCTGCCTTGGCTCAGACATGTTTTCCGTGAAAACGGAGCTGAACAATAGGCTGTTCCCATGTAGCCCCC\n", "TGGCCTCTGTGCCTTCTTTTGATTATGTTTTTTAAAATATTTATCTGATTAAGTTGTCTAAACAATGCTG\n", "ATTTGGTGACCAACTGTCACTCATTGCTGAGCCTCTGCTCCCCAGGGGAGTTGTGTCTGTAATCGCCCTA\n", "CTATTCAGTGGCGAGAAATAAAGTTTGCTTAGAAAAGAAA\n", "```\n", "\n", "**2a**. Assign the sequence to a string variable called `tnf`. (Hint: This string spans multiple lines)\n", "\n", "**2b**. Calculate the GC content \n", "\n", "![GC content](https://wikimedia.org/api/rest_v1/media/math/render/svg/e410e5ddc49b232b2c5f3b43e0cad51edb2e621b)\n", "\n", "**2c**. Find the RNA transcript using the mapping A->A, T->U, C->C, G->G." ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": true }, "outputs": [], "source": [ "tnf = '''CAGACGCTCCCTCAGCAAGGACAGCAGAGGACCAGCTAAGAGGGAGAGAAGCAACTACAGACCCCCCCTG\n", "AAAACAACCCTCAGACGCCACATCCCCTGACAAGCTGCCAGGCAGGTTCTCTTCCTCTCACATACTGACC\n", "CACGGCTCCACCCTCTCTCCCCTGGAAAGGACACCATGAGCACTGAAAGCATGATCCGGGACGTGGAGCT\n", "GGCCGAGGAGGCGCTCCCCAAGAAGACAGGGGGGCCCCAGGGCTCCAGGCGGTGCTTGTTCCTCAGCCTC\n", "TTCTCCTTCCTGATCGTGGCAGGCGCCACCACGCTCTTCTGCCTGCTGCACTTTGGAGTGATCGGCCCCC\n", "AGAGGGAAGAGGTGAGTGCCTGGCCAGCCTTCATCCACTCTCCCACCCAAGGGGAAATGGAGACGCAAGA\n", "GAGGGAGAGAGATGGGATGGGTGAAAGATGTGCGCTGATAGGGAGGGATGGAGAGAAAAAAACGTGGAGA\n", "AAGACGGGGATGCAGAAAGAGATGTGGCAAGAGATGGGGAAGAGAGAGAGAGAAAGATGGAGAGACAGGA\n", "TGTCTGGCACATGGAAGGTGCTCACTAAGTGTGTATGGAGTGAATGAATGAATGAATGAATGAACAAGCA\n", "GATATATAAATAAGATATGGAGACAGATGTGGGGTGTGAGAAGAGAGATGGGGGAAGAAACAAGTGATAT\n", "GAATAAAGATGGTGAGACAGAAAGAGCGGGAAATATGACAGCTAAGGAGAGAGATGGGGGAGATAAGGAG\n", "AGAAGAAGATAGGGTGTCTGGCACACAGAAGACACTCAGGGAAAGAGCTGTTGAATGCCTGGAAGGTGAA\n", "TACACAGATGAATGGAGAGAGAAAACCAGACACCTCAGGGCTAAGAGCGCAGGCCAGACAGGCAGCCAGC\n", "TGTTCCTCCTTTAAGGGTGACTCCCTCGATGTTAACCATTCTCCTTCTCCCCAACAGTTCCCCAGGGACC\n", "TCTCTCTAATCAGCCCTCTGGCCCAGGCAGTCAGTAAGTGTCTCCAAACCTCTTTCCTAATTCTGGGTTT\n", "GGGTTTGGGGGTAGGGTTAGTACCGGTATGGAAGCAGTGGGGGAAATTTAAAGTTTTGGTCTTGGGGGAG\n", "GATGGATGGAGGTGAAAGTAGGGGGGTATTTTCTAGGAAGTTTAAGGGTCTCAGCTTTTTCTTTTCTCTC\n", "TCCTCTTCAGGATCATCTTCTCGAACCCCGAGTGACAAGCCTGTAGCCCATGTTGTAGGTAAGAGCTCTG\n", "AGGATGTGTCTTGGAACTTGGAGGGCTAGGATTTGGGGATTGAAGCCCGGCTGATGGTAGGCAGAACTTG\n", "GAGACAATGTGAGAAGGACTCGCTGAGCTCAAGGGAAGGGTGGAGGAACAGCACAGGCCTTAGTGGGATA\n", "CTCAGAACGTCATGGCCAGGTGGGATGTGGGATGACAGACAGAGAGGACAGGAACCGGATGTGGGGTGGG\n", "CAGAGCTCGAGGGCCAGGATGTGGAGAGTGAACCGACATGGCCACACTGACTCTCCTCTCCCTCTCTCCC\n", "TCCCTCCAGCAAACCCTCAAGCTGAGGGGCAGCTCCAGTGGCTGAACCGCCGGGCCAATGCCCTCCTGGC\n", "CAATGGCGTGGAGCTGAGAGATAACCAGCTGGTGGTGCCATCAGAGGGCCTGTACCTCATCTACTCCCAG\n", "GTCCTCTTCAAGGGCCAAGGCTGCCCCTCCACCCATGTGCTCCTCACCCACACCATCAGCCGCATCGCCG\n", "TCTCCTACCAGACCAAGGTCAACCTCCTCTCTGCCATCAAGAGCCCCTGCCAGAGGGAGACCCCAGAGGG\n", "GGCTGAGGCCAAGCCCTGGTATGAGCCCATCTATCTGGGAGGGGTCTTCCAGCTGGAGAAGGGTGACCGA\n", "CTCAGCGCTGAGATCAATCGGCCCGACTATCTCGACTTTGCCGAGTCTGGGCAGGTCTACTTTGGGATCA\n", "TTGCCCTGTGAGGAGGACGAACATCCAACCTTCCCAAACGCCTCCCCTGCCCCAATCCCTTTATTACCCC\n", "CTCCTTCAGACACCCTCAACCTCTTCTGGCTCAAAAAGAGAATTGGGGGCTTAGGGTCGGAACCCAAGCT\n", "TAGAACTTTAAGCAACAAGACCACCACTTCGAAACCTGGGATTCAGGAATGTGTGGCCTGCACAGTGAAG\n", "TGCTGGCAACCACTAAGAATTCAAACTGGGGCCTCCAGAACTCACTGGGGCCTACAGCTTTGATCCCTGA\n", "CATCTGGAATCTGGAGACCAGGGAGCCTTTGGTTCTGGCCAGAATGCTGCAGGACTTGAGAAGACCTCAC\n", "CTAGAAATTGACACAAGTGGACCTTAGGCCTTCCTCTCTCCAGATGTTTCCAGACTTCCTTGAGACACGG\n", "AGCCCAGCCCTCCCCATGGAGCCAGCTCCCTCTATTTATGTTTGCACTTGTGATTATTTATTATTTATTT\n", "ATTATTTATTTATTTACAGATGAATGTATTTATTTGGGAGACCGGGGTATCCTGGGGGACCCAATGTAGG\n", "AGCTGCCTTGGCTCAGACATGTTTTCCGTGAAAACGGAGCTGAACAATAGGCTGTTCCCATGTAGCCCCC\n", "TGGCCTCTGTGCCTTCTTTTGATTATGTTTTTTAAAATATTTATCTGATTAAGTTGTCTAAACAATGCTG\n", "ATTTGGTGACCAACTGTCACTCATTGCTGAGCCTCTGCTCCCCAGGGGAGTTGTGTCTGTAATCGCCCTA\n", "CTATTCAGTGGCGAGAAATAAAGTTTGCTTAGAAAAGAAA'''" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.5281588447653429" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ng = tnf.count('G')\n", "nc = tnf.count('C')\n", "na = tnf.count('A')\n", "nt = tnf.count('T')\n", "(nc + ng)/(na + nt + nc + ng)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CAGACGCUCCCUCAGCAAGGACAGCAGAGGACCAGCUAAGAGGGAGAGAAGCAACUACAGACCCCCCCUG\n", "AAAACAACCCUCAGACGCCACAUCCCCUGACAAGCUGCCAGGCAGGUUCUCUUCCUCUCACAUACUGACC\n", "CACGGCUCCACCCUCUCUCCCCUGGAAAGGACACCAUGAGCACUGAAAGCAUGAUCCGGGACGUGGAGCU\n", "GGCCGAGGAGGCGCUCCCCAAGAAGACAGGGGGGCCCCAGGGCUCCAGGCGGUGCUUGUUCCUCAGCCUC\n", "UUCUCCUUCCUGAUCGUGGCAGGCGCCACCACGCUCUUCUGCCUGCUGCACUUUGGAGUGAUCGGCCCCC\n", "AGAGGGAAGAGGUGAGUGCCUGGCCAGCCUUCAUCCACUCUCCCACCCAAGGGGAAAUGGAGACGCAAGA\n", "GAGGGAGAGAGAUGGGAUGGGUGAAAGAUGUGCGCUGAUAGGGAGGGAUGGAGAGAAAAAAACGUGGAGA\n", "AAGACGGGGAUGCAGAAAGAGAUGUGGCAAGAGAUGGGGAAGAGAGAGAGAGAAAGAUGGAGAGACAGGA\n", "UGUCUGGCACAUGGAAGGUGCUCACUAAGUGUGUAUGGAGUGAAUGAAUGAAUGAAUGAAUGAACAAGCA\n", "GAUAUAUAAAUAAGAUAUGGAGACAGAUGUGGGGUGUGAGAAGAGAGAUGGGGGAAGAAACAAGUGAUAU\n", "GAAUAAAGAUGGUGAGACAGAAAGAGCGGGAAAUAUGACAGCUAAGGAGAGAGAUGGGGGAGAUAAGGAG\n", "AGAAGAAGAUAGGGUGUCUGGCACACAGAAGACACUCAGGGAAAGAGCUGUUGAAUGCCUGGAAGGUGAA\n", "UACACAGAUGAAUGGAGAGAGAAAACCAGACACCUCAGGGCUAAGAGCGCAGGCCAGACAGGCAGCCAGC\n", "UGUUCCUCCUUUAAGGGUGACUCCCUCGAUGUUAACCAUUCUCCUUCUCCCCAACAGUUCCCCAGGGACC\n", "UCUCUCUAAUCAGCCCUCUGGCCCAGGCAGUCAGUAAGUGUCUCCAAACCUCUUUCCUAAUUCUGGGUUU\n", "GGGUUUGGGGGUAGGGUUAGUACCGGUAUGGAAGCAGUGGGGGAAAUUUAAAGUUUUGGUCUUGGGGGAG\n", "GAUGGAUGGAGGUGAAAGUAGGGGGGUAUUUUCUAGGAAGUUUAAGGGUCUCAGCUUUUUCUUUUCUCUC\n", "UCCUCUUCAGGAUCAUCUUCUCGAACCCCGAGUGACAAGCCUGUAGCCCAUGUUGUAGGUAAGAGCUCUG\n", "AGGAUGUGUCUUGGAACUUGGAGGGCUAGGAUUUGGGGAUUGAAGCCCGGCUGAUGGUAGGCAGAACUUG\n", "GAGACAAUGUGAGAAGGACUCGCUGAGCUCAAGGGAAGGGUGGAGGAACAGCACAGGCCUUAGUGGGAUA\n", "CUCAGAACGUCAUGGCCAGGUGGGAUGUGGGAUGACAGACAGAGAGGACAGGAACCGGAUGUGGGGUGGG\n", "CAGAGCUCGAGGGCCAGGAUGUGGAGAGUGAACCGACAUGGCCACACUGACUCUCCUCUCCCUCUCUCCC\n", "UCCCUCCAGCAAACCCUCAAGCUGAGGGGCAGCUCCAGUGGCUGAACCGCCGGGCCAAUGCCCUCCUGGC\n", "CAAUGGCGUGGAGCUGAGAGAUAACCAGCUGGUGGUGCCAUCAGAGGGCCUGUACCUCAUCUACUCCCAG\n", "GUCCUCUUCAAGGGCCAAGGCUGCCCCUCCACCCAUGUGCUCCUCACCCACACCAUCAGCCGCAUCGCCG\n", "UCUCCUACCAGACCAAGGUCAACCUCCUCUCUGCCAUCAAGAGCCCCUGCCAGAGGGAGACCCCAGAGGG\n", "GGCUGAGGCCAAGCCCUGGUAUGAGCCCAUCUAUCUGGGAGGGGUCUUCCAGCUGGAGAAGGGUGACCGA\n", "CUCAGCGCUGAGAUCAAUCGGCCCGACUAUCUCGACUUUGCCGAGUCUGGGCAGGUCUACUUUGGGAUCA\n", "UUGCCCUGUGAGGAGGACGAACAUCCAACCUUCCCAAACGCCUCCCCUGCCCCAAUCCCUUUAUUACCCC\n", "CUCCUUCAGACACCCUCAACCUCUUCUGGCUCAAAAAGAGAAUUGGGGGCUUAGGGUCGGAACCCAAGCU\n", "UAGAACUUUAAGCAACAAGACCACCACUUCGAAACCUGGGAUUCAGGAAUGUGUGGCCUGCACAGUGAAG\n", "UGCUGGCAACCACUAAGAAUUCAAACUGGGGCCUCCAGAACUCACUGGGGCCUACAGCUUUGAUCCCUGA\n", "CAUCUGGAAUCUGGAGACCAGGGAGCCUUUGGUUCUGGCCAGAAUGCUGCAGGACUUGAGAAGACCUCAC\n", "CUAGAAAUUGACACAAGUGGACCUUAGGCCUUCCUCUCUCCAGAUGUUUCCAGACUUCCUUGAGACACGG\n", "AGCCCAGCCCUCCCCAUGGAGCCAGCUCCCUCUAUUUAUGUUUGCACUUGUGAUUAUUUAUUAUUUAUUU\n", "AUUAUUUAUUUAUUUACAGAUGAAUGUAUUUAUUUGGGAGACCGGGGUAUCCUGGGGGACCCAAUGUAGG\n", "AGCUGCCUUGGCUCAGACAUGUUUUCCGUGAAAACGGAGCUGAACAAUAGGCUGUUCCCAUGUAGCCCCC\n", "UGGCCUCUGUGCCUUCUUUUGAUUAUGUUUUUUAAAAUAUUUAUCUGAUUAAGUUGUCUAAACAAUGCUG\n", "AUUUGGUGACCAACUGUCACUCAUUGCUGAGCCUCUGCUCCCCAGGGGAGUUGUGUCUGUAAUCGCCCUA\n", "CUAUUCAGUGGCGAGAAAUAAAGUUUGCUUAGAAAAGAAA\n" ] } ], "source": [ "print(tnf.replace('T', 'U'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Alternative version**: The `translate` method can make multiple substitutions at the same time." ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cagacgcucccucagcaaggacagcagaggaccagcuaagagggagagaagcaacuacagacccccccug\n", "aaaacaacccucagacgccacauccccugacaagcugccaggcagguucucuuccucucacauacugacc\n", "cacggcuccacccucucuccccuggaaaggacaccaugagcacugaaagcaugauccgggacguggagcu\n", "ggccgaggaggcgcuccccaagaagacaggggggccccagggcuccaggcggugcuuguuccucagccuc\n", "uucuccuuccugaucguggcaggcgccaccacgcucuucugccugcugcacuuuggagugaucggccccc\n", "agagggaagaggugagugccuggccagccuucauccacucucccacccaaggggaaauggagacgcaaga\n", "gagggagagagaugggaugggugaaagaugugcgcugauagggagggauggagagaaaaaaacguggaga\n", "aagacggggaugcagaaagagauguggcaagagauggggaagagagagagagaaagauggagagacagga\n", "ugucuggcacauggaaggugcucacuaaguguguauggagugaaugaaugaaugaaugaaugaacaagca\n", "gauauauaaauaagauauggagacagauguggggugugagaagagagaugggggaagaaacaagugauau\n", "gaauaaagauggugagacagaaagagcgggaaauaugacagcuaaggagagagaugggggagauaaggag\n", "agaagaagauagggugucuggcacacagaagacacucagggaaagagcuguugaaugccuggaaggugaa\n", "uacacagaugaauggagagagaaaaccagacaccucagggcuaagagcgcaggccagacaggcagccagc\n", "uguuccuccuuuaagggugacucccucgauguuaaccauucuccuucuccccaacaguuccccagggacc\n", "ucucucuaaucagcccucuggcccaggcagucaguaagugucuccaaaccucuuuccuaauucuggguuu\n", "ggguuuggggguaggguuaguaccgguauggaagcagugggggaaauuuaaaguuuuggucuugggggag\n", "gauggauggaggugaaaguagggggguauuuucuaggaaguuuaagggucucagcuuuuucuuuucucuc\n", "uccucuucaggaucaucuucucgaaccccgagugacaagccuguagcccauguuguagguaagagcucug\n", "aggaugugucuuggaacuuggagggcuaggauuuggggauugaagcccggcugaugguaggcagaacuug\n", "gagacaaugugagaaggacucgcugagcucaagggaaggguggaggaacagcacaggccuuagugggaua\n", "cucagaacgucauggccaggugggaugugggaugacagacagagaggacaggaaccggaugugggguggg\n", "cagagcucgagggccaggauguggagagugaaccgacauggccacacugacucuccucucccucucuccc\n", "ucccuccagcaaacccucaagcugaggggcagcuccaguggcugaaccgccgggccaaugcccuccuggc\n", "caauggcguggagcugagagauaaccagcugguggugccaucagagggccuguaccucaucuacucccag\n", "guccucuucaagggccaaggcugccccuccacccaugugcuccucacccacaccaucagccgcaucgccg\n", "ucuccuaccagaccaaggucaaccuccucucugccaucaagagccccugccagagggagaccccagaggg\n", "ggcugaggccaagcccugguaugagcccaucuaucugggaggggucuuccagcuggagaagggugaccga\n", "cucagcgcugagaucaaucggcccgacuaucucgacuuugccgagucugggcaggucuacuuugggauca\n", "uugcccugugaggaggacgaacauccaaccuucccaaacgccuccccugccccaaucccuuuauuacccc\n", "cuccuucagacacccucaaccucuucuggcucaaaaagagaauugggggcuuagggucggaacccaagcu\n", "uagaacuuuaagcaacaagaccaccacuucgaaaccugggauucaggaauguguggccugcacagugaag\n", "ugcuggcaaccacuaagaauucaaacuggggccuccagaacucacuggggccuacagcuuugaucccuga\n", "caucuggaaucuggagaccagggagccuuugguucuggccagaaugcugcaggacuugagaagaccucac\n", "cuagaaauugacacaaguggaccuuaggccuuccucucuccagauguuuccagacuuccuugagacacgg\n", "agcccagcccuccccauggagccagcucccucuauuuauguuugcacuugugauuauuuauuauuuauuu\n", "auuauuuauuuauuuacagaugaauguauuuauuugggagaccgggguauccugggggacccaauguagg\n", "agcugccuuggcucagacauguuuuccgugaaaacggagcugaacaauaggcuguucccauguagccccc\n", "uggccucugugccuucuuuugauuauguuuuuuaaaauauuuaucugauuaaguugucuaaacaaugcug\n", "auuuggugaccaacugucacucauugcugagccucugcuccccaggggaguugugucuguaaucgcccua\n", "cuauucaguggcgagaaauaaaguuugcuuagaaaagaaa\n" ] } ], "source": [ "print(tnf.translate(tnf.maketrans('ACTG', 'acug')))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**3**. You have 5 kids in your household, whose behavior has been\n", "\n", "- Ann, Good\n", "- Bob, Bad\n", "- Charlie, Good\n", "- David, Good\n", "- Ella, Bad\n", "\n", "On Christmas Eve, Santa will give good kids an iPhone 7 and bad kids a lump of coal.\n", "\n", "**3a**. Store the kids name and behavior in a dictionary called `santa_dict`.\n", "\n", "**3b**. On Christmas Eve Eve, David threw a tantrum and kicked his sister Ann. Change the dictionary entry for David to Bad.\n", "\n", "**3c**. Write a loop that prints the name of each child, followed by 'Coal' or 'iPhone'. The output should be\n", "\n", "```python\n", "David Coal\n", "Ann iPhone\n", "Ella Coal\n", "Charlie iPhone\n", "Bob Coal\n", "```" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": true }, "outputs": [], "source": [ "santa_dict = {\n", " 'Ann': 'Good',\n", " 'Bob': 'Bad',\n", " 'Charlie': 'Good',\n", " 'David': 'Good',\n", " 'Ella': 'Bad'\n", " }" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": true }, "outputs": [], "source": [ "santa_dict['David'] = 'Bad'" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "David Coal\n", "Bob Coal\n", "Ann iPhone\n", "Charlie iPhone\n", "Ella Coal\n" ] } ], "source": [ "for kid, behavior in santa_dict.items():\n", " if behavior == 'Good':\n", " print(kid, 'iPhone')\n", " else:\n", " print(kid, 'Coal')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**4a**. Write a function (call the function `collatz`) of a positive integer that returns the following result \n", "\n", "- If the number is even, divide it by two\n", "- If the number is odd, triple it and add one\n", "\n", "![Collatz](https://wikimedia.org/api/rest_v1/media/math/render/svg/f69ea6c9163eefcadeb36c93a68626610f1f4e75)\n", "\n", "**4b**. Write a loop that repeatedly calls `n = collatz(n)` given some start value `n` while `n` is not equal to 1. At each iteration in the loop, print the current value of `n`.\n", "\n", "**4c**. Write a function `collatz_sequence` that takes a positive integer argument `n` and returns the list of numbers generated by the while loop from **4b** starting with the given value of `n`. For example, `collatz_sequence(6)` should give the following output:\n", "\n", "```python\n", "[6, 3, 10, 5, 16, 8, 4, 2, 1]\n", "```" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def collatz(n):\n", " \"\"\"The hailstone function for generating Collatz sequences.\"\"\"\n", " if n % 2 == 0:\n", " return n // 2\n", " else:\n", " return 3*n + 1" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6, 3, 10, 5, 16, 8, 4, 2, " ] } ], "source": [ "n = 6\n", "while n != 1:\n", " print(n, end=', ')\n", " n = collatz(n)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def collatz_sequence(n):\n", " \"\"\"Returns the Collatz seqeuece beginnning with n.\"\"\"\n", " ns = [n]\n", " while n != 1:\n", " n = collatz(n)\n", " ns.append(n)\n", " return ns" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[6, 3, 10, 5, 16, 8, 4, 2, 1]" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "collatz_sequence(6)" ] } ], "metadata": { "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" }, "latex_envs": { "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 0 } }, "nbformat": 4, "nbformat_minor": 0 }