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
{
"metadata": {
"name": "Caporaso Lecture 20"
},
"name": "Caporaso Lecture 20",
"nbformat": 2,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"source": "**Defining integer, string, and floating point variables**"
},
{
"cell_type": "markdown",
"source": "Variables are set using the ``=`` operator. Here we define two integers, two strings, and two floats."
},
{
"cell_type": "code",
"collapsed": true,
"input": "i = 42\nj = 65\nk = \"hello\"\nl = \"world\"\nm = 42.0\nn = 43.0",
"language": "python",
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"source": "Operations can now be applied to these variables"
},
{
"cell_type": "code",
"collapsed": false,
"input": "print i + j",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "107"
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": "print m/n",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "0.976744186047"
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"source": "But type is important: operations have different meanings when applied to different types."
},
{
"cell_type": "code",
"collapsed": false,
"input": "print k + l",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "helloworld"
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"source": "And if you create data of the wrong type, you can get surprising results. What's happening here?"
},
{
"cell_type": "code",
"collapsed": false,
"input": "o = \"42\"\np = \"65\"\nprint o + p",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "4265"
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"source": "Sometimes operations can be applied to different types. When you add an integer and a float, float addition is performed."
},
{
"cell_type": "code",
"collapsed": false,
"input": "print i + m",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "84.0"
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"source": "But if you try to add an integer and a string, well, that doesn't really make sense. It's an undefined operation, and you get an error."
},
{
"cell_type": "code",
"collapsed": false,
"input": "print i + k",
"language": "python",
"outputs": [
{
"ename": "TypeError",
"evalue": "unsupported operand type(s) for +: 'int' and 'str'",
"output_type": "pyerr",
"traceback": [
"<span class=\"ansired\">---------------------------------------------------------------------------</span>\n<span class=\"ansired\">TypeError</span> Traceback (most recent call last)",
"<span class=\"ansigreen\">/home/ubuntu/&lt;ipython-input-9-84ad1efdbebe&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><span class=\"ansigreen\">print</span> i <span class=\"ansiyellow\">+</span> k<span class=\"ansiyellow\"></span>\n",
"<span class=\"ansired\">TypeError</span>: unsupported operand type(s) for +: &apos;int&apos; and &apos;str&apos;"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"source": "**Changing the type of a variable**"
},
{
"cell_type": "markdown",
"source": "The type of a variable can be changed by `type casting` the variable. If we define a variable as an integer (i.e., an ``int``) we can subsequently cast it to a floating point value (i.e., a ``float``) by passing ``i`` to the ``float`` function.."
},
{
"cell_type": "code",
"collapsed": true,
"input": "i = 42",
"language": "python",
"outputs": [],
"prompt_number": 27
},
{
"cell_type": "code",
"collapsed": false,
"input": "print float(i)",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "42.0"
}
],
"prompt_number": 28
},
{
"cell_type": "markdown",
"source": "Note that we haven't yet actually changed ``i``:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "print i",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "42"
}
],
"prompt_number": 30
},
{
"cell_type": "markdown",
"source": "The ``float(i)`` statement just printed what it would look like if it was a float. To change the value you'd do the following:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "i = float(i)\nprint i",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "42.0"
}
],
"prompt_number": 31
},
{
"cell_type": "markdown",
"source": "**Lists and dictionaries**"
},
{
"cell_type": "markdown",
"source": "Lists are ordered collections of data. The elements of lists can be all of the same type or of different types."
},
{
"cell_type": "code",
"collapsed": true,
"input": "l = [1, 2, 3, 'hello', 4]",
"language": "python",
"outputs": [],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[1, 2, 3, &apos;hello&apos;, 4]"
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"source": "Elements in a list are accessed by their index in the list, and counting begins at 0."
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l[0]\nprint l[1]\nprint l[2]\nprint l[3]\nprint l[4]",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "1\n2\n3\nhello\n4"
}
],
"prompt_number": 19
},
{
"cell_type": "markdown",
"source": "If you try to index a position that is higher than the length of the list, you'll get an IndexError, indicating that that position doesn't exist (i.e., it's out of range)."
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l[5]",
"language": "python",
"outputs": [
{
"ename": "IndexError",
"evalue": "list 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-18-e61f378485c9&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><span class=\"ansigreen\">print</span> l<span class=\"ansiyellow\">[</span><span class=\"ansicyan\">5</span><span class=\"ansiyellow\">]</span><span class=\"ansiyellow\"></span>\n",
"<span class=\"ansired\">IndexError</span>: list index out of range"
]
}
],
"prompt_number": 18
},
{
"cell_type": "markdown",
"source": "Dictionaries are similar to lists, except they are an unordered set of (key, value) pairs. These are often useful when you have information that you'd like to access by name rather than by index, and when you don't care about the order. A good example is a dictionary containing the molecular weights of the amino acids represented in the canonical genetic code."
},
{
"cell_type": "code",
"collapsed": false,
"input": "amino_acid_mr = {\"Isoleucine\" : 131.1736,\n\"Leucine\" : 131.1736,\n\"Lysine\" : 146.1882,\n\"Methionine\" : 149.2124,\n\"Phenylalanine\" : 165.1900,\n\"Threonine\" : 119.1197,\n\"Tryptophan\" : 204.2262,\n\"Valine\" : 117.1469,\n\"Arginine\" : 174.2017,\n\"Histidine\" : 155.1552,\n\"Alanine\" : 89.0935,\n\"Asparagine\" : 132.1184,\n\"Aspartate\" : 133.1032,\n\"Cysteine\" : 121.1590,\n\"Glutamate\" : 147.1299,\n\"Glutamine\" : 146.1451,\n\"Glycine\" : 75.0669,\n\"Proline\" : 115.1310,\n\"Serine\" : 105.0930,\n\"Tyrosine\" : 181.1894}",
"language": "python",
"outputs": [],
"prompt_number": 22
},
{
"cell_type": "markdown",
"source": "You can then access the entries in the dictionary by their key (i.e., their name)"
},
{
"cell_type": "code",
"collapsed": false,
"input": "amino_acid_mr['Threonine']",
"language": "python",
"outputs": [
{
"output_type": "pyout",
"prompt_number": 23,
"text": "119.1197"
}
],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": false,
"input": "amino_acid_mr['Valine']",
"language": "python",
"outputs": [
{
"output_type": "pyout",
"prompt_number": 24,
"text": "117.1469"
}
],
"prompt_number": 24
},
{
"cell_type": "markdown",
"source": "And you can perform operations directly on these"
},
{
"cell_type": "code",
"collapsed": false,
"input": "print amino_acid_mr['Threonine'] + amino_acid_mr['Valine']",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "236.2666"
}
],
"prompt_number": 25
},
{
"cell_type": "markdown",
"source": "**Applying python's function to variables**"
},
{
"cell_type": "markdown",
"source": "Python's variables have built-in functions that can be applied to them. For example, the ``len`` function can be call on lists, dictionaries, and strings. "
},
{
"cell_type": "code",
"collapsed": false,
"input": "print len(amino_acid_mr)",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "20"
}
],
"prompt_number": 32
},
{
"cell_type": "code",
"collapsed": false,
"input": "print len(\"Hello world!\")",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "12"
}
],
"prompt_number": 33
},
{
"cell_type": "markdown",
"source": "Some variables also have built-in functions, known as methods. It can be a little confusing at first to figure out what operations are accessible via functions (where your varaible is passes as an argument) or via methods. It's an unfortunate feature of python that operations are defined differently, and something that the python development group is working on fixing in Python 3."
},
{
"cell_type": "markdown",
"source": "``keys`` is a built-in method of dictionaries, which returns a list of the keys in the dictionary."
},
{
"cell_type": "code",
"collapsed": false,
"input": "amino_acid_mr.keys()",
"language": "python",
"outputs": [
{
"output_type": "pyout",
"prompt_number": 38,
"text": "[&apos;Lysine&apos;,\n &apos;Alanine&apos;,\n &apos;Glycine&apos;,\n &apos;Glutamate&apos;,\n &apos;Asparagine&apos;,\n &apos;Glutamine&apos;,\n &apos;Arginine&apos;,\n &apos;Phenylalanine&apos;,\n &apos;Leucine&apos;,\n &apos;Methionine&apos;,\n &apos;Serine&apos;,\n &apos;Valine&apos;,\n &apos;Cysteine&apos;,\n &apos;Tryptophan&apos;,\n &apos;Isoleucine&apos;,\n &apos;Threonine&apos;,\n &apos;Proline&apos;,\n &apos;Aspartate&apos;,\n &apos;Histidine&apos;,\n &apos;Tyrosine&apos;]"
}
],
"prompt_number": 38
},
{
"cell_type": "markdown",
"source": "To find out what methods are available, you can call the ``dir`` function, passing your varaible as an argument to that function. Ignore the ones beginning with ``__`` for now."
},
{
"cell_type": "code",
"collapsed": false,
"input": "s = \"Hello world!\"\ndir(s)",
"language": "python",
"outputs": [
{
"output_type": "pyout",
"prompt_number": 39,
"text": "[&apos;__add__&apos;,\n &apos;__class__&apos;,\n &apos;__contains__&apos;,\n &apos;__delattr__&apos;,\n &apos;__doc__&apos;,\n &apos;__eq__&apos;,\n &apos;__format__&apos;,\n &apos;__ge__&apos;,\n &apos;__getattribute__&apos;,\n &apos;__getitem__&apos;,\n &apos;__getnewargs__&apos;,\n &apos;__getslice__&apos;,\n &apos;__gt__&apos;,\n &apos;__hash__&apos;,\n &apos;__init__&apos;,\n &apos;__le__&apos;,\n &apos;__len__&apos;,\n &apos;__lt__&apos;,\n &apos;__mod__&apos;,\n &apos;__mul__&apos;,\n &apos;__ne__&apos;,\n &apos;__new__&apos;,\n &apos;__reduce__&apos;,\n &apos;__reduce_ex__&apos;,\n &apos;__repr__&apos;,\n &apos;__rmod__&apos;,\n &apos;__rmul__&apos;,\n &apos;__setattr__&apos;,\n &apos;__sizeof__&apos;,\n &apos;__str__&apos;,\n &apos;__subclasshook__&apos;,\n &apos;_formatter_field_name_split&apos;,\n &apos;_formatter_parser&apos;,\n &apos;capitalize&apos;,\n &apos;center&apos;,\n &apos;count&apos;,\n &apos;decode&apos;,\n &apos;encode&apos;,\n &apos;endswith&apos;,\n &apos;expandtabs&apos;,\n &apos;find&apos;,\n &apos;format&apos;,\n &apos;index&apos;,\n &apos;isalnum&apos;,\n &apos;isalpha&apos;,\n &apos;isdigit&apos;,\n &apos;islower&apos;,\n &apos;isspace&apos;,\n &apos;istitle&apos;,\n &apos;isupper&apos;,\n &apos;join&apos;,\n &apos;ljust&apos;,\n &apos;lower&apos;,\n &apos;lstrip&apos;,\n &apos;partition&apos;,\n &apos;replace&apos;,\n &apos;rfind&apos;,\n &apos;rindex&apos;,\n &apos;rjust&apos;,\n &apos;rpartition&apos;,\n &apos;rsplit&apos;,\n &apos;rstrip&apos;,\n &apos;split&apos;,\n &apos;splitlines&apos;,\n &apos;startswith&apos;,\n &apos;strip&apos;,\n &apos;swapcase&apos;,\n &apos;title&apos;,\n &apos;translate&apos;,\n &apos;upper&apos;,\n &apos;zfill&apos;]"
}
],
"prompt_number": 39
},
{
"cell_type": "markdown",
"source": "``replace`` is a particularly useful method (especially if you're working on `Programming Assignment 1`). What am I doing here? "
},
{
"cell_type": "code",
"collapsed": false,
"input": "s = s.replace('l','L')\nprint s",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "HeLLo worLd!"
}
],
"prompt_number": 41
},
{
"cell_type": "markdown",
"source": "When working in an interactive python session, you can get help on a method or function using the ``help`` function:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "print help(len)",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Help on built-in function len in module __builtin__:\n\nlen(...)\n len(object) -&gt; integer\n \n Return the number of items of a sequence or mapping.\n\nNone"
}
],
"prompt_number": 42
},
{
"cell_type": "code",
"collapsed": false,
"input": "print help(s.replace)",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Help on built-in function replace:\n\nreplace(...)\n S.replace(old, new[, count]) -&gt; string\n \n Return a copy of string S with all occurrences of substring\n old replaced by new. If the optional argument count is\n given, only the first count occurrences are replaced.\n\nNone"
}
],
"prompt_number": 43
},
{
"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.
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