Presentation is loading. Please wait.

Presentation is loading. Please wait.

Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!

Similar presentations


Presentation on theme: "Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!"— Presentation transcript:

1 Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!

2 UC Merced2 Schedule December 17 6-9pm Quiz #6 But, there will be a lab! Masa will be in Reno next week!

3 UC Merced3 Math 15 Now till the end of Fall semester  Four more Labs  One more Assignment ( HW #6 due November 18) Total of 6 assignments  Two more Quizzes (Today-20pts and Nov. 28-30pts) Total of 6 quizzes  Project #2 (due December 7) – Available @ November 19 th  Final Exam – December 17 th

4 UC Merced4 Grading for Math 15 ActivityPoints% Final Grade Assignments10020% In-Class Quizzes10020% Computer Labs6012% Project #17014% Project #27014% Final Exam10020% Total500100% ActivityPoints Assignments110 In-Class Quizzes130 Computer Labs60 Project #170 Project #270 Final project100 Total540 Projected points GradeTotal points achieved AOver 425 BOver 375 COver 325 DOver 275

5 UC Merced5 Bonus or Extra Points  So far Lab/homework/project – 15 points Seminar Report – 50 points (Done) Halloween Bonus – 10 points (Done)  Near future Maybe …

6 UC Merced6  Any Questions?

7 UC Merced7  After Three weeks of Python Programming experience, what did you think? python

8 UC Merced8 Don’t make Python bother you. Don’t be scared! Python is just a Tool!

9 UC Merced9 This Week  More Programming Review New materials for Next Week’s Lab  Dictionaries  Input from a file / Output to a file  Defining function Homework #6 is due November 18 th ! Next Week ’ s Lab (#9) is available at the UCMCROPS.

10 UC Merced10 Quick Reminder – Part 1  List UC = [“UCLA”, “UC Davis”, “UC Merced”, “UC Berkeley”, “UCSF”, “UCI”] # of elements in UC = 6 >>> print UC[1] UC Davis >>> print UC[6] Error! Python – Index of List starts at 0

11 UC Merced11 Quick Reminder – Part 2  range() function range(n) : a list of integers from 0 to up to n, but not including n.  Concept of “ Counter ” >>> range(10) [0,1,2,3,4,5,6,7,8,9] N = 15 icount = 0 # Initializing a variable for var in range(1,N,2): icount = icount + 1 print var, icount >>> 1 3 2 5 3 7 4 9 5 11 6 13 7

12 UC Merced12 Dictionary  Mutable (or changeable) table of object references, accessed by keys.  Is like an address-book where you can find the address or contact details of a person by knowing only his/her name i.e. we associate keys (name) with corresponding values (details).  Note that the key must be unique to have associated values. just like you cannot find the correct information if you have two persons with the exact same name.

13 UC Merced13 Dictionary – cont.  In python, dictionary entries are enclosed in braces {}. Here is the general syntax: Name_of_dictionary = { key1 : value1, key2 : value2 } UC = { 'Bruins' : ‘UCLA', ‘Bobcats' : ‘UC Merced', 'Highlanders' : ‘UC Riverside', ‘Anteaters' : ‘UCI' ‘Banana Slugs’: ‘UC Santa Cruz’} >>> UC[‘Highlanders’] UC Riverside

14 UC Merced14 Example: DNA Sequence Analysis  Objectives:  Convert a DNA sequence to a protein sequence by using the translation table.  Then, use the resulting sequence to identify your protein and its function. ATGAGGAAAATGCTGACCGCTGTGCTGTCTCACGTATTTTCGGGAATGGTCCAAAAGCCAGCTCTCAGAG GACTGCTGTCATCTCTGAAGTTCTCCAACGACGCCACCTGTGACATTAAGAAATGTGACCTGTACCGGCT GGAGGAGGGCCCACCGACCTCCACCGTGCTCACCCGAGCCGAGGCCCTCAAGTACTACCGGACCATGCAG GTAATTCGGCGCATGGAGTTGAAGGCCGACCAGCTGTATAAGCAGAAATTCATCCGTGGTTTCTGTCACC TGTGTGATGGGCAGGAAGCCTGCTGCGTGGGGCTGGAGGCAGGGATAAATCCCACGGATCACGTCATCAC GTCCTACCGGGCTCATGGCTTCTGCTACACGCGAGGACTGTCCGTGAAGTCCATTCTCGCCGAGCTGACT GGACGCAAAGGAGGCTGTGCTAAAGGCAAGGGAGGCTCCATGCACATGTACGGCAAGAACTTCTACGGTG GCAATGGCATTGTTGGGGCCCAGGTACCCCTGGGAGCTGGTGTGGCTTTTGCCTGTAAATACCTGAAGAA TGGTCAGGTCTGCTTGGCTTTGTACGGCGATGGTGCGGCTAACCAAGGGCAGGTATTCGAAGCATACAAT ATGTCAGCCTTGTGGAAATTACCCTGTGTTTTCATCTGTGAGAATAACCTCTATGGAATGGGAACCTCCA ACGAGAGATCAGCAGCCAGTACTGATTACCACAAGAAAGGTTTTATTATCCCCGGACTGAGGGTGAATGG GATGGATATTCTCTGTGTTCGGGAGGCAACCAAGTTTGCAGCTGATCACTGCAGATCTGGAAAGGGGCCC ATTGTGATGGAGCTGCAGACCTACCGTTATCATGGACACAGTATGAGCGACCCAGGGATCAGTTATCGTT CACGAGAAGAAGTTCATAACGTGAGAAGTAAGAGTGATCCTATAATGCTGCTCCGAGAGAGAATTATCAG CAACAACCTCAGCAATATTGAAGAATTGAAAGAAATTGATGCAGATGTGAAGAAAGAGGTGGAGGACGCA GCTCAGTTTGCTACGACTGATCCAGAACCAGCTGTGGAAGATATAGCCAATTACCTCTACCACCAAGATC CACCTTTTGAAGTCCGTGGTGCACATAAGTGGCTCAAGTATAAGTCCCACAGTTAG

15 UC Merced15 How to convert? The modern genetic code uses triplets of letters to form the words or codons of the genetic language.

16 UC Merced16 Create the simple Python program to do the job!  First: Convert a codon table into Python ’ s dictionary Code = {'TTT': 'F', 'TCT': 'S', 'TAT': 'Y', 'TGT': 'C', 'TTC': 'F', 'TCC': 'S', 'TAC': 'Y', 'TGC': 'C', 'TTA': 'L', 'TCA': 'S', 'TAA': '*', 'TGA': 'W', 'TTG': 'L', 'TCG': 'S', 'TAG': '*', 'TGG': 'W', 'CTT': 'L', 'CCT': 'P', 'CAT': 'H', 'CGT': 'R', 'CTC': 'L', 'CCC': 'P', 'CAC': 'H', 'CGC': 'R', 'CTA': 'L', 'CCA': 'P', 'CAA': 'Q', 'CGA': 'R', 'CTG': 'L', 'CCG': 'P', 'CAG': 'Q', 'CGG': 'R', 'ATT': 'I', 'ACT': 'T', 'AAT': 'N', 'AGT': 'S', 'ATC': 'I', 'ACC': 'T', 'AAC': 'N', 'AGC': 'S', 'ATA': 'M', 'ACA': 'T', 'AAA': 'K', 'AGA': 'R', 'ATG': 'M', 'ACG': 'T', 'AAG': 'K', 'AGG': 'R', 'GTT': 'V', 'GCT': 'A', 'GAT': 'D', 'GGT': 'G', 'GTC': 'V', 'GCC': 'A', 'GAC': 'D', 'GGC': 'G', 'GTA': 'V', 'GCA': 'A', 'GAA': 'E', 'GGA': 'G', 'GTG': 'V', 'GCG': 'A', 'GAG': 'E', 'GGG': 'G'}

17 UC Merced17 Functions  Functions are reusable pieces of programs. i.e. range(5), math.sqrt(9), pow(5,2) etc. Using that function any number of times and anywhere in your program.  This is known as calling the function.

18 UC Merced18 Defining Functions  Functions are defined using the def keyword. This is followed by an identifier name for the function followed by a pair of parentheses which may enclose some names of variables and the line ends with a colon. This is followed by a block of statements that form the body of the function. Syntax def function_name(arguments): “function_documentaion_string” function_body_suite

19 UC Merced19 Defining Functions - cont  Example # python Program # def say_hello(): print 'Hello World!' # body of the function say_hello() # calling the function say_hello() Hello World!

20 UC Merced20 Defining Functions – cont.  Another Example # Python Program # Min & Max # def print_max(x, y): if x > y: print x, 'is maximum' else: print y, 'is maximum' print_max(3, 4) # directly give literal constants as arguments a = 5 b = 7 print_max(a, b) # pass in variables as arguments 4 is maximum 7 is maximum

21 UC Merced21 Defining Functions  Yet Another Example The return statement is used to return from a function (i.e. break out from a function). We can optionally return a value from the function as well. # python No. 3 def maximum(x, y): if x > y: return x else: return y print maximum(2, 3) 3

22 UC Merced22 Calculation of n! and (n!/r!)  One way: n = input ("What is n? ") icount = 1 for i in range(1,n+1): icount = icount * i nfact = icount pnint n,"! =", nfact r = input("What is r? ") icount = 1 for i in range(1,r+1): icount = icount * i rfact = icount print r,"! = ", rfact print float(nfact)/(rfact) n! r!  Another way: #================================== def fact(x): icount = 1 for i in range(1,x+1): icount = icount * i fact = icount print n,"! = ", fact return fact #================================== n = input("What is n? ") r = input("What is r? ") a = fact(n) b = fact(r) print float(a)/(b) Reusing the function

23 UC Merced23 Files and Input/Output  Working with files is a lot like working with notebooks. To use a notebook, you have to open it. When you're done, you have to close it.  With Python, it ’ s easy to read strings from plain text files and write to plain text files.

24 UC Merced24 Open a file for reading data!  This time for reading, and read the contents into a string. This time, the mode argument is "r" for reading: >>> shrimp = open("test.dat","r") If we try to open a file that doesn't exist, we get an error.  The readline method reads all the characters up to and including the next newline character: >>> shrimp = open("test.dat","r") >>> print shrimp.readline() Now is the time to close the file  The read method reads data from the file. With no arguments, it reads the entire contents of the file: >>> text = shrimp.read() >>> print text Now is the time to close the file

25 UC Merced25 Open a file for writing!  Opening a file creates a file object. In this example, the variable bobcat refers to the new file object. >>> bobcat = open("test.dat","w") The open function takes two arguments.  The first is the name of the file, and  the second is the mode. Mode "w" means that we are opening the file for writing. If there is no file named test.dat, it will be created. If there already is one, it will be replaced by the file we are writing.  To put data in the file we invoke the write method on the file object: >>> bobcat.write("Now is the time") >>> bobcat.write("to close the file")  Closing the file tells the system that we are done writing and makes the file available for reading: >>> bobcat.close()

26 UC Merced26 Example – No.1: # # Program to read and print a file # file = open(“I_hate_Python.txt","r") text = file.readlines() file.close() for line in text: print line, file_out = open(“I_Love_Python.txt","w") file_out.writelines(text) file_out.close()

27 UC Merced27 Example- No. 2:  Raw_data.txt - 3.0,4.0,5.0,6.0  Incorrect way to do this: infile = open("raw_data.txt","r") icount = 0 for line in infile: # Loop through each line of the file icount = icount + line print line, icount  Correct way to do this infile = open("raw_data.txt","r") icount = 0 for line in infile: icount = icount + float(line) print line, icount

28 UC Merced28 Type Conversion  Type conversion functions are useful. Especially, a time when you need to read data sets from the file.  Python reads data sets as strings. You need to convert to numerical types in order to conduct some mathematical operations. Function Description int(x ) converts x to an integer float(x) converts x to a floating-point number (or real number) str(x) converts x to a string representation list(s) converts a sequence to a list chr(x) converts an integer to a character

29 UC Merced29 Let’s start programming Again! The best way to learn a programming is To Practice! python


Download ppt "Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!"

Similar presentations


Ads by Google