Day 1: Introduction Python and iPython Notebooks, variables and data types, simple conversions, expressions and conditionals


Exercises 1: Getting familiar with Python and iPython Notebook (30 minutes):

  1. The following commands can be run in iPython notebook by typing them in a "code box" and then hitting alt + enter. The code entered will be run and a new code box appears. Try to type in and run (not copy-paste) the following commands, one by one, and see what happens.
In []:
  1. How do characters and numbers with and without quotes seem to interact with the '+' operator? How is division working (or not working)?
In []:
  1. Try using the Python documentation on strings (str) to figure out what 'ACACAGT'.count('A') would return and what 'ACACAGT'.count('AC') would return. Based on this, what should 'ACACAGT'.count('C') + 6 return? Try using the python documentation to learn about the function called len().
In []:
  1. What happens if you do len('ACACAGT')? With this, use count and len to calculate the AT content of the above phrase.
In []:
  1. Practice troubleshooting by breaking this erroneous statement into components and finding the smallest snippet of the statement needed to give the error you see.
In []:
(1 + '2/4') / 6


##Exercises 2: variables and types (45 minutes):

  1. Here are some uses of int, str, and float that Python can use to convert values between data types. See if you can predict the output of the following commands. Note that Python follows the mathematical order of operations as well as the order established in math for parentheses. Try some of your own also.
In []:
  1. Use the '=' sign to store some values as variables. Note that this '=' is not a math sign anymore! 1 = 0 + 1 is not valid in Python, and x = 3 is valid but 3 = x is not.
In []:
  1. Experiment with different variable names. Values can be stored as lettered variables as in algebra.
In []:
  1. Try these operations with numbers stored as variables.
In []:
  1. You can store the results of an operation as a variable, ex. z = x + y.
    See what happens when you store the results of an operation as one of the input variables, ex. x = x + y. What is x now? (type the variable name followed by the alt + enter to see what's currently stored in any variable). What happens to x if you do x = x + y again and again? If you initially chose strings for x and y, try it with integers. If you chose integers, try it with strings.
In []:
  1. Using the Python documentation on str and the entry under replace, have Python convert a string of DNA into a string of RNA and store the result as a variable.
In []:


##Exercises 3: conditionals and scripts (1 hour)

  1. Here are some example conditionals in python to enter in the interactive python to see if these things are considered true. Try rearranging the order and the symbols to guess what python derives when it comes to an answer.
In []:
  1. Python can use conditionals to make decisions. See what happens if you run the following statements (note the indentation).
In []:
if 3 < 5:
    print '3 is smaller than 5'
In []:
if 9999999 < 'AA':
    print 'AA must be big!'
In []:
if 'AACC' < 'AAAC':
    smaller = 'AACC'
else:
    smaller = 'AAAC'
print 'smaller is ' + smaller
  1. Write a program that tests whether 'Q' is in the amino acid sequence 'ALSQDQEI'.
In []:
  1. Modify the program to print the position of 'Q' if 'Q' is present (use the entry on index from the Python documentation on str to figure out how to determine the location of a character or short substring within a longer string). The result may not be what you expect. Try the same thing searching for the character 'A'.
In []:
  1. Good programs work well regardless of the input. Test your program searching for a character that isn't in the string (for example, 'M'). If 'Q' works and 'M' gives an error, the if statement needs to be revised so that it finds 'Q' and doesn't give an error when it searches for 'M'.
In []: