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
{
"metadata": {
"name": "Caporaso Lecture 22"
},
"name": "Caporaso Lecture 22",
"nbformat": 2,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"source": "**Boolean variables**\n\nBoolean variables are used to represent the values ``True`` and ``False`` only. \n"
},
{
"cell_type": "code",
"collapsed": true,
"input": "x = True\ny = False",
"language": "python",
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"source": "Logical operators such as ``==``, ``>=``, and ``>`` return boolean values."
},
{
"cell_type": "code",
"collapsed": false,
"input": "z = 43\nprint z > 41",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "True"
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"source": "These values can of course be assigned to new variables."
},
{
"cell_type": "code",
"collapsed": false,
"input": "x = z > 41\nprint x",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "True"
}
],
"prompt_number": 15
},
{
"cell_type": "markdown",
"source": "The comparative operators that python implements are the following. Modify the values of ``x`` and ``y`` to see their affect on the result of these statements."
},
{
"cell_type": "code",
"collapsed": false,
"input": "print x == y # equal to\nprint x != y # not equal to\nprint x > y # greater than\nprint x < y # less than\nprint x >= y # greater than or equal to\nprint x <= y # less than or equal to",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "False\nTrue\nTrue\nFalse\nTrue\nFalse"
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"source": "**Conditional statements**\n\nConditional statements are used to control whether specific *blocks* of code are executed. (The code indented under an ``if`` statement is often called a *block* of code.) In an ``if`` statement, the indented block is executed if the `condition` evalutes to `True`.\n\nBoolean variables are used with conditional statements. Experiment with the if blocks below to learn how this works."
},
{
"cell_type": "code",
"collapsed": false,
"input": "if z > 41:\n print \"I'm in the if block\"",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "I&apos;m in the if block"
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": "if True:\n print \"I'm in another if block\"",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "I&apos;m in another if block"
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": true,
"input": "if False:\n print \"I'm in this if block\"",
"language": "python",
"outputs": [],
"prompt_number": 9
},
{
"cell_type": "markdown",
"source": "To switch between exectuing different blocks of code based on a boolean value, you can use an ``if/else`` statement as follows. Again, experiment with the following code to learn how this works."
},
{
"cell_type": "code",
"collapsed": false,
"input": "if z < 41:\n print \"I'm in the if block\"\nelse:\n print \"I'm in the else block\"",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "I&apos;m in the else block"
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"source": "Logical operators, such as ``and``, ``or`` and ``not`` can be used to compare boolean values and also return boolean values. "
},
{
"cell_type": "code",
"collapsed": false,
"input": "print z < 41 and z > 41",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "False"
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": "print z < 41 or z > 41",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "True"
}
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"source": "As these get increasingly complex, you can use parenthesis to help control the order of operations. "
},
{
"cell_type": "code",
"collapsed": false,
"input": "print not (z < 41 or z > 41)",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "False"
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"source": "To switch between more than just two blocks of code, you can use ``if/elif/else`` statements. "
},
{
"cell_type": "code",
"collapsed": false,
"input": "\nbase = \"U\"\nif base == \"A\" or base == \"a\":\n print \"Adenine\"\nelif base == \"C\" or base == \"c\":\n print \"Cytosine\"\nelif base == \"G\" or base == \"g\":\n print \"Guanine\"\nelif base == \"T\" or base == \"t\":\n print \"Thymine\"\nelse:\n print \"Base \" + str(base) + \" is not an IUPAC DNA abbreviation\"\n ",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Base U is not an IUPAC DNA abbreviation"
}
],
"prompt_number": 22
},
{
"cell_type": "markdown",
"source": "**Loops: the ``for`` loop**\n\nThe ``for`` loop is used to iterate over the entries in an iterable type, such as a ``string`` or ``list``. A new variable is incrementally set to the value of the next entry in the iterable object until there are no items left, at which point the program leaves the ``for`` loop."
},
{
"cell_type": "code",
"collapsed": false,
"input": "dna_seq = \"ACCGGTTATTACC\"\nfor base in dna_seq:\n print base",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "A\nC\nC\nG\nG\nT\nT\nA\nT\nT\nA\nC\nC"
}
],
"prompt_number": 23
},
{
"cell_type": "markdown",
"source": "Using a for loop, you could implement a simpler version of the complementing functionality assigned in Programming Assignment 1."
},
{
"cell_type": "code",
"collapsed": false,
"input": "dna_seq = \"ACCGTGGACCAGGTTAA\"\ncomp = \"\"\nfor base in dna_seq:\n if base == \"A\" or base == \"a\":\n comp = comp + \"T\"\n elif base == \"C\" or base == \"c\":\n comp = comp + \"G\"\n elif base == \"G\" or base == \"g\":\n comp = comp + \"C\"\n elif base == \"T\" or base == \"t\":\n comp = comp + \"A\"\n else:\n print \"Base \" + str(base) + \" is not an IUPAC DNA abbreviation. Can not continue.\"\nprint \"Input sequence:\"\nprint dna_seq\nprint \"Complement of input sequence:\"\nprint comp",
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Input sequence:\nACCGTGGACCAGGTTAA\nComplement of input sequence:\nTGGCACCTGGTCCAATT"
}
],
"prompt_number": 24
},
{
"cell_type": "markdown",
"source": "Using other types that we've learned about, what would be an even better way to implement this functionality?"
},
{
"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.
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