Skip to content

Instantly share code, notes, and snippets.

@gregcaporaso
Last active October 12, 2015 07:47
Show Gist options
  • Save gregcaporaso/3994000 to your computer and use it in GitHub Desktop.
Save gregcaporaso/3994000 to your computer and use it in GitHub Desktop.
IPython Notebook files used in Greg Caporaso's Fall 2012 BIO599 Computational Biology course. See the included README.md file for more details and licensing information.

IPython Notebook files used in Greg Caporaso's Fall 2012 BIO599 Computational Biology course.

These closely follow the Python Programming chapters of Practical Computing for Biologists. A lot of exercises can be found in Learn Python the Hard Way.

This work is licensed under the Creative Commons Attribution 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.

Feel free to use or modify these notebooks, but please credit me by placing the following attribution information where you feel that it makes sense: Greg Caporaso, www.caporaso.us.

Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "Caporaso Lecture 23"
},
"name": "Caporaso Lecture 23",
"nbformat": 2,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"source": "**Defining, indexing, and slicing lists**\n\nLists are defined using the ``[`` and ``]`` (the square brackets), and contain zero or more elements. Here are a few lists:"
},
{
"cell_type": "code",
"collapsed": true,
"input": "l = ['a', 'b', 'c', 'd', 'e']",
"language": "python",
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": true,
"input": "m = []",
"language": "python",
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": true,
"input": "n = [0, 1, 2, 3]",
"language": "python",
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "markdown",
"source": "The ``[`` and ``]`` are also used to index into lists to retrieve or set elements after a list has been created."
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l[3]",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "d"
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": "x = n[2]\nprint x",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "2"
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"source": "You can also specify a range of values when indexing into lists, which returns a new list. Figure 9.2 in Practical Computing for Biologists is execellent for helping to understand list indexing."
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l[1:3]",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "['b', 'c']"
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"source": "To index from the beginning of a list, you can reference position ``0``, or leave that position blank."
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l[:3]",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "['a', 'b', 'c']"
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l[0:3] == l[:3]",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "True"
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"source": "To index from the end of a list, you can leave the second range position blank."
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l[2:]",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "['c', 'd', 'e']"
}
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"source": "You can also index into lists from the end, using negative indices. For example, ``-1`` refers to the last position in the list."
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l[-1]",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "e"
}
],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l[-2]",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "d"
}
],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l[-2:]",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "['d', 'e']"
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"source": "Finally, you can specify a step size when indexing a list to get every ``n-th`` position, where ``n`` is your step size."
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l[0:5:2]",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "['a', 'c', 'e']"
}
],
"prompt_number": 19
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l[::2]",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "['a', 'c', 'e']"
}
],
"prompt_number": 20
},
{
"cell_type": "markdown",
"source": "You can also step in reverse by providing a negative step size. The most common application of this is getting a reverse list."
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l[::-1]",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "['e', 'd', 'c', 'b', 'a']"
}
],
"prompt_number": 22
},
{
"cell_type": "markdown",
"source": "**Additional ways of creating lists**\n\nThe range function allows you to define a list spanning some range of values. Get help on range, and experiment with some usage examples."
},
{
"cell_type": "code",
"collapsed": false,
"input": "help(range)",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Help on built-in function range in module __builtin__:\n\nrange(...)\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"
}
],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": false,
"input": "print range(5)",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[0, 1, 2, 3, 4]"
}
],
"prompt_number": 24
},
{
"cell_type": "code",
"collapsed": false,
"input": "print range(-4,3,2)",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[-4, -2, 0, 2]"
}
],
"prompt_number": 25
},
{
"cell_type": "code",
"collapsed": false,
"input": "print range(99,0)",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[]"
}
],
"prompt_number": 26
},
{
"cell_type": "code",
"collapsed": false,
"input": "print range(99,0,-1)",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]"
}
],
"prompt_number": 27
},
{
"cell_type": "code",
"collapsed": false,
"input": "print range(0,99,-1)",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[]"
}
],
"prompt_number": 28
},
{
"cell_type": "markdown",
"source": "You can create a list from any string using the ``list`` function."
},
{
"cell_type": "code",
"collapsed": false,
"input": "l = list('abcde')\nprint l",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "['a', 'b', 'c', 'd', 'e']"
}
],
"prompt_number": 31
},
{
"cell_type": "markdown",
"source": "But this often isn't necessary, as indexing and slicing strings functions in the same was as indexing and slicing lists, except that strings are returned rather than lists.."
},
{
"cell_type": "code",
"collapsed": true,
"input": "s = \"abcde\"",
"language": "python",
"outputs": [],
"prompt_number": 33
},
{
"cell_type": "code",
"collapsed": false,
"input": "print s[2]",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "c"
}
],
"prompt_number": 34
},
{
"cell_type": "code",
"collapsed": false,
"input": "print s[2:4]",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "cd"
}
],
"prompt_number": 35
},
{
"cell_type": "code",
"collapsed": false,
"input": "print s[::-1]",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "edcba"
}
],
"prompt_number": 36
},
{
"cell_type": "markdown",
"source": "This last command should illuminate a 'black box' from Programming Assignment 1."
},
{
"cell_type": "markdown",
"source": "Converting from a list to a string is a little more confusing that going from a string to a list. To achieve this, you can use the following command."
},
{
"cell_type": "code",
"collapsed": false,
"input": "s = ''.join(['w','x','y','z'])\nprint s",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "wxyz"
}
],
"prompt_number": 44
},
{
"cell_type": "markdown",
"source": "**Nested for loops**\n\nChapter 9 of Practical Computing for Biologists presents an exercise based on printing the labels associated with the wells of a 96-well plate. To achieve this, you can define a couple of lists, nest one within the other, and use some print statements. For example:\n"
},
{
"cell_type": "code",
"collapsed": false,
"input": "columns = range(1,13)\nprint columns",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]"
}
],
"prompt_number": 39
},
{
"cell_type": "code",
"collapsed": false,
"input": "rows = list('ABCDEFGH')\nprint rows",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']"
}
],
"prompt_number": 40
},
{
"cell_type": "code",
"collapsed": false,
"input": "for r in rows:\n for c in columns:\n print r + str(c), \n print \"\"",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 \nB1 B2 B3 B4 B5 B6 B7 B8 B9 B10 B11 B12 \nC1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 \nD1 D2 D3 D4 D5 D6 D7 D8 D9 D10 D11 D12 \nE1 E2 E3 E4 E5 E6 E7 E8 E9 E10 E11 E12 \nF1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 \nG1 G2 G3 G4 G5 G6 G7 G8 G9 G10 G11 G12 \nH1 H2 H3 H4 H5 H6 H7 H8 H9 H10 H11 H12 "
}
],
"prompt_number": 43
},
{
"cell_type": "markdown",
"source": "There are some very convenient variations on the above for loop that you'll likely end up using in practice. For example, you don't need to pre-define the lists - you can define them in the for loops themselves. You also don't need to convert the string `'ABCDEFGH'` to a list - you can iterate over the string directly. Experiment with making some of these changes to the above code."
},
{
"cell_type": "markdown",
"source": "**Modifying lists**\n\nAn additional application of list indexing is that you can set or modify existing entries in a list."
},
{
"cell_type": "code",
"collapsed": true,
"input": "l = list('hello')",
"language": "python",
"outputs": [],
"prompt_number": 60
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "['h', 'e', 'l', 'l', 'o']"
}
],
"prompt_number": 61
},
{
"cell_type": "code",
"collapsed": true,
"input": "l[0] = 'H'",
"language": "python",
"outputs": [],
"prompt_number": 62
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "['H', 'e', 'l', 'l', 'o']"
}
],
"prompt_number": 63
},
{
"cell_type": "markdown",
"source": "This doesn't work however for adding to a list:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "l[5] = '!'",
"language": "python",
"outputs": [
{
"ename": "IndexError",
"evalue": "list assignment index out of range",
"output_type": "pyerr",
"traceback": [
"<span class=\"ansired\">---------------------------------------------------------------------------</span>\n<span class=\"ansired\">IndexError</span> Traceback (most recent call last)",
"<span class=\"ansigreen\">/home/ubuntu/&lt;ipython-input-64-3f202ca93047&gt;</span> in <span class=\"ansicyan\">&lt;module&gt;</span><span class=\"ansiblue\">()</span>\n<span class=\"ansigreen\">----&gt; 1</span><span class=\"ansiyellow\"> </span>l<span class=\"ansiyellow\">[</span><span class=\"ansicyan\">5</span><span class=\"ansiyellow\">]</span> <span class=\"ansiyellow\">=</span> <span class=\"ansiblue\">&apos;!&apos;</span><span class=\"ansiyellow\"></span>\n",
"<span class=\"ansired\">IndexError</span>: list assignment index out of range"
]
}
],
"prompt_number": 64
},
{
"cell_type": "markdown",
"source": "Instead you need to append an item to the list."
},
{
"cell_type": "code",
"collapsed": false,
"input": "l.append('!')",
"language": "python",
"outputs": [],
"prompt_number": 65
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[&apos;H&apos;, &apos;e&apos;, &apos;l&apos;, &apos;l&apos;, &apos;o&apos;, &apos;!&apos;]"
}
],
"prompt_number": 66
},
{
"cell_type": "code",
"collapsed": true,
"input": "l.append('?')\nl.append('!')",
"language": "python",
"outputs": [],
"prompt_number": 67
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[&apos;H&apos;, &apos;e&apos;, &apos;l&apos;, &apos;l&apos;, &apos;o&apos;, &apos;!&apos;, &apos;?&apos;, &apos;!&apos;]"
}
],
"prompt_number": 68
},
{
"cell_type": "markdown",
"source": "You can insert into specific positions by specifying a range to insert to"
},
{
"cell_type": "code",
"collapsed": true,
"input": "l[5:5] = [' ','w','o','r','l','d']",
"language": "python",
"outputs": [],
"prompt_number": 69
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[&apos;H&apos;, &apos;e&apos;, &apos;l&apos;, &apos;l&apos;, &apos;o&apos;, &apos; &apos;, &apos;w&apos;, &apos;o&apos;, &apos;r&apos;, &apos;l&apos;, &apos;d&apos;, &apos;!&apos;, &apos;?&apos;, &apos;!&apos;]"
}
],
"prompt_number": 70
},
{
"cell_type": "markdown",
"source": "Finally, you can delete elements from a list using the ``del`` function."
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l[12]",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "?"
}
],
"prompt_number": 71
},
{
"cell_type": "code",
"collapsed": true,
"input": "del l[12]",
"language": "python",
"outputs": [],
"prompt_number": 72
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l[12]",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "!"
}
],
"prompt_number": 73
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[&apos;H&apos;, &apos;e&apos;, &apos;l&apos;, &apos;l&apos;, &apos;o&apos;, &apos; &apos;, &apos;w&apos;, &apos;o&apos;, &apos;r&apos;, &apos;l&apos;, &apos;d&apos;, &apos;!&apos;, &apos;!&apos;]"
}
],
"prompt_number": 74
},
{
"cell_type": "code",
"collapsed": true,
"input": "del l[5:12]",
"language": "python",
"outputs": [],
"prompt_number": 75
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[&apos;H&apos;, &apos;e&apos;, &apos;l&apos;, &apos;l&apos;, &apos;o&apos;, &apos;!&apos;]"
}
],
"prompt_number": 76
},
{
"cell_type": "markdown",
"source": "**Some additionally useful tools for working with lists**"
},
{
"cell_type": "markdown",
"source": "Using ``in`` to check the contents of a list"
},
{
"cell_type": "code",
"collapsed": false,
"input": "print 'l' in l",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "True"
}
],
"prompt_number": 77
},
{
"cell_type": "code",
"collapsed": false,
"input": "print '?' in l",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "False"
}
],
"prompt_number": 78
},
{
"cell_type": "markdown",
"source": "Sorting lists using ``sort`` - note that this changes your list."
},
{
"cell_type": "code",
"collapsed": false,
"input": "l = [1,5,3,9,55,88,42,-6]\nprint l",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[1, 5, 3, 9, 55, 88, 42, -6]"
}
],
"prompt_number": 80
},
{
"cell_type": "code",
"collapsed": true,
"input": "l.sort()",
"language": "python",
"outputs": [],
"prompt_number": 81
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[-6, 1, 3, 5, 9, 42, 55, 88]"
}
],
"prompt_number": 82
},
{
"cell_type": "markdown",
"source": "Sorting lists using ``sorted`` - note that this does not change your list."
},
{
"cell_type": "code",
"collapsed": false,
"input": "l = [1,5,3,9,55,88,42,-6]\nprint l",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[1, 5, 3, 9, 55, 88, 42, -6]"
}
],
"prompt_number": 83
},
{
"cell_type": "code",
"collapsed": true,
"input": "new_l = sorted(l)",
"language": "python",
"outputs": [],
"prompt_number": 84
},
{
"cell_type": "code",
"collapsed": false,
"input": "print new_l",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[-6, 1, 3, 5, 9, 42, 55, 88]"
}
],
"prompt_number": 85
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[1, 5, 3, 9, 55, 88, 42, -6]"
}
],
"prompt_number": 86
},
{
"cell_type": "markdown",
"source": "Identifying the unique elements in a string (or list) using ``set``"
},
{
"cell_type": "code",
"collapsed": true,
"input": "dna_seq = 'ACCGTGGACCGATTGACCAGATTGACCAG'",
"language": "python",
"outputs": [],
"prompt_number": 87
},
{
"cell_type": "code",
"collapsed": false,
"input": "print list(set(dna_seq))",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[&apos;A&apos;, &apos;C&apos;, &apos;T&apos;, &apos;G&apos;]"
}
],
"prompt_number": 88
},
{
"cell_type": "code",
"collapsed": true,
"input": "",
"language": "python",
"outputs": [],
"prompt_number": "&nbsp;"
}
]
}
]
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
from sys import argv
usage = "Lecture25_example.py <name> <day>"
if len(argv) != 3:
print "ERROR: Incorrect number of arguments passed."
print "USAGE: " + usage
else:
script_name, name, day = argv
print "Hello " + name
print "Today is " + day
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment