SRPC Summer 2014 Rockefeller Programming Class Josh Hug

advertisement
SRPC
Summer 2014 Rockefeller Programming Class Josh Hug 1
Course Logistics
• 
One weekly mee?ng 10 AM – Noon 30 to 45 minutes of lecture. –  Rest of ?me used for lab exercises. – 
• 
• 
Use Coursemology or plain HTML page (more later). One homework assignment per week Hopefully ~6 hours of work. –  Autograder available on web page. –  Submit solu?ons online using Coursemology. – 
• 
I’ll provide feedback on submiVed code. 2
Course Website and Resources
• 
joshh.ug/SRPC Lecture slides –  Labs and solu?ons –  Assignments and solu?ons – 
• 
Send emails to: SRPC@joshh.ug Piazza.com Ques?ons and answers visible to all. –  I’m happy to have help answering ques?ons! – 
• 
Coursemology.com: Primary pla^orm for doing the labs and submi_ng assignments. 3
Programming Languages
• 
Tasks Find the median of a list of numbers. –  Count how many palindromes appear in your emails. –  Calcula?ng masses and charges of an AA sequence. – 
• 
Special purpose tools – 
Examples Excel =MEDIAN(A1:A1000)
•  ImageJ / Fiji • 
– 
Great if they do what you want. 4
• 
Programming Language – 
• 
Concise set of simple instruc?ons that allows you to automate any possible task. Our class – 
Python 2.7 5
Why Python?
• 
• 
• 
• 
• 
• 
Free Highly portable (Windows/Mac/Linux) Interac?ve interpreter Simple syntax Syntax enforces good style Popular – 
• 
Vast libraries exist for complex tasks. Numerical libraries share syntax with MATLAB 6
A simple program
Python print("hello world”)
Java public class Hello {
public static void main(String[] args) {
System.out.println(“hello world”);
}
}
7
Why Not Python?
• 
• 
• 
Rela?vely slow (but this can be overcome) Library support is spoVy for some computa?onal and scien?fic purposes (compared to R and MATLAB) Can be annoying to distribute needed libraries – 
More later! 8
Why Python 2 (instead of 3)?
• 
BeVer support by scien?fic libraries. Syntax is very similar. • 
Note: • 
– 
Python 3 will eventually replace Python 2. 9
Example Python Program
• 
Suptest: Internet connec?on tes?ng tool 10
Example from SRPC 2013
• 
Given incomplete amino acid sequence XYSGYQQXGYQQYNP, where X denotes an unknown AA, calculate masses and charges. ‘XYSGYQQXGYQQYNP’
mw_and_charge_calc.py
output.csv
aa_mw_and_charge.csv
11
Another SRPC 2013 Example
• 
Taking big spreadsheets and dealing with blanks and zeros gracefully. 12
More SRPC 2013
• 
Combining MRI movie files into one big file (with some fancy logic to decide the order). • 
Genera?ng queries for BLAST website from mul?ple sequence files. Conversion of BLAST website output to CSV. 13
Roadmap (Weeks 1-4)
• 
• 
• 
• 
• 
• 
• 
• 
Variables / Assignment statements Math Basic Input Condi?onals Loops Lists, Tuples, Sets, and Dic?onaries Func?ons and Modules Input / Output Today 14
Assignment Statements
• 
• 
Variables are containers. Any variable can contain any type of data (dynamic typing). Equals puts the thing on the right hand side into the container on the lem hand side. 15
Assignment Statements
• 
When using = Must have a variable on the lem hand side. –  Right hand side must provide something valid that can be stuck in a variable (formal idea next week). –  If right hand side is a variable, the contents of the right hand variable are copied into the lem side (not an alias, more way later). – 
16
Mathematical Operators
• 
• 
Examples of Operators: + - / * **
Operators can be either binary or unary
17
Order of Operations
• 
Some opera?ons have higher precedence than others: • 
• 
• 
Exponen?a?on first: 2**3 = 8 Then mul?plica?on and division: 9 * 8 = 72 Then addi?on and subtrac?on: 3 + 72 = 75 18
Order of Operations
• 
Use parentheses if you want a different order • 
Inner most parentheses first: (3 + 9) = 12 Next parentheses: (12 * 2) = 24 Final parentheses: (24 ** 3) = 13,824 • 
• 
19
String Operators
• 
String concatena?on 20
Relational Operators
< Less than <= Less than or equal to
== Equal to > Greater than >= != Not equal to 21
Relational Operators
• 
Our numerical operators took one or two numbers and gave a new number. • 
Our string operator took two strings and returned one string. • 
Our rela?onal operators take two numbers and return a boolean. 22
Logical Operators
not(L)
L and R
L or R
returns opposite of L returns true if L and R are both true returns true if L or R or both are true 23
Data Types
• 
• 
type(x) gives you the type of x. Our types so far: int: Integers –  bool: Booleans –  str: Strings –  float: Real numbers (approximate) – 
26
Mixing Datatypes
• 
Python will yell at you if you improperly mix data types. • 
It’s not always obvious what cons?tutes impropriety. 27
Type Conversion
• 
Conversion func?on: Takes an object of one type –  Returns a new object of desired type – 
28
Word of Warning
• 
When dividing integers in Python 2, the remainder is discarded. Considered a flaw by Python’s designer. –  Changed in Python 3 to result in a float if necessary. – 
29
Roadmap
• 
• 
• 
• 
• 
• 
• 
• 
Variables / Assignment statements Math Basic Input Condi?onals Loops Arrays, Sets, and Dic?onaries Func?ons and Modules Input / Output 30
Basic Input
• 
• 
raw_input gets a string from the user. Important to convert before using for numerical (or other) purposes. 31
Conditionals
• 
• 
Must include a colon amer the condi?on. All indented lines are executed. 32
Indentation
• 
Amount of indenta?on maVers! 33
While Loops
• 
Important: En?re list of things is completed before the boolean condi?on is checked. • 
What happens if condi?on never changes? If its true, results in an infinite loop –  Unless… – 
35
While Loops
• 
Important: En?re list of things is completed between each boolean check. • 
What happens if condi?on never changes? If its true, results in an infinite loop –  Unless… – 
36
Break statement
• 
Alternate way to terminate a loop. • 
Both programs above produce the same output (and are completely iden?cal). Use break sparingly (acts similar to goto). • 
37
A note on break
• 
Only the innermost loop is terminated. • 
Code above gets caught in an infinite loop amer prin?ng 10. 38
A note on bugs in your programs
• 
Debugging. – 
Cyclic process of edi?ng, compiling, and fixing errors. “As soon as we started programming, we found out to our surprise that it wasn’t as easy to get programs right as we had thought. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.” – Sir Maurice Wilkes • 
It is very normal to make (many) mistakes as you write programs. 39
Bug
• 
• 
Term predates computers and somware. Popularized as a term by Grace Hopper. 40
Simple Debugging
• 
Three types of errors – 
– 
– 
• 
Syntax error: Python interpreter yells at you and gives you a line of code Seman?c error: Code does not behave like you intended, probably no error message Performance error: Code is too slow or uses too much memory. Techniques for handling seman?c errors for now: – 
– 
Add print statements to your code. Use the visualizer described in the lab. 41
Lab
• 
For the rest of today, you should work on the lab. hVp://joshh.ug/SRPC/ –  Click on the Coursemology link and sign up. –  Join the Rockefeller course from the list of courses. –  Ok to work together! –  Flag me down if you’re stuck. –  If you get done early, consider s?cking around to help other students, or alternately you can start on assignment 1. – 
42
Download