Python for Fall 2014

Ntino Krampis, PhD! Associate Professor! Biological Sciences! !

Class 09-02-2014

1 Course Structure

• Meets weekly on Tuesdays 2 - 4pm

• Office hours on Mondays 10am - 12pm, will not answer email anymore.

• Weekly homework due on day of class

• Will follow textbook as close as possible

• Homework is writing code for data analysis: work in computer lab or your PC

2 Syllabus

(Detailed syllabus on the Blackboard)

09-02-14: Introduction to Programming and first steps with Data Types in Python

09-09-14: Programming: More Data Types & Flow Control,

09-16-14: Basic Programming: Advanced Data Types & Flow Control

09-23-14: Bioinformatics: Files & Regular Expressions

3 Bioinformatics

• Joint field of Biology and

• Use computer code for analysis of “-omics” datasets

• Interested in doing research in Bioinformatics: get a good grade in the class and join my lab!

4 Computer & Code

Difference between an and code

This is an algorithm

5 Computer Algorithms & Code

This is computer code

Computer code is instructions in computer language, on how to execute the steps of the algorithm.

6 Computer Algorithms & Code

This is a bioinformatic algorithm and code

7 Computer Code in Python

• Python is a “”, code is called “scripts”

• Scripts are “interpreted, line by line” by the Python interpreter

• The interpreter can be interactive (good for learning or testing code)

• Or can run programs in batch, reading the code from a file (we will do this for homework)

8 The Python Interpreter

• Open the terminal in your Mac computer

• Either by clicking the icon on the dock

• Or by hitting cmd+space and search for terminal:

9 Python Variables: Strings

Ntinos-MacBook-Pro:~ krampis$ python Python 2.7.5 (default, Mar 9 2014, 22:15:05) [GCC 4.2.1 Compatible Apple LLVM 5.0 (-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information. ! ! >>> print("Hello World") Hello World ! ! >>> message = "Hello World!" >>> print(message) Hello World! ! ! >>> message = input() "Hello World!” >>> print(message) Hello World! ! ! >>> greeting = "your message to the world?" >>> message = input(greeting) your message to the world? "Hello World!" >>> print message Hello World!

10 Python Variables: Integers

! >>> integer = 2 >>> print(integer) 2 ! >>> google = 2 >>> print(google) 2 ! >>> yahoo = "2" >>> print(yahoo) 2 ! >>> google / 2 1 ! >>> yahoo / 2 Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for /: 'str' and ‘int' ! ! >>> google**2 4 ! >>> string**2 ????

11 String Quotations & Functions

>>> dna = "ACGT" >>> print dna ACGT String quotes need ! >>> dna = 'ACGT' to match on both >>> print dna ACGT sides ! >>> dna = "'ACGT'" ! >>> print dna ‘ACGT’ ! ! ! >>> dna = " 'ACGT' ‘ACGT’ “ ! >>> dna = ''' 'ACGT' 'ACGT' ''' ! Functions as built in ! >>> dna = 'ACGT' to Python, and >>> dna.lower() ‘acgt' operate on specific ! ! data types >>> dna = 'ACgt' >>> dna.upper() ‘ACGT’ ! ! >>> 2.upper() ???

12 String Quotations & Functions

>>> "ACGT".replace( 'A', 'T') ‘TCGT' ! >>> "'ACGT'".replace( 'A', 'T') “‘TCGT'" ! >>> import math >>> math.sqrt(2) 1.4142135623730951 ! >>> math.sqrt(“2”) ??? ! ! >>> "Hello my name is Ntino".split() ['Hello', 'my', 'name', 'is', ‘Ntino’] ! >>> name = ‘Hello my name is Ntino’ >>> name.split() ['Hello', 'my', 'name', 'is', 'Ntino'] ! ! >>> "Hello, my name is, Ntino".split(',') ['Hello', ' my name is', ' Ntino’] ! ! >>> "Hello my name is Ntino".split(',') ['Hello my name is Ntino']

13 String Quotations & Functions

Python language reference, section 5.6 ! https://docs.python.org/2/library/index.html

14 List Variables:

>>> "Hello my name is Ntino".split() ['Hello', 'my', 'name', 'is', ‘Ntino’] ! >>> list = [1,”two",3,4,"last"] ! >>> nested_list = ["a string", list, 0] ! >>> print nested_list ['a string', [1, 'two', 3, 4, 'last'], 0] ! >>> nested_nest_list = ["a string", nested_list, 0, list] ! ! >>> print(nested_nest_list) ['a string', ['a string', [1, 'two', 3, 4, 'last'], 0], 0, [1, 'two', 3, 4, ‘last']] ! ! >>> nested_nest_list = [] >>> print(nested_nest_list) []

15 Operations with List Variables:

! >>> nucleic = ['A', '', 'G', ’T'] ! >>> nucleic[1] 'C' >>> nucleic[0] ‘A' ! >>> nucleic[4] = 'U' Traceback (most recent call last): File "", line 1, in IndexError: list assignment index out of range ! ! >>> nucleic.append('U') >>> print(nucleic) ['A', 'C', 'G', 'T', ‘U'] ! >>> nucleic.pop() 'U' >>> print(nucleic) ['A', 'C', 'G', ’T'] ! ! >>> nucleic.append([1,2,3]) >>> print(nucleic) ['A', 'C', 'G', 'T', [1, 2, 3]]

16 Operations with List Variables:

! >>> print(nucleic) ['A', 'C', 'G', 'T', [1, 2, 3]] ! >>> nucleic[0:2] ['A', ‘C'] ! >>> nucleic[0:3] ['A', 'C', ‘G'] ! >>> nucleic[-1] [1, 2, 3] ! >>> nucleic.index('T') 3 ! >>> nucleic.index([1, 2, 3]) 4

17 Read Chapters 1(quick), Chapt. 2 up to 3.5 (critical)

! Do homework, due by next class

18